shlink-web-client/src/short-urls/reducers/shortUrlDetail.ts

52 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { ShortUrl, ShortUrlIdentifier } from '../data';
import { createAsyncThunk } from '../../utils/helpers/redux';
2023-02-18 12:40:37 +03:00
import type { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
import { shortUrlMatches } from '../helpers';
2021-03-20 18:32:12 +03:00
import { parseApiError } from '../../api/utils';
2023-02-18 12:40:37 +03:00
import type { ProblemDetailsError } from '../../api/types/errors';
const REDUCER_PREFIX = 'shlink/shortUrlDetail';
export interface ShortUrlDetail {
shortUrl?: ShortUrl;
loading: boolean;
error: boolean;
2021-03-20 18:32:12 +03:00
errorData?: ProblemDetailsError;
}
export type ShortUrlDetailAction = PayloadAction<ShortUrl>;
const initialState: ShortUrlDetail = {
loading: false,
error: false,
};
export const shortUrlDetailReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
const getShortUrlDetail = createAsyncThunk(
`${REDUCER_PREFIX}/getShortUrlDetail`,
async ({ shortCode, domain }: ShortUrlIdentifier, { getState }): Promise<ShortUrl> => {
const { shortUrlsList } = getState();
const alreadyLoaded = shortUrlsList?.shortUrls?.data.find((url) => shortUrlMatches(url, shortCode, domain));
return alreadyLoaded ?? await buildShlinkApiClient(getState).getShortUrl(shortCode, domain);
},
);
const { reducer } = createSlice({
name: REDUCER_PREFIX,
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 }));
},
});
return { reducer, getShortUrlDetail };
};