-
Notifications
You must be signed in to change notification settings - Fork 1
/
HistoryTable.tsx
188 lines (166 loc) · 4.59 KB
/
HistoryTable.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { HamburgerIcon } from "@chakra-ui/icons";
import {
Button as ButtonBase,
IconButton as IconButtonBase,
AlertDialog,
AlertDialogOverlay,
AlertDialogContent,
AlertDialogHeader,
AlertDialogBody,
AlertDialogFooter,
useDisclosure,
useToast,
Heading,
Tag,
} from "@chakra-ui/react";
import { useEffect, useRef, useState } from "react";
import { FiTrash2 } from "react-icons/fi";
import { HistoryDTO } from "@/dto/http/HistoryDTO";
import IconButton from "@/components/IconButton";
import Divider from "@/components/Divider";
import { deleteHistory, getAllHistoryByRegister } from "@/services/history";
import DataTableBase from "../DataTableBase";
import Box from "../Box";
interface Props {
registerId: number
}
export default function HistoryTable({ registerId }: Props) {
const toast = useToast();
const [historyId, setHistoryId] = useState<number>(0);
const [history, setHistory] = useState<HistoryDTO[]>([]);
const {
isOpen: isOpenConfirm,
onOpen: onOpenConfirm,
onClose: onCloseConfirm,
} = useDisclosure();
const cancelRef = useRef<HTMLInputElement>(null);
const columns = [
{
name: "Período",
selector: (row: HistoryDTO) => (
<Tag size="md" variant="solid" borderRadius="full" bg="primary.700">
{row.period}
</Tag>
),
},
{
name: "Entradas",
selector: (row: HistoryDTO) => (
<Tag size="md" variant="solid" borderRadius="full" colorScheme="green">
{new Intl.NumberFormat("pt-br", {
style: "currency",
currency: "BRL",
}).format(Number(row.totalCredit))}
</Tag>
),
},
{
name: "Débito",
selector: (row: HistoryDTO) => (
<Tag size="md" variant="solid" borderRadius="full" colorScheme="red">
{new Intl.NumberFormat("pt-br", {
style: "currency",
currency: "BRL",
}).format(Number(row.totalDebt))}
</Tag>
),
},
{
name: "Saldo",
selector: (row: HistoryDTO) => (
<Tag size="md" variant="solid" borderRadius="full" colorScheme="blue">
{new Intl.NumberFormat("pt-br", {
style: "currency",
currency: "BRL",
}).format(Number(row.balanceCredit))}
</Tag>
),
},
{
name: "Deletar",
selector: (row: HistoryDTO) =>
row.financialHistoryId && (
<IconButton
size="md"
rounded={25}
boxShadow="md"
colorScheme="red"
aria-label="Delete History"
onClick={() => handleConfirm(row.financialHistoryId)}
icon={<FiTrash2 />}
/>
),
},
];
function handleConfirm(id: number) {
setHistoryId(id);
onOpenConfirm();
}
async function handleDelete() {
try {
const res = await deleteHistory(registerId, historyId);
if (res.status === 200) {
toast({
title: res.data.message,
status: "success",
isClosable: true,
});
onCloseConfirm();
loadHistory();
}
} catch (error: any) {
toast({
title: error.message,
status: "error",
isClosable: true,
});
}
}
async function loadHistory() {
try {
const res = await getAllHistoryByRegister(registerId);
if (res.status == 200) {
setHistory(res.data.financialList);
}
} catch (error: any) {
console.log(error);
}
}
useEffect(() => {
if (registerId) loadHistory();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [registerId]);
return (
<>
<Box>
<Heading as="h4" size="md">
Histórico
</Heading>
<Divider mt={2} />
<DataTableBase columns={columns} data={history} title="" />
</Box>
<AlertDialog
isOpen={isOpenConfirm}
leastDestructiveRef={cancelRef}
onClose={onCloseConfirm}
>
<AlertDialogOverlay>
<AlertDialogContent>
<AlertDialogHeader fontSize="lg" fontWeight="bold">
Deletar Histórico
</AlertDialogHeader>
<AlertDialogBody>
Você tem certeza que deseja deletar o(s) histórico(s)?
</AlertDialogBody>
<AlertDialogFooter>
<ButtonBase onClick={onCloseConfirm}>Cancel</ButtonBase>
<ButtonBase colorScheme="red" onClick={handleDelete} ml={3}>
Delete
</ButtonBase>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogOverlay>
</AlertDialog>
</>
);
}