2020-11-25 23:05:27 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { ProblemDetailsError, ShlinkDomain, ShlinkDomainRedirects } from '../../api/types';
|
2020-11-25 23:05:27 +03:00
|
|
|
import { buildReducer } from '../../utils/helpers/redux';
|
2020-12-22 11:55:39 +03:00
|
|
|
import { ShlinkApiClientBuilder } from '../../api/services/ShlinkApiClientBuilder';
|
2020-11-25 23:05:27 +03:00
|
|
|
import { GetState } from '../../container/types';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { parseApiError } from '../../api/utils';
|
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
|
|
|
import { EDIT_DOMAIN_REDIRECTS, EditDomainRedirectsAction } from './domainRedirects';
|
2020-11-25 23:05:27 +03:00
|
|
|
|
|
|
|
/* eslint-disable padding-line-between-statements */
|
|
|
|
export const LIST_DOMAINS_START = 'shlink/domainsList/LIST_DOMAINS_START';
|
|
|
|
export const LIST_DOMAINS_ERROR = 'shlink/domainsList/LIST_DOMAINS_ERROR';
|
|
|
|
export const LIST_DOMAINS = 'shlink/domainsList/LIST_DOMAINS';
|
2021-08-21 18:53:06 +03:00
|
|
|
export const FILTER_DOMAINS = 'shlink/domainsList/FILTER_DOMAINS';
|
2020-11-25 23:05:27 +03:00
|
|
|
/* eslint-enable padding-line-between-statements */
|
|
|
|
|
|
|
|
export interface DomainsList {
|
|
|
|
domains: ShlinkDomain[];
|
2021-08-21 18:53:06 +03:00
|
|
|
filteredDomains: ShlinkDomain[];
|
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
|
|
|
}
|
|
|
|
|
2020-11-28 14:22:52 +03:00
|
|
|
export interface ListDomainsAction extends Action<string> {
|
2020-11-25 23:05:27 +03:00
|
|
|
domains: ShlinkDomain[];
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:53:06 +03:00
|
|
|
interface FilterDomainsAction extends Action<string> {
|
|
|
|
searchTerm: string;
|
|
|
|
}
|
|
|
|
|
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 type DomainsCombinedAction = ListDomainsAction
|
2021-08-21 18:53:06 +03:00
|
|
|
& ApiErrorAction
|
|
|
|
& FilterDomainsAction
|
|
|
|
& EditDomainRedirectsAction;
|
|
|
|
|
2021-08-22 10:00:58 +03:00
|
|
|
export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomainRedirects) =>
|
2021-08-21 18:53:06 +03:00
|
|
|
(d: ShlinkDomain): ShlinkDomain => d.domain !== domain ? d : { ...d, redirects };
|
|
|
|
|
|
|
|
export default buildReducer<DomainsList, DomainsCombinedAction>({
|
2020-11-25 23:05:27 +03:00
|
|
|
[LIST_DOMAINS_START]: () => ({ ...initialState, loading: true }),
|
2021-08-21 18:53:06 +03:00
|
|
|
[LIST_DOMAINS_ERROR]: ({ errorData }) => ({ ...initialState, error: true, errorData }),
|
|
|
|
[LIST_DOMAINS]: (_, { domains }) => ({ ...initialState, domains, filteredDomains: domains }),
|
|
|
|
[FILTER_DOMAINS]: (state, { searchTerm }) => ({
|
|
|
|
...state,
|
|
|
|
filteredDomains: state.domains.filter(({ domain }) => domain.toLowerCase().match(searchTerm)),
|
|
|
|
}),
|
|
|
|
[EDIT_DOMAIN_REDIRECTS]: (state, { domain, redirects }) => ({
|
|
|
|
...state,
|
|
|
|
domains: state.domains.map(replaceRedirectsOnDomain(domain, redirects)),
|
|
|
|
filteredDomains: state.filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
|
|
|
|
}),
|
2020-11-25 23:05:27 +03:00
|
|
|
}, initialState);
|
|
|
|
|
|
|
|
export const listDomains = (buildShlinkApiClient: ShlinkApiClientBuilder) => () => async (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState,
|
|
|
|
) => {
|
|
|
|
dispatch({ type: LIST_DOMAINS_START });
|
|
|
|
const { listDomains } = buildShlinkApiClient(getState);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const domains = await listDomains();
|
|
|
|
|
|
|
|
dispatch<ListDomainsAction>({ type: LIST_DOMAINS, domains });
|
|
|
|
} catch (e) {
|
2021-08-21 18:53:06 +03:00
|
|
|
dispatch<ApiErrorAction>({ type: LIST_DOMAINS_ERROR, errorData: parseApiError(e) });
|
2020-11-25 23:05:27 +03:00
|
|
|
}
|
|
|
|
};
|
2021-08-21 18:53:06 +03:00
|
|
|
|
|
|
|
export const filterDomains = (searchTerm: string): FilterDomainsAction => ({ type: FILTER_DOMAINS, searchTerm });
|