Skip to content

Commit

Permalink
fix: calculate total amount from file when disbursement is not saved yet
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioTeixeira95 committed Feb 16, 2024
1 parent 59d2446 commit 38899cd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/pages/DisbursementsNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,37 @@ export const DisbursementsNew = () => {
if (apiError) {
dispatch(clearDisbursementDraftsErrorAction());
}
calculateDisbursementTotalAmountFromFile(file);
setCsvFile(file);
};

const calculateDisbursementTotalAmountFromFile = (file?: File) => {
if (file) {
const reader = new FileReader();
reader.readAsText(file);
const handleLoadFile = () => {
const totalAmount = reader.result
?.toString()
.split("\n")
.slice(1)
.reduce(
(accumulator, line) =>
!line ? accumulator : accumulator + Number(line.split(",")[2]),
0,
);

setDraftDetails({
...draftDetails,
stats: {
...draftDetails?.stats,
totalAmount: totalAmount?.toString() ?? "0",
},
} as Disbursement);
};
reader.addEventListener("load", handleLoadFile, false);
}
};

const handleViewDetails = () => {
navigate(`${Routes.DISBURSEMENTS}/${disbursementDrafts.newDraftId}`);
resetState();
Expand Down

0 comments on commit 38899cd

Please sign in to comment.