mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-02 13:51:48 +00:00
Migrated tagDelete reducer to RTK
This commit is contained in:
@@ -9,7 +9,6 @@ import domainVisitsReducer from '../visits/reducers/domainVisits';
|
||||
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 { settingsReducer } from '../settings/reducers/settings';
|
||||
import visitsOverviewReducer from '../visits/reducers/visitsOverview';
|
||||
import { appUpdatesReducer } from '../app/reducers/appUpdates';
|
||||
@@ -30,7 +29,7 @@ export default (container: IContainer) => combineReducers<ShlinkState>({
|
||||
orphanVisits: orphanVisitsReducer,
|
||||
nonOrphanVisits: nonOrphanVisitsReducer,
|
||||
tagsList: tagsListReducer,
|
||||
tagDelete: tagDeleteReducer,
|
||||
tagDelete: container.tagDeleteReducer,
|
||||
tagEdit: container.tagEditReducer,
|
||||
mercureInfo: container.mercureInfoReducer,
|
||||
settings: settingsReducer,
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
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 { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||
import { parseApiError } from '../../api/utils';
|
||||
import { ApiErrorAction } from '../../api/types/actions';
|
||||
import { ProblemDetailsError } from '../../api/types/errors';
|
||||
|
||||
export const DELETE_TAG_START = 'shlink/deleteTag/DELETE_TAG_START';
|
||||
export const DELETE_TAG_ERROR = 'shlink/deleteTag/DELETE_TAG_ERROR';
|
||||
export const DELETE_TAG = 'shlink/deleteTag/DELETE_TAG';
|
||||
export const TAG_DELETED = 'shlink/deleteTag/TAG_DELETED';
|
||||
const DELETE_TAG = 'shlink/deleteTag/DELETE_TAG';
|
||||
const TAG_DELETED = 'shlink/deleteTag/TAG_DELETED';
|
||||
|
||||
export interface TagDeletion {
|
||||
deleting: boolean;
|
||||
@@ -27,27 +22,27 @@ const initialState: TagDeletion = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export default buildReducer<TagDeletion, ApiErrorAction>({
|
||||
[DELETE_TAG_START]: () => ({ deleting: true, deleted: false, error: false }),
|
||||
[DELETE_TAG_ERROR]: (_, { errorData }) => ({ deleting: false, deleted: false, error: true, errorData }),
|
||||
[DELETE_TAG]: () => ({ deleting: false, deleted: true, error: false }),
|
||||
}, initialState);
|
||||
|
||||
export const deleteTag = (buildShlinkApiClient: ShlinkApiClientBuilder) => (tag: string) => async (
|
||||
dispatch: Dispatch,
|
||||
getState: GetState,
|
||||
) => {
|
||||
dispatch({ type: DELETE_TAG_START });
|
||||
const { deleteTags } = buildShlinkApiClient(getState);
|
||||
|
||||
try {
|
||||
await deleteTags([tag]);
|
||||
dispatch({ type: DELETE_TAG });
|
||||
} catch (e: any) {
|
||||
dispatch<ApiErrorAction>({ type: DELETE_TAG_ERROR, errorData: parseApiError(e) });
|
||||
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
export const tagDeleted = createAction<string>(TAG_DELETED);
|
||||
|
||||
export const tagDeleteReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
|
||||
const deleteTag = createAsyncThunk(DELETE_TAG, async (tag: string, { getState }): Promise<void> => {
|
||||
const { deleteTags } = buildShlinkApiClient(getState);
|
||||
await deleteTags([tag]);
|
||||
});
|
||||
|
||||
const { reducer } = createSlice({
|
||||
name: 'tagDeleteReducer',
|
||||
initialState,
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(deleteTag.pending, () => ({ deleting: true, deleted: false, error: false }));
|
||||
builder.addCase(
|
||||
deleteTag.rejected,
|
||||
(_, { error }) => ({ deleting: false, deleted: false, error: true, errorData: parseApiError(error) }),
|
||||
);
|
||||
builder.addCase(deleteTag.fulfilled, () => ({ deleting: false, deleted: true, error: false }));
|
||||
},
|
||||
});
|
||||
|
||||
return { reducer, deleteTag };
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ import { parseApiError } from '../../api/utils';
|
||||
import { TagStats } from '../data';
|
||||
import { ApiErrorAction } from '../../api/types/actions';
|
||||
import { CREATE_SHORT_URL, CreateShortUrlAction } from '../../short-urls/reducers/shortUrlCreation';
|
||||
import { DeleteTagAction, TAG_DELETED } from './tagDelete';
|
||||
import { DeleteTagAction, tagDeleted } from './tagDelete';
|
||||
import { EditTagAction, tagEdited } from './tagEdit';
|
||||
import { ProblemDetailsError } from '../../api/types/errors';
|
||||
|
||||
@@ -85,7 +85,7 @@ export default buildReducer<TagsList, TagsCombinedAction>({
|
||||
[LIST_TAGS_START]: () => ({ ...initialState, loading: true }),
|
||||
[LIST_TAGS_ERROR]: (_, { errorData }) => ({ ...initialState, error: true, errorData }),
|
||||
[LIST_TAGS]: (_, { tags, stats }) => ({ ...initialState, stats, tags, filteredTags: tags }),
|
||||
[TAG_DELETED]: (state, { payload: tag }) => ({
|
||||
[tagDeleted.toString()]: (state, { payload: tag }) => ({
|
||||
...state,
|
||||
tags: rejectTag(state.tags, tag),
|
||||
filteredTags: rejectTag(state.filteredTags, tag),
|
||||
|
||||
@@ -6,7 +6,7 @@ import { DeleteTagConfirmModal } from '../helpers/DeleteTagConfirmModal';
|
||||
import { EditTagModal } from '../helpers/EditTagModal';
|
||||
import { TagsList } from '../TagsList';
|
||||
import { filterTags, listTags } from '../reducers/tagsList';
|
||||
import { deleteTag, tagDeleted } from '../reducers/tagDelete';
|
||||
import { tagDeleted, tagDeleteReducerCreator } from '../reducers/tagDelete';
|
||||
import { tagEdited, tagEditReducerCreator } from '../reducers/tagEdit';
|
||||
import { ConnectDecorator } from '../../container/types';
|
||||
import { TagsCards } from '../TagsCards';
|
||||
@@ -41,6 +41,9 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
bottle.serviceFactory('tagEditReducerCreator', tagEditReducerCreator, 'buildShlinkApiClient', 'ColorGenerator');
|
||||
bottle.serviceFactory('tagEditReducer', prop('reducer'), 'tagEditReducerCreator');
|
||||
|
||||
bottle.serviceFactory('tagDeleteReducerCreator', tagDeleteReducerCreator, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('tagDeleteReducer', prop('reducer'), 'tagDeleteReducerCreator');
|
||||
|
||||
// Actions
|
||||
const listTagsActionFactory = (force: boolean) =>
|
||||
({ buildShlinkApiClient }: IContainer) => listTags(buildShlinkApiClient, force);
|
||||
@@ -49,7 +52,7 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
|
||||
bottle.factory('forceListTags', listTagsActionFactory(true));
|
||||
bottle.serviceFactory('filterTags', () => filterTags);
|
||||
|
||||
bottle.serviceFactory('deleteTag', deleteTag, 'buildShlinkApiClient');
|
||||
bottle.serviceFactory('deleteTag', prop('deleteTag'), 'tagDeleteReducerCreator');
|
||||
bottle.serviceFactory('tagDeleted', () => tagDeleted);
|
||||
|
||||
bottle.serviceFactory('editTag', prop('editTag'), 'tagEditReducerCreator');
|
||||
|
||||
Reference in New Issue
Block a user