Skip to content

Commit

Permalink
refactor(filtersUtils): uniformize filter formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlago99 committed Jan 10, 2025
1 parent 2c75c62 commit 5b54d25
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions src/components/designSystem/Filters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ export const FILTER_VALUE_MAP: Partial<Record<AvailableFiltersEnum, Function>> =
[AvailableFiltersEnum.creditNoteCreditStatus]: (value: string) => (value as string).split(','),
}

export const formatFiltersForCreditNotesQuery = (searchParams: URLSearchParams) => {
const formatFiltersForQuery = ({
searchParams,
keyMap,
availableFilters,
}: {
searchParams: URLSearchParams
keyMap?: Record<string, string>
availableFilters: AvailableFiltersEnum[]
}) => {
const filtersSetInUrl = Object.fromEntries(searchParams.entries())

const keyMap: Partial<Record<AvailableFiltersEnum, string>> = {
[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
}

Expand All @@ -95,36 +97,32 @@ export const formatFiltersForCreditNotesQuery = (searchParams: URLSearchParams)

return {
...acc,
[keyMap[key] || key]: value,
[keyMap?.[key] || key]: value,
}
},
{} as Record<string, string | string[] | boolean>,
)
}

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<Record<AvailableFiltersEnum, string>> = {
[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<string, string | string[] | boolean>,
)
export const formatFiltersForInvoiceQuery = (searchParams: URLSearchParams) => {
return formatFiltersForQuery({
searchParams,
availableFilters: InvoiceAvailableFilters,
})
}

export const AMOUNT_INTERVALS_TRANSLATION_MAP = {
Expand Down

0 comments on commit 5b54d25

Please sign in to comment.