diff --git a/src/components/designSystem/Filters/InvoiceStatusQuickFilter.tsx b/src/components/designSystem/Filters/InvoiceStatusQuickFilter.tsx index e988ef9cb..8d5b5fa32 100644 --- a/src/components/designSystem/Filters/InvoiceStatusQuickFilter.tsx +++ b/src/components/designSystem/Filters/InvoiceStatusQuickFilter.tsx @@ -1,24 +1,11 @@ -import { useNavigate, useSearchParams } from 'react-router-dom' +import { useNavigate } from 'react-router-dom' import { Button, Typography } from '~/components/designSystem' +import { InvoicePaymentStatusTypeEnum, InvoiceStatusTypeEnum } from '~/generated/graphql' import { useInternationalization } from '~/hooks/core/useInternationalization' import { tw } from '~/styles/utils' import { useFilters } from './hook' -import { - buildDraftUrlParams, - buildOutstandingUrlParams, - buildPaymentDisputeLostUrlParams, - buildPaymentOverdueUrlParams, - buildSucceededUrlParams, - buildVoidedUrlParams, - isDraftUrlParams, - isOutstandingUrlParams, - isPaymentDisputeLostUrlParams, - isPaymentOverdueUrlParams, - isSucceededUrlParams, - isVoidedUrlParams, -} from './utils' const QuickFilter = ({ children, @@ -41,11 +28,57 @@ const QuickFilter = ({ ) +enum InvoiceQuickFilterEnum { + Outstanding = 'outstanding', + Succeeded = 'succeeded', + Draft = 'draft', + PaymentOverdue = 'paymentOverdue', + Voided = 'voided', + PaymentDisputeLost = 'paymentDisputeLost', +} + +const invoiceQuickFilterTranslations = { + [InvoiceQuickFilterEnum.Outstanding]: 'text_666c5b12fea4aa1e1b26bf52', + [InvoiceQuickFilterEnum.Succeeded]: 'text_63ac86d797f728a87b2f9fa1', + [InvoiceQuickFilterEnum.Draft]: 'text_63ac86d797f728a87b2f9f91', + [InvoiceQuickFilterEnum.PaymentOverdue]: 'text_666c5b12fea4aa1e1b26bf55', + [InvoiceQuickFilterEnum.Voided]: 'text_6376641a2a9c70fff5bddcd5', + [InvoiceQuickFilterEnum.PaymentDisputeLost]: 'text_66141e30699a0631f0b2ed32', +} + +const quickFilterMapping: Record< + InvoiceQuickFilterEnum, + { + [key: string]: unknown + } +> = { + [InvoiceQuickFilterEnum.Draft]: { + status: InvoiceStatusTypeEnum.Draft, + }, + [InvoiceQuickFilterEnum.Outstanding]: { + paymentStatus: [InvoicePaymentStatusTypeEnum.Failed, InvoicePaymentStatusTypeEnum.Pending], + status: InvoiceStatusTypeEnum.Finalized, + }, + [InvoiceQuickFilterEnum.PaymentOverdue]: { + paymentOverdue: true, + }, + [InvoiceQuickFilterEnum.Succeeded]: { + paymentStatus: InvoicePaymentStatusTypeEnum.Succeeded, + status: InvoiceStatusTypeEnum.Finalized, + }, + [InvoiceQuickFilterEnum.Voided]: { + status: InvoiceStatusTypeEnum.Voided, + }, + [InvoiceQuickFilterEnum.PaymentDisputeLost]: { + paymentDisputeLost: true, + }, +} + export const InvoiceStatusQuickFilter = () => { const { translate } = useInternationalization() const navigate = useNavigate() - const [searchParams] = useSearchParams() - const { resetFilters, hasAppliedFilters } = useFilters() + const { resetFilters, isQuickFilterActive, buildQuickFilterUrlParams, hasAppliedFilters } = + useFilters() return ( <> @@ -54,54 +87,17 @@ export const InvoiceStatusQuickFilter = () => { {translate('text_63ac86d797f728a87b2f9f8b')} - navigate({ search: buildDraftUrlParams() })} - > - - {translate('text_63ac86d797f728a87b2f9f91')} - - - navigate({ search: buildOutstandingUrlParams() })} - > - - {translate('text_666c5b12fea4aa1e1b26bf52')} - - - navigate({ search: buildPaymentOverdueUrlParams() })} - > - - {translate('text_666c5b12fea4aa1e1b26bf55')} - - - navigate({ search: buildSucceededUrlParams() })} - > - - {translate('text_63ac86d797f728a87b2f9fa1')} - - - navigate({ search: buildVoidedUrlParams() })} - > - - {translate('text_6376641a2a9c70fff5bddcd5')} - - - navigate({ search: buildPaymentDisputeLostUrlParams() })} - > - - {translate('text_66141e30699a0631f0b2ed32')} - - + {Object.entries(quickFilterMapping).map(([key, value]) => ( + navigate({ search: buildQuickFilterUrlParams(value) })} + > + + {translate(invoiceQuickFilterTranslations[key as InvoiceQuickFilterEnum])} + + + ))} ) } diff --git a/src/components/designSystem/Filters/__tests__/hook.test.tsx b/src/components/designSystem/Filters/__tests__/hook.test.tsx new file mode 100644 index 000000000..cbcb24844 --- /dev/null +++ b/src/components/designSystem/Filters/__tests__/hook.test.tsx @@ -0,0 +1,293 @@ +import { renderHook } from '@testing-library/react' +import { ReactNode } from 'react' +import { BrowserRouter } from 'react-router-dom' + +import { InvoicePaymentStatusTypeEnum, InvoiceStatusTypeEnum } from '~/generated/graphql' + +import { FilterContext } from '../context' +import { useFilters } from '../hook' +import { AvailableFiltersEnum } from '../types' +import { + isDraftUrlParams, + isOutstandingUrlParams, + isPaymentDisputeLostUrlParams, + isPaymentOverdueUrlParams, + isSucceededUrlParams, + isVoidedUrlParams, +} from '../utils' + +const staticFilters = { + currency: 'eur', +} + +const wrapper = ({ + children, + withStaticFilters, +}: { + children: ReactNode + withStaticFilters: boolean +}): JSX.Element => { + return ( + +
+ + {children} + +
+
+ ) +} + +describe('draft', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'status=draft' + + expect( + result.current.buildQuickFilterUrlParams({ + status: InvoiceStatusTypeEnum.Draft, + }), + ).toEqual(expectedSearchParams) + + const draftSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isDraftUrlParams(draftSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&status=draft' + + const draftSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect( + result.current.buildQuickFilterUrlParams({ + status: InvoiceStatusTypeEnum.Draft, + }), + ).toEqual(expectedSearchParams) + expect(isDraftUrlParams(draftSearchParams)).toBe(true) + }) +}) + +describe('outstanding', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'paymentStatus=failed,pending&status=finalized' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentStatus: [InvoicePaymentStatusTypeEnum.Failed, InvoicePaymentStatusTypeEnum.Pending], + status: InvoiceStatusTypeEnum.Finalized, + }), + ).toEqual(expectedSearchParams) + + const outstandingSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isOutstandingUrlParams(outstandingSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&paymentStatus=failed,pending&status=finalized' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentStatus: [InvoicePaymentStatusTypeEnum.Failed, InvoicePaymentStatusTypeEnum.Pending], + status: InvoiceStatusTypeEnum.Finalized, + }), + ).toEqual(expectedSearchParams) + + const outstandingSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isOutstandingUrlParams(outstandingSearchParams)).toBe(true) + }) +}) + +describe('payment overdue', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'paymentOverdue=true' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentOverdue: true, + }), + ).toEqual(expectedSearchParams) + + const paymentOverdueSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isPaymentOverdueUrlParams(paymentOverdueSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&paymentOverdue=true' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentOverdue: true, + }), + ).toEqual(expectedSearchParams) + + const paymentOverdueSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isPaymentOverdueUrlParams(paymentOverdueSearchParams)).toBe(true) + }) +}) + +describe('succeeded', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'paymentStatus=succeeded&status=finalized' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentStatus: InvoicePaymentStatusTypeEnum.Succeeded, + status: InvoiceStatusTypeEnum.Finalized, + }), + ).toEqual(expectedSearchParams) + + const succeededSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isSucceededUrlParams(succeededSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&paymentStatus=succeeded&status=finalized' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentStatus: InvoicePaymentStatusTypeEnum.Succeeded, + status: InvoiceStatusTypeEnum.Finalized, + }), + ).toEqual(expectedSearchParams) + + const succeededSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isSucceededUrlParams(succeededSearchParams)).toBe(true) + }) +}) + +describe('voided', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'status=voided' + + expect( + result.current.buildQuickFilterUrlParams({ + status: InvoiceStatusTypeEnum.Voided, + }), + ).toEqual(expectedSearchParams) + + const voidedSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isVoidedUrlParams(voidedSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&status=voided' + + expect( + result.current.buildQuickFilterUrlParams({ + status: InvoiceStatusTypeEnum.Voided, + }), + ).toEqual(expectedSearchParams) + + const voidedSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isVoidedUrlParams(voidedSearchParams)).toBe(true) + }) +}) + +describe('payment dispute lost', () => { + it('should return search params without initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: false }), + }) + + const expectedSearchParams = 'paymentDisputeLost=true' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentDisputeLost: true, + }), + ).toEqual(expectedSearchParams) + + const paymentDisputeLostSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isPaymentDisputeLostUrlParams(paymentDisputeLostSearchParams)).toBe(true) + }) + it('should return search params with initial static filters', () => { + const { result } = renderHook(() => useFilters(), { + wrapper: ({ children }) => wrapper({ children, withStaticFilters: true }), + }) + + const expectedSearchParams = 'currency=eur&paymentDisputeLost=true' + + expect( + result.current.buildQuickFilterUrlParams({ + paymentDisputeLost: true, + }), + ).toEqual(expectedSearchParams) + + const paymentDisputeLostSearchParams = new Map( + new URLSearchParams(`?${expectedSearchParams}`).entries(), + ) as unknown as URLSearchParams + + expect(isPaymentDisputeLostUrlParams(paymentDisputeLostSearchParams)).toBe(true) + }) +}) diff --git a/src/components/designSystem/Filters/__tests__/utils.test.ts b/src/components/designSystem/Filters/__tests__/utils.test.ts index 97c199263..d48762742 100644 --- a/src/components/designSystem/Filters/__tests__/utils.test.ts +++ b/src/components/designSystem/Filters/__tests__/utils.test.ts @@ -1,22 +1,5 @@ -// Note: some test have to transform URLSearchParams to Map to make it work. -// Seems Jest does not support some URLSearchParams methods directly like size. import { AvailableFiltersEnum, filterDataInlineSeparator } from '../types' -import { - buildDraftUrlParams, - buildOutstandingUrlParams, - buildPaymentDisputeLostUrlParams, - buildPaymentOverdueUrlParams, - buildSucceededUrlParams, - buildVoidedUrlParams, - formatActiveFilterValueDisplay, - formatFiltersForInvoiceQuery, - isDraftUrlParams, - isOutstandingUrlParams, - isPaymentDisputeLostUrlParams, - isPaymentOverdueUrlParams, - isSucceededUrlParams, - isVoidedUrlParams, -} from '../utils' +import { formatActiveFilterValueDisplay, formatFiltersForInvoiceQuery } from '../utils' describe('Filters utils', () => { describe('formatFiltersForInvoiceQuery', () => { @@ -113,214 +96,4 @@ describe('Filters utils', () => { expect(result).toBe('Finalized') }) }) - - describe('outstanding', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map(new URLSearchParams(buildOutstandingUrlParams()).entries()) - - expect(isOutstandingUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const succeededSearchParams = new Map( - new URLSearchParams(buildSucceededUrlParams()).entries(), - ) - const draftSearchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - const paymentOverdueSearchParams = new Map( - new URLSearchParams(buildPaymentOverdueUrlParams()).entries(), - ) - const voidedSearchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - const paymentDisputeLostSearchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isOutstandingUrlParams(succeededSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isOutstandingUrlParams(draftSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isOutstandingUrlParams(paymentOverdueSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isOutstandingUrlParams(voidedSearchParams as unknown as URLSearchParams)).toBe(false) - expect( - isOutstandingUrlParams(paymentDisputeLostSearchParams as unknown as URLSearchParams), - ).toBe(false) - }) - }) - - describe('succeeded', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map(new URLSearchParams(buildSucceededUrlParams()).entries()) - - expect(isSucceededUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const outstandingSearchParams = new Map( - new URLSearchParams(buildOutstandingUrlParams()).entries(), - ) - const draftSearchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - const paymentOverdueSearchParams = new Map( - new URLSearchParams(buildPaymentOverdueUrlParams()).entries(), - ) - const voidedSearchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - const paymentDisputeLostSearchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isSucceededUrlParams(outstandingSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isSucceededUrlParams(draftSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isSucceededUrlParams(paymentOverdueSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isSucceededUrlParams(voidedSearchParams as unknown as URLSearchParams)).toBe(false) - expect( - isSucceededUrlParams(paymentDisputeLostSearchParams as unknown as URLSearchParams), - ).toBe(false) - }) - }) - - describe('draft', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - - expect(isDraftUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const outstandingSearchParams = new Map( - new URLSearchParams(buildOutstandingUrlParams()).entries(), - ) - const succeededSearchParams = new Map( - new URLSearchParams(buildSucceededUrlParams()).entries(), - ) - const paymentOverdueSearchParams = new Map( - new URLSearchParams(buildPaymentOverdueUrlParams()).entries(), - ) - const voidedSearchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - const paymentDisputeLostSearchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isDraftUrlParams(outstandingSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isDraftUrlParams(succeededSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isDraftUrlParams(paymentOverdueSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isDraftUrlParams(voidedSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isDraftUrlParams(paymentDisputeLostSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - }) - }) - - describe('paymentOverdue', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map(new URLSearchParams(buildPaymentOverdueUrlParams()).entries()) - - expect(isPaymentOverdueUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const outstandingSearchParams = new Map( - new URLSearchParams(buildOutstandingUrlParams()).entries(), - ) - const succeededSearchParams = new Map( - new URLSearchParams(buildSucceededUrlParams()).entries(), - ) - const draftSearchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - const voidedSearchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - const paymentDisputeLostSearchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isPaymentOverdueUrlParams(outstandingSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isPaymentOverdueUrlParams(succeededSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isPaymentOverdueUrlParams(draftSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isPaymentOverdueUrlParams(voidedSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect( - isPaymentOverdueUrlParams(paymentDisputeLostSearchParams as unknown as URLSearchParams), - ).toBe(false) - }) - }) - - describe('voided', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - - expect(isVoidedUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const outstandingSearchParams = new Map( - new URLSearchParams(buildOutstandingUrlParams()).entries(), - ) - const succeededSearchParams = new Map( - new URLSearchParams(buildSucceededUrlParams()).entries(), - ) - const draftSearchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - const paymentOverdueSearchParams = new Map( - new URLSearchParams(buildPaymentOverdueUrlParams()).entries(), - ) - const paymentDisputeLostSearchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isVoidedUrlParams(outstandingSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isVoidedUrlParams(succeededSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isVoidedUrlParams(draftSearchParams as unknown as URLSearchParams)).toBe(false) - expect(isVoidedUrlParams(paymentOverdueSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect(isVoidedUrlParams(paymentDisputeLostSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - }) - }) - - describe('paymentDisputeLost', () => { - it('should return true when search params are valid', () => { - const searchParams = new Map( - new URLSearchParams(buildPaymentDisputeLostUrlParams()).entries(), - ) - - expect(isPaymentDisputeLostUrlParams(searchParams as unknown as URLSearchParams)).toBe(true) - }) - - it('should return false when search params are not valid', () => { - const outstandingSearchParams = new Map( - new URLSearchParams(buildOutstandingUrlParams()).entries(), - ) - const succeededSearchParams = new Map( - new URLSearchParams(buildSucceededUrlParams()).entries(), - ) - const draftSearchParams = new Map(new URLSearchParams(buildDraftUrlParams()).entries()) - const paymentOverdueSearchParams = new Map( - new URLSearchParams(buildPaymentOverdueUrlParams()).entries(), - ) - const voidedSearchParams = new Map(new URLSearchParams(buildVoidedUrlParams()).entries()) - - expect( - isPaymentDisputeLostUrlParams(outstandingSearchParams as unknown as URLSearchParams), - ).toBe(false) - expect( - isPaymentDisputeLostUrlParams(succeededSearchParams as unknown as URLSearchParams), - ).toBe(false) - expect(isPaymentDisputeLostUrlParams(draftSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - expect( - isPaymentDisputeLostUrlParams(paymentOverdueSearchParams as unknown as URLSearchParams), - ).toBe(false) - expect(isPaymentDisputeLostUrlParams(voidedSearchParams as unknown as URLSearchParams)).toBe( - false, - ) - }) - }) }) diff --git a/src/components/designSystem/Filters/context.tsx b/src/components/designSystem/Filters/context.tsx index 11230c282..9295bbcc4 100644 --- a/src/components/designSystem/Filters/context.tsx +++ b/src/components/designSystem/Filters/context.tsx @@ -9,7 +9,7 @@ interface FilterContextType { staticFilters?: Partial> } -const FilterContext = createContext(undefined) +export const FilterContext = createContext(undefined) export const FiltersProvider: FC> = ({ children, diff --git a/src/components/designSystem/Filters/hook.ts b/src/components/designSystem/Filters/hook.ts index 8f1a46722..474aac3c2 100644 --- a/src/components/designSystem/Filters/hook.ts +++ b/src/components/designSystem/Filters/hook.ts @@ -78,6 +78,41 @@ export const useFilters = () => { return false } + const buildQuickFilterUrlParams = (filters: { [key: string]: unknown }) => { + const staticFilters = Object.entries(context.staticFilters ?? {}) + .map(([key, value]) => `${key}=${value}`) + .join('&') + + const newFilters = Object.entries(filters) + .map(([key, value]) => { + if (Array.isArray(value)) { + return `${key}=${value.join(',')}` + } + + return `${key}=${value}` + }) + .join('&') + + // If there are no static filters, return only the new filters + return staticFilters ? `${staticFilters}&${newFilters}` : newFilters + } + + const isQuickFilterActive = (filters: { [key: string]: unknown }) => { + for (const [key, value] of Object.entries(filters)) { + if (Array.isArray(value)) { + if (searchParamsObject[key] !== value.join(',')) { + return false + } + } else { + if (searchParamsObject[key] !== String(value)) { + return false + } + } + } + + return true + } + return { ...context, hasAppliedFilters: hasAppliedFilters(), @@ -85,5 +120,7 @@ export const useFilters = () => { staticFiltersFormValues: getInitialFiltersFormValues('default'), applyFilters, resetFilters, + isQuickFilterActive, + buildQuickFilterUrlParams, } } diff --git a/src/components/designSystem/Filters/utils.ts b/src/components/designSystem/Filters/utils.ts index 238b5b871..0b0a2c55a 100644 --- a/src/components/designSystem/Filters/utils.ts +++ b/src/components/designSystem/Filters/utils.ts @@ -172,7 +172,7 @@ export const formatActiveFilterValueDisplay = ( export const isOutstandingUrlParams = (searchParams: URLSearchParams): boolean => { return ( - searchParams.size === 2 && + searchParams.size >= 2 && searchParams.get('paymentStatus') === `${InvoicePaymentStatusTypeEnum.Failed},${InvoicePaymentStatusTypeEnum.Pending}` && searchParams.get('status') === InvoiceStatusTypeEnum.Finalized @@ -181,48 +181,24 @@ export const isOutstandingUrlParams = (searchParams: URLSearchParams): boolean = export const isSucceededUrlParams = (searchParams: URLSearchParams): boolean => { return ( - searchParams.size === 2 && + searchParams.size >= 2 && searchParams.get('paymentStatus') === InvoicePaymentStatusTypeEnum.Succeeded && searchParams.get('status') === InvoiceStatusTypeEnum.Finalized ) } export const isDraftUrlParams = (searchParams: URLSearchParams): boolean => { - return searchParams.size === 1 && searchParams.get('status') === InvoiceStatusTypeEnum.Draft + return searchParams.size >= 1 && searchParams.get('status') === InvoiceStatusTypeEnum.Draft } export const isPaymentOverdueUrlParams = (searchParams: URLSearchParams): boolean => { - return searchParams.size === 1 && searchParams.get('paymentOverdue') === 'true' + return searchParams.size >= 1 && searchParams.get('paymentOverdue') === 'true' } export const isVoidedUrlParams = (searchParams: URLSearchParams): boolean => { - return searchParams.size === 1 && searchParams.get('status') === InvoiceStatusTypeEnum.Voided + return searchParams.size >= 1 && searchParams.get('status') === InvoiceStatusTypeEnum.Voided } export const isPaymentDisputeLostUrlParams = (searchParams: URLSearchParams): boolean => { - return searchParams.size === 1 && searchParams.get('paymentDisputeLost') === 'true' -} - -export const buildOutstandingUrlParams = (): string => { - return `?paymentStatus=${InvoicePaymentStatusTypeEnum.Failed},${InvoicePaymentStatusTypeEnum.Pending}&status=${InvoiceStatusTypeEnum.Finalized}` -} - -export const buildSucceededUrlParams = (): string => { - return `?paymentStatus=${InvoicePaymentStatusTypeEnum.Succeeded}&status=${InvoiceStatusTypeEnum.Finalized}` -} - -export const buildDraftUrlParams = (): string => { - return `?status=${InvoiceStatusTypeEnum.Draft}` -} - -export const buildPaymentOverdueUrlParams = (): string => { - return `?paymentOverdue=true` -} - -export const buildVoidedUrlParams = (): string => { - return `?status=${InvoiceStatusTypeEnum.Voided}` -} - -export const buildPaymentDisputeLostUrlParams = (): string => { - return `?paymentDisputeLost=true` + return searchParams.size >= 1 && searchParams.get('paymentDisputeLost') === 'true' }