From e632c5b04f513a49d223917944de398e46d81b57 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 13 Mar 2022 11:14:30 +0100 Subject: [PATCH] Abstracted logic to parse tags from string to array and back for the query --- src/short-urls/ShortUrlsFilteringBar.tsx | 12 +++++------- src/short-urls/ShortUrlsList.tsx | 11 +++++------ src/short-urls/helpers/hooks.ts | 19 +++++++++++++------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/short-urls/ShortUrlsFilteringBar.tsx b/src/short-urls/ShortUrlsFilteringBar.tsx index 915ce4e7..610203cb 100644 --- a/src/short-urls/ShortUrlsFilteringBar.tsx +++ b/src/short-urls/ShortUrlsFilteringBar.tsx @@ -34,7 +34,6 @@ const ShortUrlsFilteringBar = (colorGenerator: ColorGenerator): FC { const [{ search, tags, startDate, endDate, tagsMode = 'any' }, toFirstPage ] = useShortUrlsQuery(); - const selectedTags = tags?.split(',') ?? []; const setDates = pipe( ({ startDate, endDate }: DateRange) => ({ startDate: formatIsoDate(startDate) ?? undefined, @@ -47,9 +46,8 @@ const ShortUrlsFilteringBar = (colorGenerator: ColorGenerator): FC toFirstPage({ search }), ); const removeTag = pipe( - (tag: string) => selectedTags.filter((selectedTag) => selectedTag !== tag), - (tagsList) => tagsList.length === 0 ? undefined : tagsList.join(','), - (tags) => toFirstPage({ tags }), + (tag: string) => tags.filter((selectedTag) => selectedTag !== tag), + (updateTags) => toFirstPage({ tags: updateTags }), ); const canChangeTagsMode = supportsAllTagsFiltering(selectedServer); const toggleTagsMode = pipe( @@ -80,9 +78,9 @@ const ShortUrlsFilteringBar = (colorGenerator: ColorGenerator): FC - {selectedTags.length > 0 && ( + {tags.length > 0 && (

- {canChangeTagsMode && selectedTags.length > 1 && ( + {canChangeTagsMode && tags.length > 1 && (
)} - {selectedTags.map((tag) => + {tags.map((tag) => removeTag(tag)} />)}

)} diff --git a/src/short-urls/ShortUrlsList.tsx b/src/short-urls/ShortUrlsList.tsx index 1eddadc6..fb1c2115 100644 --- a/src/short-urls/ShortUrlsList.tsx +++ b/src/short-urls/ShortUrlsList.tsx @@ -1,5 +1,5 @@ import { pipe } from 'ramda'; -import { FC, useEffect, useMemo, useState } from 'react'; +import { FC, useEffect, useState } from 'react'; import { Card } from 'reactstrap'; import { useLocation, useParams } from 'react-router-dom'; import { determineOrderDir, OrderDir } from '../utils/helpers/ordering'; @@ -35,7 +35,6 @@ const ShortUrlsList = ( // This separated state handling is needed to be able to fall back to settings value, but only once when loaded orderBy ?? settings.shortUrlsList?.defaultOrdering ?? DEFAULT_SHORT_URLS_ORDERING, ); - const selectedTags = useMemo(() => tags?.split(',') ?? [], [ tags ]); const { pagination } = shortUrlsList?.shortUrls ?? {}; const handleOrderBy = (field?: ShortUrlsOrderableFields, dir?: OrderDir) => { toFirstPage({ orderBy: { field, dir } }); @@ -46,21 +45,21 @@ const ShortUrlsList = ( const renderOrderIcon = (field: ShortUrlsOrderableFields) => ; const addTag = pipe( - (newTag: string) => [ ...new Set([ ...selectedTags, newTag ]) ].join(','), - (tags) => toFirstPage({ tags }), + (newTag: string) => [ ...new Set([ ...tags, newTag ]) ], + (updatedTags) => toFirstPage({ tags: updatedTags }), ); useEffect(() => { listShortUrls({ page, searchTerm: search, - tags: selectedTags, + tags, startDate, endDate, orderBy: actualOrderBy, tagsMode, }); - }, [ page, search, selectedTags, startDate, endDate, actualOrderBy, tagsMode ]); + }, [ page, search, tags, startDate, endDate, actualOrderBy, tagsMode ]); return ( <> diff --git a/src/short-urls/helpers/hooks.ts b/src/short-urls/helpers/hooks.ts index 9e55e026..7505db47 100644 --- a/src/short-urls/helpers/hooks.ts +++ b/src/short-urls/helpers/hooks.ts @@ -14,7 +14,6 @@ export interface ShortUrlListRouteParams { } interface ShortUrlsQueryCommon { - tags?: string; search?: string; startDate?: string; endDate?: string; @@ -23,10 +22,12 @@ interface ShortUrlsQueryCommon { interface ShortUrlsQuery extends ShortUrlsQueryCommon { orderBy?: string; + tags?: string; } interface ShortUrlsFiltering extends ShortUrlsQueryCommon { orderBy?: ShortUrlsOrder; + tags: string[]; } export const useShortUrlsQuery = (): [ShortUrlsFiltering, ToFirstPage] => { @@ -37,16 +38,22 @@ export const useShortUrlsQuery = (): [ShortUrlsFiltering, ToFirstPage] => { const query = useMemo( pipe( () => parseQuery(location.search), - ({ orderBy, ...rest }: ShortUrlsQuery): ShortUrlsFiltering => !orderBy ? rest : { - ...rest, - orderBy: stringToOrder(orderBy), + ({ orderBy, tags, ...rest }: ShortUrlsQuery): ShortUrlsFiltering => { + const parsedOrderBy = orderBy ? stringToOrder(orderBy) : undefined; + const parsedTags = tags?.split(',') ?? []; + + return { ...rest, orderBy: parsedOrderBy, tags: parsedTags }; }, ), [ location.search ], ); const toFirstPageWithExtra = (extra: Partial) => { - const { orderBy, ...mergedQuery } = { ...query, ...extra }; - const normalizedQuery: ShortUrlsQuery = { ...mergedQuery, orderBy: orderBy && orderToString(orderBy) }; + const { orderBy, tags, ...mergedQuery } = { ...query, ...extra }; + const normalizedQuery: ShortUrlsQuery = { + ...mergedQuery, + orderBy: orderBy && orderToString(orderBy), + tags: tags.length > 0 ? tags.join(',') : undefined, + }; const evolvedQuery = stringifyQuery(normalizedQuery); const queryString = isEmpty(evolvedQuery) ? '' : `?${evolvedQuery}`;