|
| 1 | +import { useInvoicesContext } from '../../contexts'; |
| 2 | +import { Badge, Box, Dd, descriptors, Dl, Dt, Heading, Spinner, Text } from '../../customizables'; |
| 3 | +import { Header, LineItems } from '../../elements'; |
| 4 | +import { useRouter } from '../../router'; |
| 5 | +import { common } from '../../styledSystem'; |
| 6 | +import { colors } from '../../utils'; |
| 7 | +import { truncateWithEndVisible } from '../../utils/truncateTextWithEndVisible'; |
| 8 | + |
| 9 | +export const InvoicePage = () => { |
| 10 | + const { params, navigate } = useRouter(); |
| 11 | + const { getInvoiceById, isLoading } = useInvoicesContext(); |
| 12 | + const invoice = params.invoiceId ? getInvoiceById(params.invoiceId) : null; |
| 13 | + |
| 14 | + if (isLoading) { |
| 15 | + return ( |
| 16 | + <Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}> |
| 17 | + <Spinner |
| 18 | + colorScheme='primary' |
| 19 | + sx={{ margin: 'auto', display: 'block' }} |
| 20 | + elementDescriptor={descriptors.spinner} |
| 21 | + /> |
| 22 | + </Box> |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + return ( |
| 27 | + <> |
| 28 | + <Header.Root> |
| 29 | + <Header.BackLink onClick={() => void navigate('../../', { searchParams: new URLSearchParams('tab=invoices') })}> |
| 30 | + <Header.Title |
| 31 | + localizationKey='Invoices' |
| 32 | + textVariant='h2' |
| 33 | + /> |
| 34 | + </Header.BackLink> |
| 35 | + </Header.Root> |
| 36 | + <Box |
| 37 | + elementDescriptor={descriptors.invoiceRoot} |
| 38 | + sx={t => ({ |
| 39 | + display: 'flex', |
| 40 | + flexDirection: 'column', |
| 41 | + gap: t.space.$4, |
| 42 | + borderTopWidth: t.borderWidths.$normal, |
| 43 | + borderTopStyle: t.borderStyles.$solid, |
| 44 | + borderTopColor: t.colors.$neutralAlpha100, |
| 45 | + marginBlockStart: t.space.$4, |
| 46 | + paddingBlockStart: t.space.$4, |
| 47 | + })} |
| 48 | + > |
| 49 | + {!invoice ? ( |
| 50 | + <Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}> |
| 51 | + <Text>Invoice not found</Text> |
| 52 | + </Box> |
| 53 | + ) : ( |
| 54 | + <Box |
| 55 | + elementDescriptor={descriptors.invoiceCard} |
| 56 | + sx={t => ({ |
| 57 | + borderWidth: t.borderWidths.$normal, |
| 58 | + borderStyle: t.borderStyles.$solid, |
| 59 | + borderColor: t.colors.$neutralAlpha100, |
| 60 | + borderRadius: t.radii.$lg, |
| 61 | + overflow: 'hidden', |
| 62 | + })} |
| 63 | + > |
| 64 | + <Box |
| 65 | + elementDescriptor={descriptors.invoiceHeader} |
| 66 | + as='header' |
| 67 | + sx={t => ({ |
| 68 | + padding: t.space.$4, |
| 69 | + background: common.mergedColorsBackground( |
| 70 | + colors.setAlpha(t.colors.$colorBackground, 1), |
| 71 | + t.colors.$neutralAlpha50, |
| 72 | + ), |
| 73 | + borderBlockEndWidth: t.borderWidths.$normal, |
| 74 | + borderBlockEndStyle: t.borderStyles.$solid, |
| 75 | + borderBlockEndColor: t.colors.$neutralAlpha100, |
| 76 | + })} |
| 77 | + > |
| 78 | + <Box |
| 79 | + sx={{ |
| 80 | + display: 'flex', |
| 81 | + justifyContent: 'space-between', |
| 82 | + alignItems: 'center', |
| 83 | + }} |
| 84 | + > |
| 85 | + <Heading |
| 86 | + textVariant='h2' |
| 87 | + elementDescriptor={descriptors.invoiceTitle} |
| 88 | + > |
| 89 | + {truncateWithEndVisible(invoice.id)} |
| 90 | + </Heading> |
| 91 | + <Badge |
| 92 | + elementDescriptor={descriptors.invoiceBadge} |
| 93 | + colorScheme={ |
| 94 | + invoice.status === 'paid' ? 'success' : invoice.status === 'unpaid' ? 'warning' : 'danger' |
| 95 | + } |
| 96 | + sx={{ textTransform: 'capitalize' }} |
| 97 | + > |
| 98 | + {invoice.status} |
| 99 | + </Badge> |
| 100 | + </Box> |
| 101 | + <Dl |
| 102 | + elementDescriptor={descriptors.invoiceDetails} |
| 103 | + sx={t => ({ |
| 104 | + display: 'flex', |
| 105 | + justifyContent: 'space-between', |
| 106 | + marginBlockStart: t.space.$3, |
| 107 | + })} |
| 108 | + > |
| 109 | + <Box elementDescriptor={descriptors.invoiceDetailsItem}> |
| 110 | + <Dt elementDescriptor={descriptors.invoiceDetailsItemTitle}> |
| 111 | + <Text |
| 112 | + colorScheme='secondary' |
| 113 | + variant='body' |
| 114 | + > |
| 115 | + Created on |
| 116 | + </Text> |
| 117 | + </Dt> |
| 118 | + <Dd elementDescriptor={descriptors.invoiceDetailsItemValue}> |
| 119 | + <Text variant='subtitle'>{new Date(invoice.paymentDueOn).toLocaleDateString()}</Text> |
| 120 | + </Dd> |
| 121 | + </Box> |
| 122 | + <Box |
| 123 | + elementDescriptor={descriptors.invoiceDetailsItem} |
| 124 | + sx={{ |
| 125 | + textAlign: 'right', |
| 126 | + }} |
| 127 | + > |
| 128 | + <Dt elementDescriptor={descriptors.invoiceDetailsItemTitle}> |
| 129 | + <Text |
| 130 | + colorScheme='secondary' |
| 131 | + variant='body' |
| 132 | + > |
| 133 | + Due on |
| 134 | + </Text> |
| 135 | + </Dt> |
| 136 | + <Dd elementDescriptor={descriptors.invoiceDetailsItemValue}> |
| 137 | + <Text variant='subtitle'>{new Date(invoice.paymentDueOn).toLocaleDateString()}</Text> |
| 138 | + </Dd> |
| 139 | + </Box> |
| 140 | + </Dl> |
| 141 | + </Box> |
| 142 | + <Box |
| 143 | + elementDescriptor={descriptors.invoiceContent} |
| 144 | + sx={t => ({ |
| 145 | + padding: t.space.$4, |
| 146 | + })} |
| 147 | + > |
| 148 | + <LineItems.Root> |
| 149 | + <LineItems.Group> |
| 150 | + <LineItems.Title title='Plan' /> |
| 151 | + <LineItems.Description |
| 152 | + text={`${invoice.totals.grandTotal.currencySymbol}${invoice.totals.grandTotal.amountFormatted}`} |
| 153 | + suffix='per month' |
| 154 | + /> |
| 155 | + </LineItems.Group> |
| 156 | + <LineItems.Group |
| 157 | + variant='secondary' |
| 158 | + borderTop |
| 159 | + > |
| 160 | + <LineItems.Title title='Subtotal' /> |
| 161 | + <LineItems.Description |
| 162 | + text={`${invoice.totals.grandTotal.currencySymbol}${invoice.totals.grandTotal.amountFormatted}`} |
| 163 | + /> |
| 164 | + </LineItems.Group> |
| 165 | + <LineItems.Group variant='secondary'> |
| 166 | + <LineItems.Title title='Tax' /> |
| 167 | + <LineItems.Description |
| 168 | + text={`${invoice.totals.grandTotal.currencySymbol}${invoice.totals.grandTotal.amountFormatted}`} |
| 169 | + /> |
| 170 | + </LineItems.Group> |
| 171 | + <LineItems.Group borderTop> |
| 172 | + <LineItems.Title title='Total due' /> |
| 173 | + <LineItems.Description |
| 174 | + text={`${invoice.totals.grandTotal.currencySymbol}${invoice.totals.grandTotal.amountFormatted}`} |
| 175 | + prefix='USD' |
| 176 | + /> |
| 177 | + </LineItems.Group> |
| 178 | + </LineItems.Root> |
| 179 | + </Box> |
| 180 | + </Box> |
| 181 | + )} |
| 182 | + </Box> |
| 183 | + </> |
| 184 | + ); |
| 185 | +}; |
0 commit comments