2020-08-26 19:55:40 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
|
|
|
import { GetState } from '../../container/types';
|
2020-08-26 21:03:23 +03:00
|
|
|
import { OptionalString } from '../../utils/utils';
|
2021-03-27 15:56:44 +03:00
|
|
|
import { EditShortUrlData, ShortUrl } from '../data';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ProblemDetailsError } from '../../api/types';
|
2020-12-22 11:24:33 +03:00
|
|
|
import { parseApiError } from '../../api/utils';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
2020-03-30 21:42:58 +03:00
|
|
|
|
|
|
|
export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START';
|
|
|
|
export const EDIT_SHORT_URL_ERROR = 'shlink/shortUrlEdition/EDIT_SHORT_URL_ERROR';
|
|
|
|
export const SHORT_URL_EDITED = 'shlink/shortUrlEdition/SHORT_URL_EDITED';
|
|
|
|
|
2020-08-26 19:55:40 +03:00
|
|
|
export interface ShortUrlEdition {
|
2021-03-27 15:56:44 +03:00
|
|
|
shortUrl?: ShortUrl;
|
2020-08-26 19:55:40 +03:00
|
|
|
saving: boolean;
|
|
|
|
error: boolean;
|
2020-12-21 23:26:45 +03:00
|
|
|
errorData?: ProblemDetailsError;
|
2020-08-26 19:55:40 +03:00
|
|
|
}
|
|
|
|
|
2021-03-27 15:56:44 +03:00
|
|
|
export interface ShortUrlEditedAction extends Action<string> {
|
|
|
|
shortUrl: ShortUrl;
|
2020-08-26 19:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: ShortUrlEdition = {
|
2020-03-30 21:42:58 +03:00
|
|
|
saving: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2021-08-21 18:53:06 +03:00
|
|
|
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction & ApiErrorAction>({
|
2020-03-30 21:42:58 +03:00
|
|
|
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
|
2020-12-21 23:26:45 +03:00
|
|
|
[EDIT_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }),
|
2021-03-27 15:56:44 +03:00
|
|
|
[SHORT_URL_EDITED]: (_, { shortUrl }) => ({ shortUrl, saving: false, error: false }),
|
2020-03-30 21:42:58 +03:00
|
|
|
}, initialState);
|
|
|
|
|
2020-08-26 19:55:40 +03:00
|
|
|
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
|
|
|
shortCode: string,
|
2020-08-26 21:03:23 +03:00
|
|
|
domain: OptionalString,
|
2021-03-27 11:49:47 +03:00
|
|
|
data: EditShortUrlData,
|
2020-08-26 19:55:40 +03:00
|
|
|
) => async (dispatch: Dispatch, getState: GetState) => {
|
2020-03-30 21:42:58 +03:00
|
|
|
dispatch({ type: EDIT_SHORT_URL_START });
|
2021-03-27 11:49:47 +03:00
|
|
|
|
2022-05-01 11:30:51 +03:00
|
|
|
const { updateShortUrl } = buildShlinkApiClient(getState);
|
2020-03-30 21:42:58 +03:00
|
|
|
|
|
|
|
try {
|
2022-05-01 11:30:51 +03:00
|
|
|
const shortUrl = await updateShortUrl(shortCode, domain, data as any); // FIXME parse dates;
|
2021-03-27 11:49:47 +03:00
|
|
|
|
2021-03-27 15:56:44 +03:00
|
|
|
dispatch<ShortUrlEditedAction>({ shortUrl, type: SHORT_URL_EDITED });
|
2021-10-31 14:38:42 +03:00
|
|
|
} catch (e: any) {
|
2021-08-21 18:53:06 +03:00
|
|
|
dispatch<ApiErrorAction>({ type: EDIT_SHORT_URL_ERROR, errorData: parseApiError(e) });
|
2020-03-30 21:42:58 +03:00
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|