mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-18 05:23:49 +00:00
Migrated short URL detail reducer to RTK
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
import { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { Dispatch } from 'redux';
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||
import { ShortUrl, ShortUrlIdentifier } from '../data';
|
||||
import { buildReducer } from '../../utils/helpers/redux';
|
||||
import { createAsyncThunk } from '../../utils/helpers/redux';
|
||||
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
||||
import { GetState } from '../../container/types';
|
||||
import { shortUrlMatches } from '../helpers';
|
||||
import { parseApiError } from '../../api/utils';
|
||||
import { ApiErrorAction } from '../../api/types/actions';
|
||||
import { ProblemDetailsError } from '../../api/types/errors';
|
||||
|
||||
export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START';
|
||||
export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR';
|
||||
export const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
|
||||
const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
|
||||
|
||||
export interface ShortUrlDetail {
|
||||
shortUrl?: ShortUrl;
|
||||
@@ -27,25 +22,29 @@ const initialState: ShortUrlDetail = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export default buildReducer<ShortUrlDetail, ShortUrlDetailAction & ApiErrorAction>({
|
||||
[GET_SHORT_URL_DETAIL_START]: () => ({ loading: true, error: false }),
|
||||
[GET_SHORT_URL_DETAIL_ERROR]: (_, { errorData }) => ({ loading: false, error: true, errorData }),
|
||||
[GET_SHORT_URL_DETAIL]: (_, { payload: shortUrl }) => ({ shortUrl, ...initialState }),
|
||||
}, initialState);
|
||||
export const shortUrlDetailReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
|
||||
const getShortUrlDetail = createAsyncThunk(
|
||||
GET_SHORT_URL_DETAIL,
|
||||
async ({ shortCode, domain }: ShortUrlIdentifier, { getState }): Promise<ShortUrl> => {
|
||||
const { shortUrlsList } = getState();
|
||||
const alreadyLoaded = shortUrlsList?.shortUrls?.data.find((url) => shortUrlMatches(url, shortCode, domain));
|
||||
|
||||
export const getShortUrlDetail = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
|
||||
{ shortCode, domain }: ShortUrlIdentifier,
|
||||
) => async (dispatch: Dispatch, getState: GetState) => {
|
||||
dispatch({ type: GET_SHORT_URL_DETAIL_START });
|
||||
return alreadyLoaded ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
const { shortUrlsList } = getState();
|
||||
const payload = shortUrlsList?.shortUrls?.data.find(
|
||||
(url) => shortUrlMatches(url, shortCode, domain),
|
||||
) ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
|
||||
const { reducer } = createSlice({
|
||||
name: 'shortUrlDetailReducer',
|
||||
initialState,
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(getShortUrlDetail.pending, () => ({ loading: true, error: false }));
|
||||
builder.addCase(getShortUrlDetail.rejected, (_, { error }) => (
|
||||
{ loading: false, error: true, errorData: parseApiError(error) }
|
||||
));
|
||||
builder.addCase(getShortUrlDetail.fulfilled, (_, { payload: shortUrl }) => ({ ...initialState, shortUrl }));
|
||||
},
|
||||
});
|
||||
|
||||
dispatch<ShortUrlDetailAction>({ payload, type: GET_SHORT_URL_DETAIL });
|
||||
} catch (e: any) {
|
||||
dispatch<ApiErrorAction>({ type: GET_SHORT_URL_DETAIL_ERROR, errorData: parseApiError(e) });
|
||||
}
|
||||
return { reducer, getShortUrlDetail };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user