Skip to content

Commit d81e8cb

Browse files
authored
Merge pull request #11 from UTT-GL03/add-expense
Add expense
2 parents 10617cb + 0553537 commit d81e8cb

6 files changed

Lines changed: 193 additions & 103 deletions

File tree

docker-compose.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@ services:
3737
index:
3838
image: curlimages/curl
3939
entrypoint: ["/bin/sh","-c"]
40-
volumes:
41-
- ./frontend/g
40+
volumes:
41+
- ./frontend/public/data.json:/data.json
42+
command:
43+
- |
44+
curl -X POST http://backend:5984/finary/_bulk_docs \
45+
-H "Content-Type: application/json" \
46+
-d @/data.json
47+
depends_on:
48+
accessible_backend:
49+
condition: service_completed_successfully
4250

4351
index_created_at:
4452
image: curlimages/curl

frontend/package-lock.json

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"chart.js": "^4.5.1",
1515
"react": "^19.1.1",
1616
"react-dom": "^19.1.1",
17-
"react-router": "^7.9.4"
17+
"react-router": "^7.9.4",
18+
"uuid": "^13.0.0"
1819
},
1920
"devDependencies": {
2021
"@eslint/js": "^9.36.0",
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { useState } from "react";
2+
import { v4 as uuidv4 } from "uuid";
3+
4+
export function AddMoney({ open, setOpen, transactionType }) {
5+
const [amount, setAmount] = useState("");
6+
const [category, setCategory] = useState("");
7+
const [date, setDate] = useState("");
8+
9+
10+
const [loading, setLoading] = useState(false);
11+
12+
13+
const handleSubmit = async () => {
14+
//setOpen(false);
15+
console.log(amount, category, date);
16+
setLoading(true);
17+
const amountNumber = transactionType === "outcome" ? Number(-amount) : Number(amount);
18+
const response = await fetch("http://localhost:5984/finary", {
19+
method: "POST",
20+
headers: {
21+
"Content-Type": "application/json",
22+
Authorization: "Basic " + btoa("admin:secret"),
23+
},
24+
body: JSON.stringify({
25+
id: uuidv4(),
26+
amount: amountNumber,
27+
category,
28+
created_at: date,
29+
user_id: "10",
30+
}),
31+
});
32+
const result = await response.json();
33+
console.log(result);
34+
setLoading(false);
35+
setOpen(false);
36+
}
37+
return (
38+
<>
39+
<button onClick={() => setOpen(true)}>+ Add</button>
40+
<dialog open={open}>
41+
<article>
42+
<h2>Add {transactionType}</h2>
43+
<p>
44+
Please enter the {transactionType} details below:
45+
</p>
46+
<ul>
47+
<label>Amount: </label>
48+
<input type="number" value={amount} onChange={(e) => setAmount(e.target.value)} />
49+
<label>Category: </label>
50+
<input type="text" value={category} onChange={(e) => setCategory(e.target.value)} />
51+
<label>Date: </label>
52+
<input type="date" value={date} onChange={(e) => setDate(e.target.value)} />
53+
54+
</ul>
55+
<footer>
56+
<button class="secondary" onClick={() => setOpen(false)}>
57+
Cancel
58+
</button>
59+
<button onClick={() => handleSubmit()} disabled={loading}>{loading ? "Loading..." : "Confirm"}</button>
60+
</footer>
61+
</article>
62+
</dialog>
63+
</>
64+
);
65+
}

frontend/src/components/incomes.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useState } from "react";
22
import { LineChart } from "./line-chart";
33
import { CustomTable } from "./custom-table";
4+
import { AddMoney } from "./add-money";
45

56
export function Incomes() {
67
const [data, setData] = useState(null);
@@ -11,6 +12,8 @@ export function Incomes() {
1112
const [incomeChartData, setIncomeChartData] = useState(null);
1213
const [tableData, setTableData] = useState(null);
1314

15+
const [dialogOpen, setDialogOpen] = useState(false);
16+
1417
useEffect(() => {
1518
const currentTime = new Date().toISOString();
1619
const fetchData = async () => {
@@ -111,8 +114,10 @@ export function Incomes() {
111114

112115
return (
113116
<div style={{ padding: "2rem" }}>
114-
<h1>Incomes</h1>
115-
117+
<div style={{ display: "flex", justifyContent: "space-between" }}>
118+
<h1>Incomes</h1>
119+
<AddMoney open={dialogOpen} setOpen={setDialogOpen} transactionType="income" />
120+
</div>
116121
<LineChart chartData={incomeChartData} />
117122
<CustomTable tableData={tableData} />
118123
<button onClick={handleLoadMore}>Load More</button>

0 commit comments

Comments
 (0)