Skip to content

Commit

Permalink
transaction tile
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanRoskilly committed Apr 28, 2024
1 parent 6994a67 commit b02c61b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
14 changes: 11 additions & 3 deletions src/components/transactions/CSVUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import { Alert, Button, Form, Modal } from "react-bootstrap";
import { writeNewTransactionsBatched } from "../../utils/transaction.ts";
import { auth } from "../../utils/firebase";

export function CSVUpload({ show, closeModal }: { show: boolean, closeModal: () => void }) {
export function stopDragging(e: React.PointerEvent<HTMLDivElement>) {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
}

export function CSVUpload({ show, closeModal }: { show: boolean, closeModal: (added: boolean) => void }) {
const [error, setError] = useState<string | null>();
const [successMsg, setSuccessMsg] = useState<string | null>();

const [added, setAdded] = useState<boolean>(false);

const reader = new FileReader();

async function handleUpload() {
Expand Down Expand Up @@ -43,12 +50,13 @@ export function CSVUpload({ show, closeModal }: { show: boolean, closeModal: ()
setTimeout(() => setSuccessMsg(null), 10000);

fileElement.value = "";
setAdded(true);
};

reader.readAsText(file);
};

return <Modal show={show} onHide={closeModal}>
return <Modal show={show} onHide={() => closeModal(added)}>
<Modal.Header closeButton>
<Modal.Title>Upload CSV Transactions</Modal.Title>
</Modal.Header>
Expand All @@ -61,7 +69,7 @@ export function CSVUpload({ show, closeModal }: { show: boolean, closeModal: ()
{error && <Alert variant="danger">{error}</Alert>}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={closeModal}>Close</Button>
<Button variant="secondary" onClick={() => closeModal(added)}>Close</Button>
<Button variant="primary" onClick={handleUpload}>Upload CSV</Button>
</Modal.Footer>
</Modal>
Expand Down
10 changes: 7 additions & 3 deletions src/components/transactions/InputTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Button, Modal, Form, Alert } from "react-bootstrap";
import { auth } from "../../utils/firebase";
import { writeNewTransaction } from "../../utils/transaction.ts";

export function InputTransaction({ show, closeModal }: { show: boolean, closeModal: () => void }) {
export function InputTransaction({ show, closeModal }: { show: boolean, closeModal: (added: boolean) => void }) {
const [name, setName] = useState<string>("");
const [category, setCategory] = useState<string>("Income");

Expand All @@ -17,6 +17,8 @@ export function InputTransaction({ show, closeModal }: { show: boolean, closeMod
const [error, setError] = useState<string | null>("");
const [successMsg, setSuccessMsg] = useState<string | null>("");

const [added, setAdded] = useState<boolean>(false);

async function addTransaction() {
setError(null);
setSuccessMsg(null);
Expand Down Expand Up @@ -53,9 +55,11 @@ export function InputTransaction({ show, closeModal }: { show: boolean, closeMod
setDescription("");
setNotes("");
setAddress("");

setAdded(true);
}

return <Modal show={show} onHide={closeModal}>
return <Modal show={show} onHide={() => closeModal(added)}>
<Modal.Header closeButton>
<Modal.Title>Add Transaction</Modal.Title>
</Modal.Header>
Expand All @@ -76,7 +80,7 @@ export function InputTransaction({ show, closeModal }: { show: boolean, closeMod
{error && <Alert variant="danger">{error}</Alert>}
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={closeModal}>Close</Button>
<Button variant="secondary" onClick={() => closeModal(added)}>Close</Button>
<Button variant="primary" onClick={addTransaction}>Add Transaction</Button>
</Modal.Footer>
</Modal>
Expand Down
26 changes: 13 additions & 13 deletions src/pages/dashboard/add transaction tile/AddTransactionTile.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {ReactNode, useState} from "react";
import {CSVUpload} from "../../../components/transactions/CSVUpload.tsx";
import {CSVUpload, stopDragging} from "../../../components/transactions/CSVUpload.tsx";
import {InputTransaction} from "../../../components/transactions/InputTransaction.tsx";

export default function AddTransactionTile(updateTransactions: () => void): ReactNode {
const [showCSVModal, setShowCSVModal] = useState(false);
const [showTransactionModal, setShowTransactionModal] = useState(false);

const onCSVModalClose = () => {
setShowCSVModal(false);
updateTransactions();
};

const onTransactionModalClose = () => {
setShowTransactionModal(false);
updateTransactions();
function closeModal(added: boolean, setShow: React.Dispatch<React.SetStateAction<boolean>>) {
setShow(false);
if (added) updateTransactions();
}
const onCSVModalClose = (added: boolean) => closeModal(added, setShowCSVModal)
const onTransactionModalClose = (added: boolean) => closeModal(added, setShowTransactionModal)

return <>
return <div className="card w-100 h-100" onPointerDown={showCSVModal || showTransactionModal ? stopDragging : undefined}>
<CSVUpload show={showCSVModal} closeModal={onCSVModalClose}/>
<InputTransaction show={showTransactionModal} closeModal={onTransactionModalClose}/>
<button onClick={() => setShowCSVModal(true)}>Upload CSV</button>
<button onClick={() => setShowTransactionModal(true)}>Add Transaction</button>
</>
<div className="card-header w-100 fw-bold align">Import</div>
<div className="card-body p-1 align-content-center">
<button className="btn btn-primary w-100 mb-2" onClick={() => setShowCSVModal(true)}>Upload CSV</button>
<button className="btn btn-primary w-100" onClick={() => setShowTransactionModal(true)}>Add Transaction</button>
</div>
</div>
}
10 changes: 3 additions & 7 deletions src/pages/dashboard/goal setting tile/GoalSettingTile.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {ReactNode, useState} from "react";
import {ReactNode, useState} from "react";
import {setUserPrefs, UserPrefs} from "../../../utils/user_prefs.ts";
import MultiRangeSlider, {ChangeResult} from "multi-range-slider-react";
import "./GoalsSettingTile.scss";
import {auth} from "../../../utils/firebase.ts";
import { stopDragging } from "../../../components/transactions/CSVUpload.tsx";

export default function goalSettingTile(userPrefs: UserPrefs, forceUpdate: () => void): ReactNode {
const [minValue, setMinValue] = useState(
Expand All @@ -27,11 +28,6 @@ export default function goalSettingTile(userPrefs: UserPrefs, forceUpdate: () =>
setMaxValue(max);
};

const stop = (e: React.PointerEvent<HTMLDivElement>) => {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
}

return <>
<div className={"card-header w-100 fw-bold"}>
Goals
Expand Down Expand Up @@ -63,7 +59,7 @@ export default function goalSettingTile(userPrefs: UserPrefs, forceUpdate: () =>
</li>
<li className="list-group-item align-content-stretch" style={{height: "33%"}}>
<div className="w-100"
onPointerDown={stop}> {/* THIS LINE TOOK 1.5H */}
onPointerDown={stopDragging}> {/* THIS LINE TOOK 1.5H */}
<MultiRangeSlider
ruler={false}
label={false}
Expand Down

0 comments on commit b02c61b

Please sign in to comment.