2022-11-02 22:40:14 +03:00
|
|
|
import { createSlice, PayloadAction, createAsyncThunk, SliceCaseReducers } 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';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedirects';
|
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';
|
2020-11-25 23:05:27 +03:00
|
|
|
|
|
|
|
export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
|
2021-12-26 15:38:17 +03:00
|
|
|
export 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
|
|
|
}
|
|
|
|
|
2022-11-03 22:51:20 +03:00
|
|
|
type ListDomainsAction = PayloadAction<ListDomains>;
|
|
|
|
type FilterDomainsAction = PayloadAction<string>;
|
2022-11-03 21:52:57 +03:00
|
|
|
type ValidateDomainAction = PayloadAction<ValidateDomain>;
|
2021-12-26 15:38:17 +03:00
|
|
|
|
2022-11-03 22:51:20 +03:00
|
|
|
export type DomainsCombinedAction = ListDomainsAction
|
|
|
|
& FilterDomainsAction
|
|
|
|
& EditDomainRedirectsAction
|
|
|
|
& ValidateDomainAction;
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2021-08-22 10:00:58 +03:00
|
|
|
export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomainRedirects) =>
|
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-03 22:15:28 +03:00
|
|
|
export const domainsListReducerCreator = (buildShlinkApiClient: ShlinkApiClientBuilder) => {
|
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' };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const { actions, reducer } = createSlice<DomainsList, SliceCaseReducers<DomainsList>>({
|
|
|
|
name: 'domainsList',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
2022-11-03 22:51:20 +03:00
|
|
|
filterDomains: (state, { payload }) => ({
|
|
|
|
...state,
|
|
|
|
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(payload.toLowerCase())),
|
|
|
|
}),
|
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)),
|
|
|
|
}));
|
|
|
|
|
|
|
|
builder.addCase(EDIT_DOMAIN_REDIRECTS, (state, { domain, redirects }: any) => ({ // TODO Fix this "any"
|
|
|
|
...state,
|
|
|
|
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
|
|
|
filteredDomains: state.filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
|
|
|
|
}));
|
2022-11-02 22:40:14 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
reducer,
|
|
|
|
listDomains,
|
|
|
|
checkDomainHealth,
|
|
|
|
...actions,
|
|
|
|
};
|
|
|
|
};
|