2022-11-04 20:56:34 +03:00
|
|
|
import { createSlice, createAsyncThunk, createAction, SliceCaseReducers, AsyncThunk } from '@reduxjs/toolkit';
|
2022-10-12 11:35:16 +03:00
|
|
|
import { ShlinkDomainRedirects } from '../../api/types';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2022-11-03 22:29:10 +03:00
|
|
|
import { ShlinkState } from '../../container/types';
|
2021-12-26 15:38:17 +03:00
|
|
|
import { Domain, DomainStatus } from '../data';
|
|
|
|
import { hasServerData } from '../../servers/data';
|
|
|
|
import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
|
2022-10-12 11:35:16 +03:00
|
|
|
import { ProblemDetailsError } from '../../api/types/errors';
|
2022-11-02 22:40:14 +03:00
|
|
|
import { parseApiError } from '../../api/utils';
|
2022-11-04 20:56:34 +03:00
|
|
|
import { EditDomainRedirects } from './domainRedirects';
|
2020-11-25 23:05:27 +03:00
|
|
|
|
2022-11-04 20:56:34 +03:00
|
|
|
const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
|
|
|
|
const FILTER_DOMAINS = 'shlink/domainsList/FILTER_DOMAINS';
|
|
|
|
const VALIDATE_DOMAIN = 'shlink/domainsList/VALIDATE_DOMAIN';
|
2020-11-25 23:05:27 +03:00
|
|
|
|
|
|
|
export interface DomainsList {
|
2021-12-26 15:38:17 +03:00
|
|
|
domains: Domain[];
|
|
|
|
filteredDomains: Domain[];
|
2021-12-09 15:44:29 +03:00
|
|
|
defaultRedirects?: ShlinkDomainRedirects;
|
2020-11-25 23:05:27 +03:00
|
|
|
loading: boolean;
|
|
|
|
error: boolean;
|
2021-08-21 18:53:06 +03:00
|
|
|
errorData?: ProblemDetailsError;
|
2020-11-25 23:05:27 +03:00
|
|
|
}
|
|
|
|
|
2022-11-03 21:52:57 +03:00
|
|
|
interface ListDomains {
|
2021-12-26 15:38:17 +03:00
|
|
|
domains: Domain[];
|
2021-12-09 15:44:29 +03:00
|
|
|
defaultRedirects?: ShlinkDomainRedirects;
|
2022-11-03 21:52:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ValidateDomain {
|
2021-12-26 15:38:17 +03:00
|
|
|
domain: string;
|
|
|
|
status: DomainStatus;
|
2022-11-03 21:52:57 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 23:05:27 +03:00
|
|
|
const initialState: DomainsList = {
|
|
|
|
domains: [],
|
2021-08-21 18:53:06 +03:00
|
|
|
filteredDomains: [],
|
2020-11-25 23:05:27 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
2022-11-04 20:56:34 +03:00
|
|
|
export const replaceRedirectsOnDomain = ({ domain, redirects }: EditDomainRedirects) =>
|
2022-03-26 14:17:42 +03:00
|
|
|
(d: Domain): Domain => (d.domain !== domain ? d : { ...d, redirects });
|
2021-12-26 15:38:17 +03:00
|
|
|
|
|
|
|
export const replaceStatusOnDomain = (domain: string, status: DomainStatus) =>
|
2022-03-26 14:17:42 +03:00
|
|
|
(d: Domain): Domain => (d.domain !== domain ? d : { ...d, status });
|
2021-08-21 18:53:06 +03:00
|
|
|
|
2022-11-04 20:56:34 +03:00
|
|
|
export const domainsListReducerCreator = (
|
|
|
|
buildShlinkApiClient: ShlinkApiClientBuilder,
|
|
|
|
editDomainRedirects: AsyncThunk<EditDomainRedirects, any, any>,
|
|
|
|
) => {
|
2022-11-03 21:52:57 +03:00
|
|
|
const listDomains = createAsyncThunk<ListDomains, void, { state: ShlinkState }>(
|
2022-11-02 22:40:14 +03:00
|
|
|
LIST_DOMAINS,
|
|
|
|
async (_, { getState }) => {
|
|
|
|
const { listDomains: shlinkListDomains } = buildShlinkApiClient(getState);
|
|
|
|
const { data, defaultRedirects } = await shlinkListDomains();
|
|
|
|
|
|
|
|
return {
|
|
|
|
domains: data.map((domain): Domain => ({ ...domain, status: 'validating' })),
|
|
|
|
defaultRedirects,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-03 21:52:57 +03:00
|
|
|
const checkDomainHealth = createAsyncThunk<ValidateDomain, string, { state: ShlinkState }>(
|
2022-11-02 22:40:14 +03:00
|
|
|
VALIDATE_DOMAIN,
|
|
|
|
async (domain: string, { getState }) => {
|
|
|
|
const { selectedServer } = getState();
|
|
|
|
|
|
|
|
if (!hasServerData(selectedServer)) {
|
|
|
|
return { domain, status: 'invalid' };
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { url, ...rest } = selectedServer;
|
|
|
|
const { health } = buildShlinkApiClient({
|
|
|
|
...rest,
|
|
|
|
url: replaceAuthorityFromUri(url, domain),
|
|
|
|
});
|
|
|
|
|
|
|
|
const { status } = await health();
|
|
|
|
|
|
|
|
return { domain, status: status === 'pass' ? 'valid' : 'invalid' };
|
|
|
|
} catch (e) {
|
|
|
|
return { domain, status: 'invalid' };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-11-04 18:50:03 +03:00
|
|
|
const filterDomains = createAction<string>(FILTER_DOMAINS);
|
|
|
|
|
|
|
|
const { reducer } = createSlice<DomainsList, SliceCaseReducers<DomainsList>>({
|
2022-11-02 22:40:14 +03:00
|
|
|
name: 'domainsList',
|
|
|
|
initialState,
|
2022-11-04 18:50:03 +03:00
|
|
|
reducers: {},
|
2022-11-02 22:40:14 +03:00
|
|
|
extraReducers: (builder) => {
|
|
|
|
builder.addCase(listDomains.pending, () => ({ ...initialState, loading: true }));
|
|
|
|
builder.addCase(listDomains.rejected, (_, { error }) => (
|
2022-11-03 22:51:20 +03:00
|
|
|
{ ...initialState, error: true, errorData: parseApiError(error) }
|
2022-11-02 22:40:14 +03:00
|
|
|
));
|
|
|
|
builder.addCase(listDomains.fulfilled, (_, { payload }) => (
|
|
|
|
{ ...initialState, ...payload, filteredDomains: payload.domains }
|
|
|
|
));
|
|
|
|
|
2022-11-03 22:51:20 +03:00
|
|
|
builder.addCase(checkDomainHealth.fulfilled, ({ domains, filteredDomains, ...rest }, { payload }) => ({
|
|
|
|
...rest,
|
|
|
|
domains: domains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
|
|
|
filteredDomains: filteredDomains.map(replaceStatusOnDomain(payload.domain, payload.status)),
|
|
|
|
}));
|
|
|
|
|
2022-11-04 18:50:03 +03:00
|
|
|
builder.addCase(filterDomains, (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
|
|
|
|
}));
|
|
|
|
|
2022-11-04 20:56:34 +03:00
|
|
|
builder.addCase(editDomainRedirects.fulfilled, (state, { payload }) => ({
|
2022-11-03 22:51:20 +03:00
|
|
|
...state,
|
2022-11-04 20:56:34 +03:00
|
|
|
domains: state.domains.map(replaceRedirectsOnDomain(payload)),
|
|
|
|
filteredDomains: state.filteredDomains.map(replaceRedirectsOnDomain(payload)),
|
2022-11-03 22:51:20 +03:00
|
|
|
}));
|
2022-11-02 22:40:14 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
reducer,
|
|
|
|
listDomains,
|
|
|
|
checkDomainHealth,
|
2022-11-04 18:50:03 +03:00
|
|
|
filterDomains,
|
2022-11-02 22:40:14 +03:00
|
|
|
};
|
|
|
|
};
|