mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Migrated editShortUrl payload action
This commit is contained in:
parent
a316366ae9
commit
bf84e4a2ed
6 changed files with 13 additions and 11 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { Action } from 'redux';
|
import { Action } from 'redux';
|
||||||
import { ProblemDetailsError } from './errors';
|
import { ProblemDetailsError } from './errors';
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
export interface ApiErrorAction extends Action<string> {
|
export interface ApiErrorAction extends Action<string> {
|
||||||
errorData?: ProblemDetailsError;
|
errorData?: ProblemDetailsError;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Action, Dispatch } from 'redux';
|
import { PayloadAction } from '@reduxjs/toolkit';
|
||||||
|
import { Dispatch } from 'redux';
|
||||||
import { buildReducer } from '../../utils/helpers/redux';
|
import { buildReducer } from '../../utils/helpers/redux';
|
||||||
import { GetState } from '../../container/types';
|
import { GetState } from '../../container/types';
|
||||||
import { OptionalString } from '../../utils/utils';
|
import { OptionalString } from '../../utils/utils';
|
||||||
|
@ -19,9 +20,7 @@ export interface ShortUrlEdition {
|
||||||
errorData?: ProblemDetailsError;
|
errorData?: ProblemDetailsError;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ShortUrlEditedAction extends Action<string> {
|
export type ShortUrlEditedAction = PayloadAction<ShortUrl>;
|
||||||
shortUrl: ShortUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
const initialState: ShortUrlEdition = {
|
const initialState: ShortUrlEdition = {
|
||||||
saving: false,
|
saving: false,
|
||||||
|
@ -31,7 +30,7 @@ const initialState: ShortUrlEdition = {
|
||||||
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction & ApiErrorAction>({
|
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction & ApiErrorAction>({
|
||||||
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
|
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
|
||||||
[EDIT_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }),
|
[EDIT_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, saving: false, error: true, errorData }),
|
||||||
[SHORT_URL_EDITED]: (_, { shortUrl }) => ({ shortUrl, saving: false, error: false }),
|
[SHORT_URL_EDITED]: (_, { payload: shortUrl }) => ({ shortUrl, saving: false, error: false }),
|
||||||
}, initialState);
|
}, initialState);
|
||||||
|
|
||||||
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
|
@ -44,9 +43,9 @@ export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||||
const { updateShortUrl } = buildShlinkApiClient(getState);
|
const { updateShortUrl } = buildShlinkApiClient(getState);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const shortUrl = await updateShortUrl(shortCode, domain, data as any); // FIXME parse dates;
|
const payload = await updateShortUrl(shortCode, domain, data as any); // FIXME parse dates;
|
||||||
|
|
||||||
dispatch<ShortUrlEditedAction>({ shortUrl, type: SHORT_URL_EDITED });
|
dispatch<ShortUrlEditedAction>({ payload, type: SHORT_URL_EDITED });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
dispatch<ApiErrorAction>({ type: EDIT_SHORT_URL_ERROR, errorData: parseApiError(e) });
|
dispatch<ApiErrorAction>({ type: EDIT_SHORT_URL_ERROR, errorData: parseApiError(e) });
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
|
||||||
state,
|
state,
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
[SHORT_URL_EDITED]: (state, { shortUrl: editedShortUrl }) => (!state.shortUrls ? state : assocPath(
|
[SHORT_URL_EDITED]: (state, { payload: editedShortUrl }) => (!state.shortUrls ? state : assocPath(
|
||||||
['shortUrls', 'data'],
|
['shortUrls', 'data'],
|
||||||
state.shortUrls.data.map((shortUrl) => {
|
state.shortUrls.data.map((shortUrl) => {
|
||||||
const { shortCode, domain } = editedShortUrl;
|
const { shortCode, domain } = editedShortUrl;
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { ShlinkState } from '../../container/types';
|
||||||
type ActionHandler<State, AT> = (currentState: State, action: AT) => State;
|
type ActionHandler<State, AT> = (currentState: State, action: AT) => State;
|
||||||
type ActionHandlerMap<State, AT> = Record<string, ActionHandler<State, AT>>;
|
type ActionHandlerMap<State, AT> = Record<string, ActionHandler<State, AT>>;
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<State, AT>, initialState: State) => (
|
export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<State, AT>, initialState: State) => (
|
||||||
state: State | undefined,
|
state: State | undefined,
|
||||||
action: AT,
|
action: AT,
|
||||||
|
@ -16,6 +17,7 @@ export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<Sta
|
||||||
return actionHandler ? actionHandler(currentState, action) : currentState;
|
return actionHandler ? actionHandler(currentState, action) : currentState;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @deprecated */
|
||||||
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
|
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
|
||||||
|
|
||||||
export const createAsyncThunk = <Returned, ThunkArg>(
|
export const createAsyncThunk = <Returned, ThunkArg>(
|
||||||
|
|
|
@ -31,7 +31,7 @@ describe('shortUrlEditionReducer', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
||||||
expect(reducer(undefined, { type: SHORT_URL_EDITED, shortUrl })).toEqual({
|
expect(reducer(undefined, { type: SHORT_URL_EDITED, payload: shortUrl })).toEqual({
|
||||||
shortUrl,
|
shortUrl,
|
||||||
saving: false,
|
saving: false,
|
||||||
error: false,
|
error: false,
|
||||||
|
@ -55,7 +55,7 @@ describe('shortUrlEditionReducer', () => {
|
||||||
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
|
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
|
||||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
||||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, shortUrl });
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, payload: shortUrl });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('dispatches error on failure', async () => {
|
it('dispatches error on failure', async () => {
|
||||||
|
|
|
@ -181,7 +181,7 @@ describe('shortUrlsListReducer', () => {
|
||||||
error: false,
|
error: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = reducer(state, { type: SHORT_URL_EDITED, shortUrl: editedShortUrl } as any);
|
const result = reducer(state, { type: SHORT_URL_EDITED, payload: editedShortUrl } as any);
|
||||||
|
|
||||||
expect(result.shortUrls?.data).toEqual(expectedList);
|
expect(result.shortUrls?.data).toEqual(expectedList);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue