2020-08-28 19:33:37 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2021-03-05 18:04:02 +03:00
|
|
|
import { ShortUrl } from '../data';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { OptionalString } from '../../utils/utils';
|
|
|
|
import { GetState } from '../../container/types';
|
2021-03-05 18:25:20 +03:00
|
|
|
import { shortUrlMatches } from '../helpers';
|
2021-03-20 18:32:12 +03:00
|
|
|
import { ProblemDetailsError } from '../../api/types';
|
|
|
|
import { parseApiError } from '../../api/utils';
|
2020-08-28 19:33:37 +03:00
|
|
|
|
|
|
|
/* eslint-disable padding-line-between-statements */
|
|
|
|
export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START';
|
|
|
|
export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR';
|
|
|
|
export const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
|
|
|
|
/* eslint-enable padding-line-between-statements */
|
|
|
|
|
|
|
|
export interface ShortUrlDetail {
|
|
|
|
shortUrl?: ShortUrl;
|
|
|
|
loading: boolean;
|
|
|
|
error: boolean;
|
2021-03-20 18:32:12 +03:00
|
|
|
errorData?: ProblemDetailsError;
|
2020-08-28 19:33:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ShortUrlDetailAction extends Action<string> {
|
|
|
|
shortUrl: ShortUrl;
|
|
|
|
}
|
|
|
|
|
2021-03-20 18:32:12 +03:00
|
|
|
export interface ShortUrlDetailFailedAction extends Action<string> {
|
|
|
|
errorData?: ProblemDetailsError;
|
|
|
|
}
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const initialState: ShortUrlDetail = {
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2021-03-20 18:32:12 +03:00
|
|
|
export default buildReducer<ShortUrlDetail, ShortUrlDetailAction & ShortUrlDetailFailedAction>({
|
2020-08-28 19:33:37 +03:00
|
|
|
[GET_SHORT_URL_DETAIL_START]: () => ({ loading: true, error: false }),
|
2021-03-20 18:32:12 +03:00
|
|
|
[GET_SHORT_URL_DETAIL_ERROR]: (_, { errorData }) => ({ loading: false, error: true, errorData }),
|
2020-08-28 19:33:37 +03:00
|
|
|
[GET_SHORT_URL_DETAIL]: (_, { shortUrl }) => ({ shortUrl, ...initialState }),
|
|
|
|
}, initialState);
|
|
|
|
|
|
|
|
export const getShortUrlDetail = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
|
|
|
shortCode: string,
|
|
|
|
domain: OptionalString,
|
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
dispatch({ type: GET_SHORT_URL_DETAIL_START });
|
|
|
|
|
|
|
|
try {
|
2021-03-05 18:25:20 +03:00
|
|
|
const { shortUrlsList } = getState();
|
|
|
|
const shortUrl = shortUrlsList?.shortUrls?.data.find(
|
|
|
|
(shortUrl) => shortUrlMatches(shortUrl, shortCode, domain),
|
|
|
|
) ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
|
2020-08-28 19:33:37 +03:00
|
|
|
|
|
|
|
dispatch<ShortUrlDetailAction>({ shortUrl, type: GET_SHORT_URL_DETAIL });
|
|
|
|
} catch (e) {
|
2021-03-20 18:32:12 +03:00
|
|
|
dispatch<ShortUrlDetailFailedAction>({ type: GET_SHORT_URL_DETAIL_ERROR, errorData: parseApiError(e) });
|
2020-08-28 19:33:37 +03:00
|
|
|
}
|
|
|
|
};
|