Migrated more short-url reducers to TS

This commit is contained in:
Alejandro Celaya
2020-08-26 18:55:40 +02:00
parent 6696fb13d6
commit 1b03d04318
15 changed files with 169 additions and 57 deletions

View File

@@ -4,19 +4,21 @@ import { SelectedServer } from '../servers/data';
import { Settings } from '../settings/reducers/settings';
import { ShortUrlMetaEdition } from '../short-urls/reducers/shortUrlMeta';
import { ShortUrlCreation } from '../short-urls/reducers/shortUrlCreation';
export type ConnectDecorator = (props: string[], actions?: string[]) => any;
import { ShortUrlDeletion } from '../short-urls/reducers/shortUrlDeletion';
import { ShortUrlEdition } from '../short-urls/reducers/shortUrlEdition';
import { ShortUrlsListParams } from '../short-urls/reducers/shortUrlsListParams';
import { ShortUrlTags } from '../short-urls/reducers/shortUrlTags';
export interface ShlinkState {
servers: ServersMap;
selectedServer: SelectedServer;
shortUrlsList: any;
shortUrlsListParams: any;
shortUrlsListParams: ShortUrlsListParams;
shortUrlCreationResult: ShortUrlCreation;
shortUrlDeletion: any;
shortUrlTags: any;
shortUrlDeletion: ShortUrlDeletion;
shortUrlTags: ShortUrlTags;
shortUrlMeta: ShortUrlMetaEdition;
shortUrlEdition: any;
shortUrlEdition: ShortUrlEdition;
shortUrlVisits: any;
tagVisits: any;
shortUrlDetail: any;
@@ -27,4 +29,6 @@ export interface ShlinkState {
settings: Settings;
}
export type ConnectDecorator = (props: string[], actions?: string[]) => any;
export type GetState = () => ShlinkState;

View File

@@ -22,7 +22,7 @@ const DeleteShortUrlModal = ({ shortUrl, toggle, isOpen, shortUrlDeletion, reset
useEffect(() => resetDeleteShortUrl, []);
const { error, errorData } = shortUrlDeletion;
const errorCode = error && (errorData.type || errorData.error);
const errorCode = error && errorData && (errorData.type || errorData.error);
const hasThresholdError = errorCode === THRESHOLD_REACHED;
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED;
const close = pipe(resetDeleteShortUrl, toggle);

View File

@@ -1,6 +1,9 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { apiErrorType } from '../../utils/services/ShlinkApiClient';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { ProblemDetailsError, ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */
export const DELETE_SHORT_URL_START = 'shlink/deleteShortUrl/DELETE_SHORT_URL_START';
@@ -9,6 +12,7 @@ export const SHORT_URL_DELETED = 'shlink/deleteShortUrl/SHORT_URL_DELETED';
export const RESET_DELETE_SHORT_URL = 'shlink/deleteShortUrl/RESET_DELETE_SHORT_URL';
/* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlDeletion interface */
export const shortUrlDeletionType = PropTypes.shape({
shortCode: PropTypes.string.isRequired,
loading: PropTypes.bool.isRequired,
@@ -16,32 +20,50 @@ export const shortUrlDeletionType = PropTypes.shape({
errorData: apiErrorType.isRequired,
});
const initialState = {
export interface ShortUrlDeletion {
shortCode: string;
loading: boolean;
error: boolean;
errorData?: ProblemDetailsError;
}
interface DeleteShortUrlAction extends Action<string> {
shortCode: string;
domain?: string | null;
}
interface DeleteShortUrlErrorAction extends Action<string> {
errorData: ProblemDetailsError;
}
const initialState: ShortUrlDeletion = {
shortCode: '',
loading: false,
error: false,
errorData: {},
};
export default handleActions({
export default buildReducer<ShortUrlDeletion, DeleteShortUrlAction & DeleteShortUrlErrorAction>({
[DELETE_SHORT_URL_START]: (state) => ({ ...state, loading: true, error: false }),
[DELETE_SHORT_URL_ERROR]: (state, { errorData }) => ({ ...state, errorData, loading: false, error: true }),
[SHORT_URL_DELETED]: (state, { shortCode }) => ({ ...state, shortCode, loading: false, error: false }),
[RESET_DELETE_SHORT_URL]: () => initialState,
}, initialState);
export const deleteShortUrl = (buildShlinkApiClient) => (shortCode, domain) => async (dispatch, getState) => {
export const deleteShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain?: string | null,
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: DELETE_SHORT_URL_START });
const { deleteShortUrl } = buildShlinkApiClient(getState);
try {
await deleteShortUrl(shortCode, domain);
dispatch({ type: SHORT_URL_DELETED, shortCode, domain });
dispatch<DeleteShortUrlAction>({ type: SHORT_URL_DELETED, shortCode, domain });
} catch (e) {
dispatch({ type: DELETE_SHORT_URL_ERROR, errorData: e.response.data });
dispatch<DeleteShortUrlErrorAction>({ type: DELETE_SHORT_URL_ERROR, errorData: e.response.data });
throw e;
}
};
export const resetDeleteShortUrl = createAction(RESET_DELETE_SHORT_URL);
export const resetDeleteShortUrl = buildActionCreator(RESET_DELETE_SHORT_URL);

View File

@@ -1,5 +1,8 @@
import { handleActions } from 'redux-actions';
import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { buildReducer } from '../../utils/helpers/redux';
import { ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */
export const EDIT_SHORT_URL_START = 'shlink/shortUrlEdition/EDIT_SHORT_URL_START';
@@ -7,6 +10,7 @@ export const EDIT_SHORT_URL_ERROR = 'shlink/shortUrlEdition/EDIT_SHORT_URL_ERROR
export const SHORT_URL_EDITED = 'shlink/shortUrlEdition/SHORT_URL_EDITED';
/* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlEdition interface instead */
export const ShortUrlEditionType = PropTypes.shape({
shortCode: PropTypes.string,
longUrl: PropTypes.string,
@@ -14,26 +18,43 @@ export const ShortUrlEditionType = PropTypes.shape({
error: PropTypes.bool.isRequired,
});
const initialState = {
export interface ShortUrlEdition {
shortCode: string | null;
longUrl: string | null;
saving: boolean;
error: boolean;
}
export interface ShortUrlEditedAction extends Action<string> {
shortCode: string;
longUrl: string;
domain: string | undefined | null;
}
const initialState: ShortUrlEdition = {
shortCode: null,
longUrl: null,
saving: false,
error: false,
};
export default handleActions({
export default buildReducer<ShortUrlEdition, ShortUrlEditedAction>({
[EDIT_SHORT_URL_START]: (state) => ({ ...state, saving: true, error: false }),
[EDIT_SHORT_URL_ERROR]: (state) => ({ ...state, saving: false, error: true }),
[SHORT_URL_EDITED]: (state, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }),
[SHORT_URL_EDITED]: (_, { shortCode, longUrl }) => ({ shortCode, longUrl, saving: false, error: false }),
}, initialState);
export const editShortUrl = (buildShlinkApiClient) => (shortCode, domain, longUrl) => async (dispatch, getState) => {
export const editShortUrl = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain: string | undefined | null,
longUrl: string,
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: EDIT_SHORT_URL_START });
const { updateShortUrlMeta } = buildShlinkApiClient(getState);
try {
await updateShortUrlMeta(shortCode, domain, { longUrl });
dispatch({ shortCode, longUrl, domain, type: SHORT_URL_EDITED });
dispatch<ShortUrlEditedAction>({ shortCode, longUrl, domain, type: SHORT_URL_EDITED });
} catch (e) {
dispatch({ type: EDIT_SHORT_URL_ERROR });

View File

@@ -1,5 +1,8 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { ShlinkApiClientBuilder } from '../../utils/services/types';
import { GetState } from '../../container/types';
/* eslint-disable padding-line-between-statements */
export const EDIT_SHORT_URL_TAGS_START = 'shlink/shortUrlTags/EDIT_SHORT_URL_TAGS_START';
@@ -8,6 +11,7 @@ export const SHORT_URL_TAGS_EDITED = 'shlink/shortUrlTags/SHORT_URL_TAGS_EDITED'
export const RESET_EDIT_SHORT_URL_TAGS = 'shlink/shortUrlTags/RESET_EDIT_SHORT_URL_TAGS';
/* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlTags interface */
export const shortUrlTagsType = PropTypes.shape({
shortCode: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string).isRequired,
@@ -15,28 +19,45 @@ export const shortUrlTagsType = PropTypes.shape({
error: PropTypes.bool.isRequired,
});
const initialState = {
export interface ShortUrlTags {
shortCode: string | null;
tags: string[];
saving: boolean;
error: boolean;
}
export interface EditShortUrlTagsAction extends Action<string> {
shortCode: string;
tags: string[];
domain: string | null | undefined;
}
const initialState: ShortUrlTags = {
shortCode: null,
tags: [],
saving: false,
error: false,
};
export default handleActions({
export default buildReducer<ShortUrlTags, EditShortUrlTagsAction>({
[EDIT_SHORT_URL_TAGS_START]: (state) => ({ ...state, saving: true, error: false }),
[EDIT_SHORT_URL_TAGS_ERROR]: (state) => ({ ...state, saving: false, error: true }),
[SHORT_URL_TAGS_EDITED]: (state, { shortCode, tags }) => ({ shortCode, tags, saving: false, error: false }),
[SHORT_URL_TAGS_EDITED]: (_, { shortCode, tags }) => ({ shortCode, tags, saving: false, error: false }),
[RESET_EDIT_SHORT_URL_TAGS]: () => initialState,
}, initialState);
export const editShortUrlTags = (buildShlinkApiClient) => (shortCode, domain, tags) => async (dispatch, getState) => {
export const editShortUrlTags = (buildShlinkApiClient: ShlinkApiClientBuilder) => (
shortCode: string,
domain: string | null | undefined,
tags: string[],
) => async (dispatch: Dispatch, getState: GetState) => {
dispatch({ type: EDIT_SHORT_URL_TAGS_START });
const { updateShortUrlTags } = buildShlinkApiClient(getState);
try {
const normalizedTags = await updateShortUrlTags(shortCode, domain, tags);
dispatch({ tags: normalizedTags, shortCode, domain, type: SHORT_URL_TAGS_EDITED });
dispatch<EditShortUrlTagsAction>({ tags: normalizedTags, shortCode, domain, type: SHORT_URL_TAGS_EDITED });
} catch (e) {
dispatch({ type: EDIT_SHORT_URL_TAGS_ERROR });
@@ -44,4 +65,4 @@ export const editShortUrlTags = (buildShlinkApiClient) => (shortCode, domain, ta
}
};
export const resetShortUrlsTags = createAction(RESET_EDIT_SHORT_URL_TAGS);
export const resetShortUrlsTags = buildActionCreator(RESET_EDIT_SHORT_URL_TAGS);

View File

@@ -30,7 +30,6 @@ const initialState = {
error: false,
};
// TODO Make all actions fetch shortCode, domain and prop from payload
const setPropFromActionOnMatchingShortUrl = (prop) => (state, { shortCode, domain, [prop]: propValue }) => assocPath(
[ 'shortUrls', 'data' ],
state.shortUrls.data.map(

View File

@@ -1,9 +1,11 @@
import { createAction, handleActions } from 'redux-actions';
import PropTypes from 'prop-types';
import { Action } from 'redux';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { LIST_SHORT_URLS } from './shortUrlsList';
export const RESET_SHORT_URL_PARAMS = 'shlink/shortUrlsListParams/RESET_SHORT_URL_PARAMS';
/** @deprecated Use ShortUrlsListParams interface instead */
export const shortUrlsListParamsType = PropTypes.shape({
page: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string),
@@ -13,11 +15,24 @@ export const shortUrlsListParamsType = PropTypes.shape({
orderBy: PropTypes.object,
});
export interface ShortUrlsListParams {
page: string;
tags?: string[];
searchTerm?: string;
startDate?: string;
endDate?: string;
orderBy?: object;
}
interface ListShortUrlsAction extends Action<string> {
params: ShortUrlsListParams;
}
const initialState = { page: '1' };
export default handleActions({
export default buildReducer<ShortUrlsListParams, ListShortUrlsAction>({
[LIST_SHORT_URLS]: (state, { params }) => ({ ...state, ...params }),
[RESET_SHORT_URL_PARAMS]: () => initialState,
}, initialState);
export const resetShortUrlParams = createAction(RESET_SHORT_URL_PARAMS);
export const resetShortUrlParams = buildActionCreator(RESET_SHORT_URL_PARAMS);

View File

@@ -14,3 +14,12 @@ export interface ShlinkHealth {
status: 'pass' | 'fail';
version: string;
}
export interface ProblemDetailsError {
type: string;
detail: string;
title: string;
status: number;
error?: string; // Deprecated
message?: string; // Deprecated
}