-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new payments list tab in invoices
- Loading branch information
Showing
4 changed files
with
329 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { ApolloError, gql, LazyQueryHookOptions } from '@apollo/client' | ||
import { FC, useEffect, useRef } from 'react' | ||
import { useSearchParams } from 'react-router-dom' | ||
|
||
import { addToast } from '~/core/apolloClient' | ||
import { paymentStatusMapping } from '~/core/constants/statusInvoiceMapping' | ||
import { intlFormatNumber } from '~/core/formats/intlFormatNumber' | ||
import { deserializeAmount } from '~/core/serializers/serializeAmount' | ||
import { formatDateToTZ } from '~/core/timezone' | ||
import { copyToClipboard } from '~/core/utils/copyToClipboard' | ||
import { | ||
CurrencyEnum, | ||
GetPaymentRequestsListQuery, | ||
GetPaymentRequestsListQueryHookResult, | ||
PaymentRequestsForPaymentsListFragment, | ||
} from '~/generated/graphql' | ||
import { useInternationalization } from '~/hooks/core/useInternationalization' | ||
|
||
import { InfiniteScroll, Status, Typography } from '../designSystem' | ||
import { Table } from '../designSystem/Table' | ||
|
||
gql` | ||
fragment PaymentRequestsForPaymentsList on PaymentRequest { | ||
id | ||
paymentStatus | ||
invoices { | ||
id | ||
status | ||
} | ||
amountCents | ||
amountCurrency | ||
customer { | ||
name | ||
displayName | ||
applicableTimezone | ||
} | ||
createdAt | ||
} | ||
` | ||
|
||
interface PaymentsListProps { | ||
isLoading: boolean | ||
paymentRequests?: PaymentRequestsForPaymentsListFragment[] | ||
metadata?: GetPaymentRequestsListQuery['paymentRequests']['metadata'] | ||
error?: ApolloError | ||
variables?: LazyQueryHookOptions['variables'] | ||
fetchMore?: GetPaymentRequestsListQueryHookResult['fetchMore'] | ||
} | ||
|
||
export const PaymentsList: FC<PaymentsListProps> = ({ | ||
isLoading, | ||
paymentRequests, | ||
metadata, | ||
error, | ||
variables, | ||
fetchMore, | ||
}) => { | ||
const [searchParams] = useSearchParams() | ||
const { translate } = useInternationalization() | ||
|
||
const listContainerElementRef = useRef<HTMLDivElement>(null) | ||
|
||
useEffect(() => { | ||
// Scroll to top of list when switching tabs | ||
listContainerElementRef?.current?.scrollTo({ top: 0 }) | ||
}, [searchParams]) | ||
|
||
return ( | ||
<div ref={listContainerElementRef}> | ||
<InfiniteScroll | ||
onBottom={() => { | ||
const { currentPage = 0, totalPages = 0 } = metadata || {} | ||
|
||
currentPage < totalPages && | ||
!isLoading && | ||
fetchMore?.({ | ||
variables: { page: currentPage + 1 }, | ||
}) | ||
}} | ||
> | ||
<Table | ||
name="payments-list" | ||
data={paymentRequests || []} | ||
containerSize={{ | ||
default: 16, | ||
md: 48, | ||
}} | ||
isLoading={isLoading} | ||
hasError={!!error} | ||
actionColumn={() => { | ||
return [ | ||
{ | ||
startIcon: 'duplicate', | ||
title: translate('text_1737029625089rtcf3ah5khq'), | ||
onAction: ({ id }) => { | ||
copyToClipboard(id) | ||
addToast({ | ||
severity: 'info', | ||
translateKey: translate('text_17370296250897n2pakp5v33'), | ||
}) | ||
}, | ||
}, | ||
] | ||
}} | ||
columns={[ | ||
{ | ||
key: 'paymentStatus', | ||
title: translate('text_63ac86d797f728a87b2f9fa7'), | ||
minWidth: 80, | ||
content: ({ paymentStatus }) => ( | ||
<Status {...paymentStatusMapping({ paymentStatus })} /> | ||
), | ||
}, | ||
{ | ||
key: 'invoices.length', | ||
title: translate('text_63ac86d797f728a87b2f9fad'), | ||
minWidth: 160, | ||
content: ({ invoices }) => { | ||
if (invoices.length > 1) { | ||
return translate('text_17370296250898eqj4qe4qg9', { count: invoices.length }) | ||
} | ||
return invoices[0]?.id | ||
}, | ||
}, | ||
{ | ||
key: 'amountCents', | ||
title: translate('text_6419c64eace749372fc72b3e'), | ||
content: ({ amountCents, amountCurrency }) => ( | ||
<Typography variant="bodyHl" color="textSecondary" noWrap> | ||
{intlFormatNumber( | ||
deserializeAmount(amountCents, amountCurrency || CurrencyEnum.Usd), | ||
{ | ||
currency: amountCurrency || CurrencyEnum.Usd, | ||
}, | ||
)} | ||
</Typography> | ||
), | ||
}, | ||
{ | ||
key: 'customer.displayName', | ||
title: translate('text_63ac86d797f728a87b2f9fb3'), | ||
maxSpace: true, | ||
content: ({ customer }) => customer?.displayName || customer?.name, | ||
}, | ||
{ | ||
key: 'createdAt', | ||
title: translate('text_664cb90097bfa800e6efa3f5'), | ||
content: ({ createdAt, customer }) => | ||
formatDateToTZ(createdAt, customer.applicableTimezone), | ||
}, | ||
]} | ||
placeholder={{ | ||
errorState: variables?.searchTerm | ||
? { | ||
title: translate('text_623b53fea66c76017eaebb6e'), | ||
subtitle: translate('text_63bab307a61c62af497e0599'), | ||
} | ||
: { | ||
title: translate('text_63ac86d797f728a87b2f9fea'), | ||
subtitle: translate('text_63ac86d797f728a87b2f9ff2'), | ||
buttonTitle: translate('text_63ac86d797f728a87b2f9ffa'), | ||
buttonAction: () => location.reload(), | ||
buttonVariant: 'primary', | ||
}, | ||
emptyState: variables?.searchTerm | ||
? { | ||
title: translate('text_623b53fea66c76017eaebb6e'), | ||
subtitle: translate('text_63bab307a61c62af497e0599'), | ||
} | ||
: { | ||
title: translate('text_63ac86d797f728a87b2f9fea'), | ||
subtitle: translate('text_63ac86d797f728a87b2f9ff2'), | ||
buttonTitle: translate('text_63ac86d797f728a87b2f9ffa'), | ||
buttonAction: () => location.reload(), | ||
buttonVariant: 'primary', | ||
}, | ||
}} | ||
/> | ||
</InfiniteScroll> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.