2022-11-06 21:06:39 +03:00
|
|
|
import { PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
import { 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 { parseApiError } from '../../api/utils';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
2022-10-12 11:35:16 +03:00
|
|
|
import { ProblemDetailsError } from '../../api/types/errors';
|
2020-08-28 19:33:37 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-06 21:06:39 +03:00
|
|
|
export type ShortUrlDetailAction = PayloadAction<ShortUrl>;
|
2020-08-28 19:33:37 +03:00
|
|
|
|
|
|
|
const initialState: ShortUrlDetail = {
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2021-08-21 18:53:06 +03:00
|
|
|
export default buildReducer<ShortUrlDetail, ShortUrlDetailAction & ApiErrorAction>({
|
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 }),
|
2022-11-06 21:06:39 +03:00
|
|
|
[GET_SHORT_URL_DETAIL]: (_, { payload: shortUrl }) => ({ shortUrl, ...initialState }),
|
2020-08-28 19:33:37 +03:00
|
|
|
}, 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();
|
2022-11-06 21:06:39 +03:00
|
|
|
const payload = shortUrlsList?.shortUrls?.data.find(
|
2022-03-26 14:17:42 +03:00
|
|
|
(url) => shortUrlMatches(url, shortCode, domain),
|
2021-03-05 18:25:20 +03:00
|
|
|
) ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
|
2020-08-28 19:33:37 +03:00
|
|
|
|
2022-11-06 21:06:39 +03:00
|
|
|
dispatch<ShortUrlDetailAction>({ payload, type: GET_SHORT_URL_DETAIL });
|
2021-10-31 14:38:42 +03:00
|
|
|
} catch (e: any) {
|
2021-08-21 18:53:06 +03:00
|
|
|
dispatch<ApiErrorAction>({ type: GET_SHORT_URL_DETAIL_ERROR, errorData: parseApiError(e) });
|
2020-08-28 19:33:37 +03:00
|
|
|
}
|
|
|
|
};
|