Migrated all remaining short-url elements to TS

This commit is contained in:
Alejandro Celaya
2020-08-30 19:45:17 +02:00
parent 4b33d39d44
commit 8a9c694fbc
24 changed files with 555 additions and 595 deletions

View File

@@ -1,4 +1,3 @@
import PropTypes from 'prop-types';
import { Action, Dispatch } from 'redux';
import { GetState } from '../../container/types';
import { ShortUrl, ShortUrlData } from '../data';
@@ -12,15 +11,6 @@ export const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL';
export const RESET_CREATE_SHORT_URL = 'shlink/createShortUrl/RESET_CREATE_SHORT_URL';
/* eslint-enable padding-line-between-statements */
/** @deprecated Use ShortUrlCreation interface instead */
export const createShortUrlResultType = PropTypes.shape({
result: PropTypes.shape({
shortUrl: PropTypes.string,
}),
saving: PropTypes.bool,
error: PropTypes.bool,
});
export interface ShortUrlCreation {
result: ShortUrl | null;
saving: boolean;

View File

@@ -7,6 +7,7 @@ import { ShortUrl, ShortUrlIdentifier } from '../data';
import { buildReducer } from '../../utils/helpers/redux';
import { GetState } from '../../container/types';
import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder';
import { ShlinkShortUrlsResponse } from '../../utils/services/types';
import { EditShortUrlTagsAction, SHORT_URL_TAGS_EDITED } from './shortUrlTags';
import { SHORT_URL_DELETED } from './shortUrlDeletion';
import { SHORT_URL_META_EDITED, ShortUrlMetaEditedAction, shortUrlMetaType } from './shortUrlMeta';
@@ -30,18 +31,14 @@ export const shortUrlType = PropTypes.shape({
domain: PropTypes.string,
});
interface ShortUrlsData {
data: ShortUrl[];
}
export interface ShortUrlsList {
shortUrls: ShortUrlsData;
shortUrls?: ShlinkShortUrlsResponse;
loading: boolean;
error: boolean;
}
export interface ListShortUrlsAction extends Action<string> {
shortUrls: ShortUrlsData;
shortUrls: ShlinkShortUrlsResponse;
params: ShortUrlsListParams;
}
@@ -50,9 +47,6 @@ export type ListShortUrlsCombinedAction = (
);
const initialState: ShortUrlsList = {
shortUrls: {
data: [],
},
loading: true,
error: false,
};
@@ -60,7 +54,7 @@ const initialState: ShortUrlsList = {
const setPropFromActionOnMatchingShortUrl = <T extends ShortUrlIdentifier>(prop: keyof T) => (
state: ShortUrlsList,
{ shortCode, domain, [prop]: propValue }: T,
): ShortUrlsList => assocPath(
): ShortUrlsList => !state.shortUrls ? state : assocPath(
[ 'shortUrls', 'data' ],
state.shortUrls.data.map(
(shortUrl: ShortUrl) =>
@@ -71,9 +65,9 @@ const setPropFromActionOnMatchingShortUrl = <T extends ShortUrlIdentifier>(prop:
export default buildReducer<ShortUrlsList, ListShortUrlsCombinedAction>({
[LIST_SHORT_URLS_START]: (state) => ({ ...state, loading: true, error: false }),
[LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true, shortUrls: { data: [] } }),
[LIST_SHORT_URLS_ERROR]: () => ({ loading: false, error: true }),
[LIST_SHORT_URLS]: (_, { shortUrls }) => ({ loading: false, error: false, shortUrls }),
[SHORT_URL_DELETED]: (state, { shortCode, domain }) => assocPath(
[SHORT_URL_DELETED]: (state, { shortCode, domain }) => !state.shortUrls ? state : assocPath(
[ 'shortUrls', 'data' ],
reject((shortUrl) => shortUrlMatches(shortUrl, shortCode, domain), state.shortUrls.data),
state,

View File

@@ -1,26 +1,16 @@
import PropTypes from 'prop-types';
import { buildActionCreator, buildReducer } from '../../utils/helpers/redux';
import { OrderDir } from '../../utils/utils';
import { LIST_SHORT_URLS, ListShortUrlsAction } 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),
searchTerm: PropTypes.string,
startDate: PropTypes.string,
endDate: PropTypes.string,
orderBy: PropTypes.object,
});
export interface ShortUrlsListParams {
page?: string;
tags?: string[];
searchTerm?: string;
startDate?: string;
endDate?: string;
orderBy?: string | Record<string, 'ASC' | 'DESC'>;
orderBy?: Record<string, OrderDir>;
}
const initialState: ShortUrlsListParams = { page: '1' };