-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrawerContainer.tsx
96 lines (89 loc) · 2.73 KB
/
DrawerContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Divider, Table, Tbody, Th, Thead, Tr } from '@chakra-ui/react';
import {
Account,
CartLine,
useAccountsQuery,
useAddCartLineAccountMutation,
} from '@multi-cart/react-data-access';
import { Drawer, SearchBar, TextMuted } from '@multi-cart/react-ui';
import { getRemainingAmount } from '@multi-cart/util';
import React, { useCallback } from 'react';
import AccountRow from '../account-row/AccountRow';
export interface DrawerContainerProps {
line: CartLine;
isOpen: boolean;
onClose();
btnRef?: React.MutableRefObject<undefined>;
}
export const DrawerContainer = ({
btnRef,
isOpen,
line,
onClose,
}: DrawerContainerProps) => {
const [{ data }] = useAccountsQuery();
const [, addCartLineAccount] = useAddCartLineAccountMutation();
const [searchTerm, setSearchTerm] = React.useState('');
const handleSelect = async (a: Account) => {
onClose();
const remainingAmount = getRemainingAmount(line);
await addCartLineAccount({
cartId: line.cartId,
cartLineId: line.id,
accountNumber: a.accountNumber,
amount: Math.abs(remainingAmount),
});
};
const isAlreadySelected = useCallback(
(accountNumber: string) => {
return line.cartLineAccounts
? line.cartLineAccounts.filter((a) => a.accountNumber === accountNumber)
.length !== 0
: false;
},
[line.cartLineAccounts]
);
const filterFn = (account: Account) =>
account.accountNumber.toLowerCase().includes(searchTerm.toLowerCase()) ||
account.accountName.toLowerCase().includes(searchTerm.toLowerCase());
return (
<Drawer
isOpen={isOpen}
onClose={onClose}
btnRef={btnRef}
drawerHeader={
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
}
>
<Divider />
<Table variant="simple" colorScheme="gray">
<Thead>
<Tr>
<Th>
<TextMuted style={{ textAlign: 'left' }}>Account #</TextMuted>
</Th>
<Th>
<TextMuted style={{ textAlign: 'left' }}>Name</TextMuted>
</Th>
<Th>
<TextMuted style={{ textAlign: 'left' }}>
Amount Remaining
</TextMuted>
</Th>
</Tr>
</Thead>
<Tbody>
{data?.accounts.filter(filterFn).map((filteredAccount: Account) => (
<AccountRow
account={filteredAccount}
isAlreadySelected={isAlreadySelected(filteredAccount.accountNumber)}
handleSelect={handleSelect}
key={filteredAccount.accountNumber}
/>
))}
</Tbody>
</Table>
</Drawer>
);
};
export default DrawerContainer;