Skip to content

Commit

Permalink
[SDP-1325] fix code that relied on CSV column order (#143)
Browse files Browse the repository at this point in the history
### What

Fix code that relied on CSV column order

### Why

This is an unreasonable assumption.

### Further detail

Here's the main change: 594eb85

The other commit is a small refactor that doesn't change the main logic.
  • Loading branch information
marcelosalloum authored Aug 29, 2024
1 parent 5be16e2 commit f88f753
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions src/pages/DisbursementsNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,43 +161,42 @@ export const DisbursementsNew = () => {
};

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
: BigNumber(accumulator)
.plus(BigNumber(line.split(",")[2]))
.toNumber(),
0,
);

setDraftDetails({
...draftDetails,
stats: {
...draftDetails?.stats,
totalAmount: totalAmount?.toString() ?? "0",
},
} as Disbursement);

// update future balance
const assetBalance = allBalances?.find(
(a) => a.assetCode === draftDetails?.asset.code,
)?.balance;

if (totalAmount) {
setFutureBalance(Number(assetBalance) - totalAmount);
}
};
reader.addEventListener("load", handleLoadFile, false);
}
if (!file) return;

const reader = new FileReader();
reader.readAsText(file);

reader.onload = () => {
const csvRows = reader.result?.toString();
if (!csvRows) return;

const [header, ...rows] = csvRows.split("\n");
const amountIndex = header.split(",").indexOf("amount");
if (amountIndex === -1) return;

const totalAmount = rows.reduce((accumulator, line) => {
return !line
? accumulator
: accumulator.plus(BigNumber(line.split(",")[amountIndex]));
}, BigNumber(0));

setDraftDetails({
...draftDetails,
stats: {
...draftDetails?.stats,
totalAmount: totalAmount?.toString() ?? "0",
},
} as Disbursement);

// update future balance
const assetBalance =
allBalances?.find((a) => a.assetCode === draftDetails?.asset.code)
?.balance ?? "0";

if (totalAmount) {
setFutureBalance(Number(assetBalance) - totalAmount.toNumber());
}
};
};

const handleViewDetails = () => {
Expand Down

0 comments on commit f88f753

Please sign in to comment.