mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-08-02 09:01:52 +00:00
Use apiClient factory to dynamically resolved different values at runtime
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import { createAsyncThunk } from '../../../src/utils/helpers/redux';
|
||||
import type { ProblemDetailsError, ShlinkApiClient } from '../../api-contract';
|
||||
import { parseApiError } from '../../api-contract/utils';
|
||||
import { createAsyncThunk } from '../../utils/redux';
|
||||
import type { ShortUrl, ShortUrlData } from '../data';
|
||||
|
||||
const REDUCER_PREFIX = 'shlink/shortUrlCreation';
|
||||
@@ -35,9 +35,9 @@ const initialState: ShortUrlCreation = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export const createShortUrl = (apiClient: ShlinkApiClient) => createAsyncThunk(
|
||||
export const createShortUrl = (apiClientFactory: () => ShlinkApiClient) => createAsyncThunk(
|
||||
`${REDUCER_PREFIX}/createShortUrl`,
|
||||
(data: ShortUrlData): Promise<ShortUrl> => apiClient.createShortUrl(data),
|
||||
(data: ShortUrlData): Promise<ShortUrl> => apiClientFactory().createShortUrl(data),
|
||||
);
|
||||
|
||||
export const shortUrlCreationReducerCreator = (createShortUrlThunk: ReturnType<typeof createShortUrl>) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createAction, createSlice } from '@reduxjs/toolkit';
|
||||
import { createAsyncThunk } from '../../../src/utils/helpers/redux';
|
||||
import type { ProblemDetailsError, ShlinkApiClient } from '../../api-contract';
|
||||
import { parseApiError } from '../../api-contract/utils';
|
||||
import { createAsyncThunk } from '../../utils/redux';
|
||||
import type { ShortUrl, ShortUrlIdentifier } from '../data';
|
||||
|
||||
const REDUCER_PREFIX = 'shlink/shortUrlDeletion';
|
||||
@@ -21,10 +21,10 @@ const initialState: ShortUrlDeletion = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export const deleteShortUrl = (apiClient: ShlinkApiClient) => createAsyncThunk(
|
||||
export const deleteShortUrl = (apiClientFactory: () => ShlinkApiClient) => createAsyncThunk(
|
||||
`${REDUCER_PREFIX}/deleteShortUrl`,
|
||||
async ({ shortCode, domain }: ShortUrlIdentifier): Promise<ShortUrlIdentifier> => {
|
||||
await apiClient.deleteShortUrl(shortCode, domain);
|
||||
await apiClientFactory().deleteShortUrl(shortCode, domain);
|
||||
return { shortCode, domain };
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import { createAsyncThunk } from '../../../src/utils/helpers/redux';
|
||||
import type { ProblemDetailsError, ShlinkApiClient } from '../../api-contract';
|
||||
import { parseApiError } from '../../api-contract/utils';
|
||||
import { createAsyncThunk } from '../../utils/redux';
|
||||
import type { ShortUrl, ShortUrlIdentifier } from '../data';
|
||||
import { shortUrlMatches } from '../helpers';
|
||||
|
||||
@@ -22,14 +22,14 @@ const initialState: ShortUrlDetail = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export const shortUrlDetailReducerCreator = (apiClient: ShlinkApiClient) => {
|
||||
export const shortUrlDetailReducerCreator = (apiClientFactory: () => ShlinkApiClient) => {
|
||||
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 apiClient.getShortUrl(shortCode, domain);
|
||||
return alreadyLoaded ?? await apiClientFactory().getShortUrl(shortCode, domain);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import { createAsyncThunk } from '../../../src/utils/helpers/redux';
|
||||
import type { ProblemDetailsError, ShlinkApiClient } from '../../api-contract';
|
||||
import { parseApiError } from '../../api-contract/utils';
|
||||
import { createAsyncThunk } from '../../utils/redux';
|
||||
import type { EditShortUrlData, ShortUrl, ShortUrlIdentifier } from '../data';
|
||||
|
||||
const REDUCER_PREFIX = 'shlink/shortUrlEdition';
|
||||
@@ -27,10 +27,10 @@ const initialState: ShortUrlEdition = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export const editShortUrl = (apiClient: ShlinkApiClient) => createAsyncThunk(
|
||||
export const editShortUrl = (apiClientFactory: () => ShlinkApiClient) => createAsyncThunk(
|
||||
`${REDUCER_PREFIX}/editShortUrl`,
|
||||
({ shortCode, domain, data }: EditShortUrl): Promise<ShortUrl> =>
|
||||
apiClient.updateShortUrl(shortCode, domain, data as any) // TODO parse dates
|
||||
apiClientFactory().updateShortUrl(shortCode, domain, data as any) // TODO parse dates
|
||||
,
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import { assocPath, last, pipe, reject } from 'ramda';
|
||||
import { createAsyncThunk } from '../../../src/utils/helpers/redux';
|
||||
import type { ShlinkApiClient, ShlinkShortUrlsListParams, ShlinkShortUrlsResponse } from '../../api-contract';
|
||||
import { createAsyncThunk } from '../../utils/redux';
|
||||
import { createNewVisits } from '../../visits/reducers/visitCreation';
|
||||
import type { ShortUrl } from '../data';
|
||||
import { shortUrlMatches } from '../helpers';
|
||||
@@ -23,9 +23,11 @@ const initialState: ShortUrlsList = {
|
||||
error: false,
|
||||
};
|
||||
|
||||
export const listShortUrls = (apiClient: ShlinkApiClient) => createAsyncThunk(
|
||||
export const listShortUrls = (apiClientFactory: () => ShlinkApiClient) => createAsyncThunk(
|
||||
`${REDUCER_PREFIX}/listShortUrls`,
|
||||
(params: ShlinkShortUrlsListParams | void): Promise<ShlinkShortUrlsResponse> => apiClient.listShortUrls(params ?? {}),
|
||||
(params: ShlinkShortUrlsListParams | void): Promise<ShlinkShortUrlsResponse> => apiClientFactory().listShortUrls(
|
||||
params ?? {},
|
||||
),
|
||||
);
|
||||
|
||||
export const shortUrlsListReducerCreator = (
|
||||
|
||||
Reference in New Issue
Block a user