From 5b54d25dfff31065f265ca5d826b25c916ad5f97 Mon Sep 17 00:00:00 2001 From: Stefan Moraru Date: Thu, 9 Jan 2025 11:11:08 +0200 Subject: [PATCH] refactor(filtersUtils): uniformize filter formatting --- src/components/designSystem/Filters/utils.ts | 58 ++++++++++---------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/components/designSystem/Filters/utils.ts b/src/components/designSystem/Filters/utils.ts index e65ae0e50..238b5b871 100644 --- a/src/components/designSystem/Filters/utils.ts +++ b/src/components/designSystem/Filters/utils.ts @@ -64,21 +64,23 @@ export const FILTER_VALUE_MAP: Partial> = [AvailableFiltersEnum.creditNoteCreditStatus]: (value: string) => (value as string).split(','), } -export const formatFiltersForCreditNotesQuery = (searchParams: URLSearchParams) => { +const formatFiltersForQuery = ({ + searchParams, + keyMap, + availableFilters, +}: { + searchParams: URLSearchParams + keyMap?: Record + availableFilters: AvailableFiltersEnum[] +}) => { const filtersSetInUrl = Object.fromEntries(searchParams.entries()) - const keyMap: Partial> = { - [AvailableFiltersEnum.creditNoteReason]: 'reason', - [AvailableFiltersEnum.creditNoteCreditStatus]: 'creditStatus', - [AvailableFiltersEnum.creditNoteRefundStatus]: 'refundStatus', - } - return Object.entries(filtersSetInUrl).reduce( (acc, cur) => { const current = cur as [AvailableFiltersEnum, string | string[] | boolean] const key = current[0] - if (!CreditNoteAvailableFilters.includes(key)) { + if (!availableFilters.includes(key)) { return acc } @@ -95,36 +97,32 @@ export const formatFiltersForCreditNotesQuery = (searchParams: URLSearchParams) return { ...acc, - [keyMap[key] || key]: value, + [keyMap?.[key] || key]: value, } }, {} as Record, ) } -export const formatFiltersForInvoiceQuery = (searchParams: URLSearchParams) => { - const filtersSetInUrl = Object.fromEntries(searchParams.entries()) - - return Object.entries(filtersSetInUrl).reduce( - (acc, cur) => { - const current = cur as [AvailableFiltersEnum, string | string[] | boolean] - const key = current[0] - - if (!InvoiceAvailableFilters.includes(key)) { - return acc - } - - const filterFunction = FILTER_VALUE_MAP[key] +export const formatFiltersForCreditNotesQuery = (searchParams: URLSearchParams) => { + const keyMap: Partial> = { + [AvailableFiltersEnum.creditNoteReason]: 'reason', + [AvailableFiltersEnum.creditNoteCreditStatus]: 'creditStatus', + [AvailableFiltersEnum.creditNoteRefundStatus]: 'refundStatus', + } - const value = filterFunction ? filterFunction(current[1]) : current[1] + return formatFiltersForQuery({ + searchParams, + keyMap, + availableFilters: CreditNoteAvailableFilters, + }) +} - return { - ...acc, - [key]: value, - } - }, - {} as Record, - ) +export const formatFiltersForInvoiceQuery = (searchParams: URLSearchParams) => { + return formatFiltersForQuery({ + searchParams, + availableFilters: InvoiceAvailableFilters, + }) } export const AMOUNT_INTERVALS_TRANSLATION_MAP = {