mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-19 21:16:18 +00:00
Migrated tagEdit reducer to RTK
This commit is contained in:
@@ -10,7 +10,6 @@ import orphanVisitsReducer from '../visits/reducers/orphanVisits';
|
||||
import nonOrphanVisitsReducer from '../visits/reducers/nonOrphanVisits';
|
||||
import tagsListReducer from '../tags/reducers/tagsList';
|
||||
import tagDeleteReducer from '../tags/reducers/tagDelete';
|
||||
import tagEditReducer from '../tags/reducers/tagEdit';
|
||||
import { settingsReducer } from '../settings/reducers/settings';
|
||||
import visitsOverviewReducer from '../visits/reducers/visitsOverview';
|
||||
import { appUpdatesReducer } from '../app/reducers/appUpdates';
|
||||
@@ -32,7 +31,7 @@ export default (container: IContainer) => combineReducers<ShlinkState>({
|
||||
nonOrphanVisits: nonOrphanVisitsReducer,
|
||||
tagsList: tagsListReducer,
|
||||
tagDelete: tagDeleteReducer,
|
||||
tagEdit: tagEditReducer,
|
||||
tagEdit: container.tagEditReducer,
|
||||
mercureInfo: container.mercureInfoReducer,
|
||||
settings: settingsReducer,
|
||||
domainsList: container.domainsListReducer,
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
import { pick } from 'ramda';
|
||||
import { createAction, PayloadAction } from '@reduxjs/toolkit';
|
||||
import { Dispatch } from 'redux';
|
||||
import { buildReducer } from '../../utils/helpers/redux';
|
||||
import { GetState } from '../../container/types';
|
||||
import { createAction, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createAsyncThunk } from '../../utils/helpers/redux';
|
||||
import { ColorGenerator } from '../../utils/services/ColorGenerator';
|
||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||
import { parseApiError } from '../../api/utils';
|
||||
import { ApiErrorAction } from '../../api/types/actions';
|
||||
import { ProblemDetailsError } from '../../api/types/errors';
|
||||
|
||||
export const EDIT_TAG_START = 'shlink/editTag/EDIT_TAG_START';
|
||||
export const EDIT_TAG_ERROR = 'shlink/editTag/EDIT_TAG_ERROR';
|
||||
export const EDIT_TAG = 'shlink/editTag/EDIT_TAG';
|
||||
export const TAG_EDITED = 'shlink/editTag/TAG_EDITED';
|
||||
const EDIT_TAG = 'shlink/editTag/EDIT_TAG';
|
||||
const TAG_EDITED = 'shlink/editTag/TAG_EDITED';
|
||||
|
||||
export interface TagEdition {
|
||||
oldName?: string;
|
||||
@@ -37,35 +32,37 @@ const initialState: TagEdition = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export default buildReducer<TagEdition, EditTagAction & ApiErrorAction>({
|
||||
[EDIT_TAG_START]: () => ({ editing: true, edited: false, error: false }),
|
||||
[EDIT_TAG_ERROR]: (_, { errorData }) => ({ editing: false, edited: false, error: true, errorData }),
|
||||
[EDIT_TAG]: (_, { payload }) => ({
|
||||
...pick(['oldName', 'newName'], payload),
|
||||
editing: false,
|
||||
edited: true,
|
||||
error: false,
|
||||
}),
|
||||
}, initialState);
|
||||
|
||||
export const editTag = (buildShlinkApiClient: ShlinkApiClientBuilder, colorGenerator: ColorGenerator) => (
|
||||
{ oldName, newName, color }: EditTag,
|
||||
) => async (dispatch: Dispatch, getState: GetState) => {
|
||||
dispatch({ type: EDIT_TAG_START });
|
||||
const { editTag: shlinkEditTag } = buildShlinkApiClient(getState);
|
||||
|
||||
try {
|
||||
await shlinkEditTag(oldName, newName);
|
||||
colorGenerator.setColorForKey(newName, color);
|
||||
dispatch<EditTagAction>({
|
||||
type: EDIT_TAG,
|
||||
payload: { oldName, newName, color },
|
||||
});
|
||||
} catch (e: any) {
|
||||
dispatch<ApiErrorAction>({ type: EDIT_TAG_ERROR, errorData: parseApiError(e) });
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
export const tagEdited = createAction<EditTag>(TAG_EDITED);
|
||||
|
||||
export const tagEditReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder, colorGenerator: ColorGenerator) => {
|
||||
const editTag = createAsyncThunk(
|
||||
EDIT_TAG,
|
||||
async ({ oldName, newName, color }: EditTag, { getState }): Promise<EditTag> => {
|
||||
await buildShlinkApiClient(getState).editTag(oldName, newName);
|
||||
colorGenerator.setColorForKey(newName, color);
|
||||
|
||||
return { oldName, newName, color };
|
||||
},
|
||||
);
|
||||
|
||||
const { reducer } = createSlice({
|
||||
name: 'tagEditReducer',
|
||||
initialState,
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(editTag.pending, () => ({ editing: true, edited: false, error: false }));
|
||||
builder.addCase(
|
||||
editTag.rejected,
|
||||
(_, { error }) => ({ editing: false, edited: false, error: true, errorData: parseApiError(error) }),
|
||||
);
|
||||
builder.addCase(editTag.fulfilled, (_, { payload }) => ({
|
||||
...pick(['oldName', 'newName'], payload),
|
||||
editing: false,
|
||||
edited: true,
|
||||
error: false,
|
||||
}));
|
||||
},
|
||||
});
|
||||
|
||||
return { reducer, editTag };
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { prop } from 'ramda';
|
||||
import Bottle, { IContainer } from 'bottlejs';
|
||||
import { TagsSelector } from '../helpers/TagsSelector';
|
||||
import { TagCard } from '../TagCard';
|
||||
@@ -6,7 +7,7 @@ import { EditTagModal } from '../helpers/EditTagModal';
|
||||
import { TagsList } from '../TagsList';
|
||||
import { filterTags, listTags } from '../reducers/tagsList';
|
||||
import { deleteTag, tagDeleted } from '../reducers/tagDelete';
|
||||
import { editTag, tagEdited } from '../reducers/tagEdit';
|
||||
import { tagEdited, tagEditReducerCreator } from '../reducers/tagEdit';
|
||||
import { ConnectDecorator } from '../../container/types';
|
||||
import { TagsCards } from '../TagsCards';
|
||||
import { TagsTable } from '../TagsTable';
|
||||
@@ -36,6 +37,10 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
['forceListTags', 'filterTags', 'createNewVisits', 'loadMercureInfo'],
|
||||
));
|
||||
|
||||
// Reducers
|
||||
bottle.serviceFactory('tagEditReducerCreator', tagEditReducerCreator, 'buildShlinkApiClient', 'ColorGenerator');
|
||||
bottle.serviceFactory('tagEditReducer', prop('reducer'), 'tagEditReducerCreator');
|
||||
|
||||
// Actions
|
||||
const listTagsActionFactory = (force: boolean) =>
|
||||
({ buildShlinkApiClient }: IContainer) => listTags(buildShlinkApiClient, force);
|
||||
@@ -43,11 +48,12 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
bottle.factory('listTags', listTagsActionFactory(false));
|
||||
bottle.factory('forceListTags', listTagsActionFactory(true));
|
||||
bottle.serviceFactory('filterTags', () => filterTags);
|
||||
bottle.serviceFactory('tagDeleted', () => tagDeleted);
|
||||
bottle.serviceFactory('tagEdited', () => tagEdited);
|
||||
|
||||
bottle.serviceFactory('deleteTag', deleteTag, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('editTag', editTag, 'buildShlinkApiClient', 'ColorGenerator');
|
||||
bottle.serviceFactory('tagDeleted', () => tagDeleted);
|
||||
|
||||
bottle.serviceFactory('editTag', prop('editTag'), 'tagEditReducerCreator');
|
||||
bottle.serviceFactory('tagEdited', () => tagEdited);
|
||||
};
|
||||
|
||||
export default provideServices;
|
||||
|
||||
Reference in New Issue
Block a user