Migrated deleteShortUrl action creator to use PayloadAction and have a single param

This commit is contained in:
Alejandro Celaya
2022-11-06 12:46:29 +01:00
parent 2a268de2cb
commit d468fb1efe
5 changed files with 36 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
import { Action, Dispatch } from 'redux';
import { PayloadAction } from '@reduxjs/toolkit';
import { Dispatch } from 'redux';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { GetState } from '../../container/types';
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
@@ -18,11 +19,13 @@ export interface ShortUrlDeletion {
errorData?: ProblemDetailsError;
}
export interface DeleteShortUrlAction extends Action<string> {
export interface DeleteShortUrl {
shortCode: string;
domain?: string | null;
}
export type DeleteShortUrlAction = PayloadAction<DeleteShortUrl>;
const initialState: ShortUrlDeletion = {
shortCode: '',
loading: false,
@@ -32,20 +35,24 @@ const initialState: ShortUrlDeletion = {
export default buildReducer<ShortUrlDeletion, DeleteShortUrlAction & ApiErrorAction>({
[DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }),
[DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }),
[SHORT_URL_DELETED]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }),
[SHORT_URL_DELETED]: (state, { payload }) => (
{ ...state, shortCode: payload.shortCode, loading: false, error: false }
),
[RESET_DELETE_SHORT_URL]: () => initialState,
}, initialState);
export const deleteShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain?: string | null,
{ shortCode, domain }: DeleteShortUrl,
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: DELETE_SHORT_URL_START });
const { deleteShortUrl: shlinkDeleteShortUrl } = buildShlinkApiClient(getState);
try {
await shlinkDeleteShortUrl(shortCode, domain);
dispatch<DeleteShortUrlAction>({ type: SHORT_URL_DELETED, shortCode, domain });
dispatch<DeleteShortUrlAction>({
type: SHORT_URL_DELETED,
payload: { shortCode, domain },
});
} catch (e: any) {
dispatch<ApiErrorAction>({ type: DELETE_SHORT_URL_ERROR, errorData: parseApiError(e) });

View File

@@ -44,10 +44,12 @@ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
[LIST_SHORT_URLS_START]: (state) => ({ ...state, loading: true, error: false }),
[LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true }),
[LIST_SHORT_URLS]: (_, { shortUrls }) => ({ loading: false, error: false, shortUrls }),
// [`${SHORT_URL_DELETED}/fulfilled`]: pipe( // TODO Do not hardcode action type here
[SHORT_URL_DELETED]: pipe(
(state: ShortUrlsList, { shortCode, domain }: DeleteShortUrlAction) => (!state.shortUrls ? state : assocPath(
(state: ShortUrlsList, { payload }: DeleteShortUrlAction) => (!state.shortUrls ? state : assocPath(
['shortUrls', 'data'],
reject<ShortUrl, ShortUrl[]>((shortUrl) => shortUrlMatches(shortUrl, shortCode, domain), state.shortUrls.data),
reject<ShortUrl, ShortUrl[]>((shortUrl) =>
shortUrlMatches(shortUrl, payload.shortCode, payload.domain), state.shortUrls.data),
state,
)),
(state) => (!state.shortUrls ? state : assocPath(
@@ -89,6 +91,7 @@ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
state,
)),
),
// TODO Do not hardcode action type here
[`${SHORT_URL_EDITED}/fulfilled`]: (state, { payload: editedShortUrl }) => (!state.shortUrls ? state : assocPath(
['shortUrls', 'data'],
state.shortUrls.data.map((shortUrl) => {