Skip to content

Commit

Permalink
Merge pull request #32 from Robert-M-Lucas/dev-SCRUM-46/51
Browse files Browse the repository at this point in the history
store uploaded transactions
  • Loading branch information
DylanRoskilly authored Apr 26, 2024
2 parents c46a5a0 + fcf1b79 commit d0258ed
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
31 changes: 16 additions & 15 deletions src/components/transactions/CSVUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import { useState } from "react";
import { Transaction } from "./Transaction";
import { Alert, Button, Form, Modal } from "react-bootstrap";
import { writeNewTransactionsBatched } from "../../utils/transaction.ts";
import { auth } from "../../utils/firebase";

export function CSVUpload({ show, setShow }: { show: boolean, setShow: React.Dispatch<React.SetStateAction<boolean>> }) {
const [error, setError] = useState<string | null>();
const [successMsg, setSuccessMsg] = useState<string | null>();

const reader = new FileReader();

function handleUpload() {
async function handleUpload() {
setError(null);
setSuccessMsg(null);

const fileElement = document.getElementById("file") as HTMLInputElement | null;
if (!fileElement || fileElement.files?.length === 0) return setError("You haven't uploaded a CSV file");

const file = fileElement.files![0];
if (!file) return setError("You haven't uploaded a CSV file");

reader.onload = (event) => {
reader.onload = async (event) => {
if (!auth.currentUser) return setError("You are not signed in");

const csvContent = event.target?.result;
if (!csvContent || csvContent instanceof ArrayBuffer) return setError("Unable to read uploaded CSV file");

const rows = csvContent
const transactionDocuments = csvContent
.split("\n")
.slice(1)
.map((row) => row.split(","));

const transactions = rows
.map((row) => row.split(","))
.filter((row) => row[3] === "Card payment" || row[3] === "Faster payment") // filter by type
.map((row) => new Transaction().fromRow(row));
.map((row) => new Transaction().fromRow(row))
.filter((transaction) => transaction.isValid)
.map((transaction) => transaction.toDocument(auth.currentUser!.uid));

const validTransactions = transactions.filter((transaction) => transaction.isValid);
if (validTransactions.length === 0) return setError("The uploaded CSV file has no valid transactions");

// -------------------------------------------------------------------------------------
// TODO: STORE "validTransactions" IN THE DATABASE
// -------------------------------------------------------------------------------------
if (transactionDocuments.length === 0) return setError("The uploaded CSV file has no valid transactions");

await writeNewTransactionsBatched(auth.currentUser, transactionDocuments);

setSuccessMsg(`${validTransactions.length} valid transactions have been imported out of ${transactions.length} total transactions`);
setSuccessMsg(`${transactionDocuments.length} transactions have been imported`);
setTimeout(() => setSuccessMsg(null), 10000);

fileElement.value = "";
Expand Down
10 changes: 6 additions & 4 deletions src/components/transactions/InputTransaction.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useState } from "react";
import { Transaction, emojis, formatDate, formatTime } from "./Transaction";
import { Button, Modal, Form, Alert } from "react-bootstrap";
import { auth } from "../../utils/firebase";
import { writeNewTransaction } from "../../utils/transaction.ts";

export function InputTransaction({ show, setShow}: { show: boolean, setShow: React.Dispatch<React.SetStateAction<boolean>> }) {
const [name, setName] = useState<string>("");
Expand All @@ -15,10 +17,12 @@ export function InputTransaction({ show, setShow}: { show: boolean, setShow: Rea
const [error, setError] = useState<string | null>("");
const [successMsg, setSuccessMsg] = useState<string | null>("");

function addTransaction() {
async function addTransaction() {
setError(null);
setSuccessMsg(null);

if (!auth.currentUser) return setError("You are not signed in");

const date = new Date();

const transaction = new Transaction()
Expand All @@ -39,9 +43,7 @@ export function InputTransaction({ show, setShow}: { show: boolean, setShow: Rea
return;
}

// -------------------------------------------------------------------------------------
// TODO: STORE "transaction" IN THE DATABASE
// -------------------------------------------------------------------------------------
await writeNewTransaction(auth.currentUser, transaction.toDocument(auth.currentUser.uid));

setSuccessMsg("Transaction has been successfully added");
setTimeout(() => setSuccessMsg(null), 10000);
Expand Down
17 changes: 17 additions & 0 deletions src/components/transactions/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Transaction as TransactionDocument } from "../../utils/transaction.ts";

export const emojis: { [index: string]: string } = {
"Income": "💸",
"Transfers": "🏦",
Expand Down Expand Up @@ -148,6 +150,21 @@ export class Transaction {
return this;
}

toDocument(uid: string): TransactionDocument {
return new TransactionDocument(
this.address as string,
this.amount as number,
this.category as string,
this.currency as string,
new Date((this.date as string) + (this.time as string)).getTime(),
this.description as string,
this.emoji as string,
this.name as string,
this.notes as string,
uid
);
}

isNotEmpty(val?: string): val is string {
return !!val && val.trim() !== "";
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down

0 comments on commit d0258ed

Please sign in to comment.