Minor refactorings and function extractions

This commit is contained in:
Alejandro Celaya
2022-11-26 09:11:46 +01:00
parent a3f5095dce
commit 165afa436d
6 changed files with 26 additions and 25 deletions

View File

@@ -1,6 +1,5 @@
import { FC } from 'react';
import { isEmpty, pipe } from 'ramda';
import { parseISO } from 'date-fns';
import { Button, InputGroup, Row, UncontrolledTooltip } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTag, faTags } from '@fortawesome/free-solid-svg-icons';
@@ -8,7 +7,7 @@ import classNames from 'classnames';
import { SearchField } from '../utils/SearchField';
import { DateRangeSelector } from '../utils/dates/DateRangeSelector';
import { formatIsoDate } from '../utils/helpers/date';
import { DateRange } from '../utils/dates/types';
import { DateRange, datesToDateRange } from '../utils/dates/types';
import { supportsAllTagsFiltering } from '../utils/helpers/features';
import { SelectedServer } from '../servers/data';
import { OrderDir } from '../utils/helpers/ordering';
@@ -27,8 +26,6 @@ export interface ShortUrlsFilteringProps {
shortUrlsAmount?: number;
}
const dateOrNull = (date?: string) => (date ? parseISO(date) : null);
export const ShortUrlsFilteringBar = (
ExportShortUrlsBtn: FC<ExportShortUrlsBtnProps>,
TagsSelector: FC<TagsSelectorProps>,
@@ -74,10 +71,7 @@ export const ShortUrlsFilteringBar = (
<div className="col-lg-8 col-xl-6 mt-3">
<DateRangeSelector
defaultText="All short URLs"
initialDateRange={{
startDate: dateOrNull(startDate),
endDate: dateOrNull(endDate),
}}
initialDateRange={datesToDateRange(startDate, endDate)}
onDatesChange={setDates}
/>
</div>

View File

@@ -6,8 +6,6 @@ import { ShortUrlsOrder, ShortUrlsOrderableFields } from '../data';
import { orderToString, stringToOrder } from '../../utils/helpers/ordering';
import { TagsFilteringMode } from '../../api/types';
type ToFirstPage = (extra: Partial<ShortUrlsFiltering>) => void;
interface ShortUrlsQueryCommon {
search?: string;
startDate?: string;
@@ -25,14 +23,16 @@ interface ShortUrlsFiltering extends ShortUrlsQueryCommon {
tags: string[];
}
type ToFirstPage = (extra: Partial<ShortUrlsFiltering>) => void;
export const useShortUrlsQuery = (): [ShortUrlsFiltering, ToFirstPage] => {
const navigate = useNavigate();
const location = useLocation();
const params = useParams<{ serverId: string }>();
const { search } = useLocation();
const { serverId = '' } = useParams<{ serverId: string }>();
const query = useMemo(
const filtering = useMemo(
pipe(
() => parseQuery<ShortUrlsQuery>(location.search),
() => parseQuery<ShortUrlsQuery>(search),
({ orderBy, tags, ...rest }: ShortUrlsQuery): ShortUrlsFiltering => {
const parsedOrderBy = orderBy ? stringToOrder<ShortUrlsOrderableFields>(orderBy) : undefined;
const parsedTags = tags?.split(',') ?? [];
@@ -40,20 +40,20 @@ export const useShortUrlsQuery = (): [ShortUrlsFiltering, ToFirstPage] => {
return { ...rest, orderBy: parsedOrderBy, tags: parsedTags };
},
),
[location.search],
[search],
);
const toFirstPageWithExtra = (extra: Partial<ShortUrlsFiltering>) => {
const { orderBy, tags, ...mergedQuery } = { ...query, ...extra };
const normalizedQuery: ShortUrlsQuery = {
...mergedQuery,
const { orderBy, tags, ...mergedFiltering } = { ...filtering, ...extra };
const query: ShortUrlsQuery = {
...mergedFiltering,
orderBy: orderBy && orderToString(orderBy),
tags: tags.length > 0 ? tags.join(',') : undefined,
};
const evolvedQuery = stringifyQuery(normalizedQuery);
const queryString = isEmpty(evolvedQuery) ? '' : `?${evolvedQuery}`;
const stringifiedQuery = stringifyQuery(query);
const queryString = isEmpty(stringifiedQuery) ? '' : `?${stringifiedQuery}`;
navigate(`/server/${params.serverId ?? ''}/list-short-urls/1${queryString}`);
navigate(`/server/${serverId}/list-short-urls/1${queryString}`);
};
return [query, toFirstPageWithExtra];
return [filtering, toFirstPageWithExtra];
};