From befae761384352dac683fe38c7dff4526d76921a Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 20:15:25 +0000
Subject: [PATCH 1/8] Remove Forgot Password option
---
src/components/{budget.js => Budget.js} | 0
src/pages/Login.jsx | 4 ++--
2 files changed, 2 insertions(+), 2 deletions(-)
rename src/components/{budget.js => Budget.js} (100%)
diff --git a/src/components/budget.js b/src/components/Budget.js
similarity index 100%
rename from src/components/budget.js
rename to src/components/Budget.js
diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx
index cbf6739..d23105a 100644
--- a/src/pages/Login.jsx
+++ b/src/pages/Login.jsx
@@ -84,9 +84,9 @@ export default function Login() {
Don't have an account? sign up
-
+ {/*
Forgot password?
-
+
*/}
From ec09fb8bf1bb60c666724eb862f880473bbeac7a Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 20:30:00 +0000
Subject: [PATCH 2/8] Fix session checking
---
src/utils/utils.js | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/src/utils/utils.js b/src/utils/utils.js
index d547956..68824fc 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,13 +1,26 @@
export function checkLogin() {
var cookies = document.cookie.split(';');
- for (var i = 0; i < cookies.length; i++) {
- var cookie = cookies[i].trim();
- if (cookie.indexOf('session=') === 0) {
+ if (getSessionKey() !== null) {
// The "session" cookie exists
- return true;
+ fetch('https://api.bb.gabefarrell.com/userinfo', {
+ method: 'GET',
+ headers: {
+ 'x-session-key': getSessionKey(),
+ },
+ })
+ .then(data => data.json())
+ .then((r) => {
+ if (r.status !== 200) {
+ // key is invalid
+ handleLogout()
+ return false
+ } else {
+ // key is OK
+ return true
+ }
+ })
}
- }
- // The "session" cookie doesn't exist
+ // The "session" cookie doesn't exist or is invalid
return false;
}
From 384f15bc8324084a9ecc118c47ac046e1285bc30 Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 20:38:42 +0000
Subject: [PATCH 3/8] navbar bug fix
---
src/utils/utils.js | 24 +++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 68824fc..ed72eda 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,24 +1,22 @@
-export function checkLogin() {
+export async function checkLogin() {
var cookies = document.cookie.split(';');
if (getSessionKey() !== null) {
// The "session" cookie exists
- fetch('https://api.bb.gabefarrell.com/userinfo', {
+ let result = await fetch('https://api.bb.gabefarrell.com/userinfo', {
method: 'GET',
headers: {
'x-session-key': getSessionKey(),
},
})
- .then(data => data.json())
- .then((r) => {
- if (r.status !== 200) {
- // key is invalid
- handleLogout()
- return false
- } else {
- // key is OK
- return true
- }
- })
+ result = await result.json()
+ if (result.status !== 200) {
+ // key is invalid
+ handleLogout()
+ return false
+ } else {
+ // key is OK
+ return true
+ }
}
// The "session" cookie doesn't exist or is invalid
return false;
From a65f6f09b14cc64f703a411483de9aacef7c9cc9 Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 20:54:30 +0000
Subject: [PATCH 4/8] Finish Month Expenses Widget
---
src/components/ExpenseTotal.js | 28 ++++++++++++++++++++--------
src/components/css/ExpenseTotal.css | 4 ++++
2 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/src/components/ExpenseTotal.js b/src/components/ExpenseTotal.js
index c8fe744..8e51540 100644
--- a/src/components/ExpenseTotal.js
+++ b/src/components/ExpenseTotal.js
@@ -1,19 +1,31 @@
-import React, { useContext } from 'react';
-import { AppContext } from '../context/AppContext';
+import React, { useState, useEffect } from 'react';
+import { getSessionKey } from "../utils/utils";
import logo from './widget_logos/expenses_logo.png';
import './css/ExpenseTotal.css'
const ExpenseTotal = () => {
- const { expenses } = useContext(AppContext);
+ const [expense, setExpense] = useState(0.0);
- const total = expenses.reduce((total, item) => {
- return (total += item.cost);
- }, 0);
+ useEffect(() => {
+ fetch('https://api.bb.gabefarrell.com/w/expenses/month', {
+ method: 'GET',
+ headers: {
+ 'x-session-key': getSessionKey(),
+ },
+ })
+ .then(data => data.json())
+ .then((data) => {
+ let monthExpense = data.whole
+ monthExpense += (data.decimal / 100)
+ setExpense(monthExpense)
+ })
+ .catch(console.log)
+ }, [])
return (
-

-
This Month's Expenses: ${total}
+

+
This Month's Expenses: ${expense}
);
};
diff --git a/src/components/css/ExpenseTotal.css b/src/components/css/ExpenseTotal.css
index e69de29..de22b4c 100644
--- a/src/components/css/ExpenseTotal.css
+++ b/src/components/css/ExpenseTotal.css
@@ -0,0 +1,4 @@
+.widgetText {
+ padding-left: 1em;
+ color: #333;
+}
\ No newline at end of file
From 0d0cbc4e79a3a106133f6e9ad565ca93e28b37e4 Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 21:20:37 -0400
Subject: [PATCH 5/8] add GET /w/income
---
widgets/income.go | 41 +++++++++++++++++++++++++++++++++++++++++
widgets/router.go | 1 +
2 files changed, 42 insertions(+)
diff --git a/widgets/income.go b/widgets/income.go
index 95cbd91..7c14d69 100644
--- a/widgets/income.go
+++ b/widgets/income.go
@@ -13,6 +13,11 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
+type IBCResponse struct {
+ Status int `json:"status"`
+ IncomeByCategory map[string]money.Money `json:"income_by_category"`
+}
+
func GetMonthIncome(w http.ResponseWriter, r *http.Request) {
// get session key from request
session := r.Header.Get("x-session-key")
@@ -53,3 +58,39 @@ func GetMonthIncome(w http.ResponseWriter, r *http.Request) {
w.Write(ret)
}
+
+func IncomeByCategory(w http.ResponseWriter, r *http.Request) {
+ // get session key from request
+ session := r.Header.Get("x-session-key")
+
+ // get collection handle from db
+ var userCollection = db.Client.Database("budgetbuddy").Collection("users")
+
+ var user = db.UserSchema{}
+
+ err := userCollection.FindOne(context.Background(), bson.D{primitive.E{Key: "session", Value: session}}).Decode(&user)
+ if err != nil {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprintf(w, "{\"error\":\"invalid session key\"}")
+ return
+ }
+
+ var response IBCResponse
+
+ response.IncomeByCategory = make(map[string]money.Money)
+ for _, e := range user.Income {
+ response.IncomeByCategory[e.Category] = money.Add(e.Amount, response.IncomeByCategory[e.Category])
+ }
+
+ response.Status = 200
+
+ ret, err := json.Marshal(response)
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprintf(w, "{\"error\":\"problem marshalling response\"}")
+ return
+ }
+
+ w.Write(ret)
+
+}
diff --git a/widgets/router.go b/widgets/router.go
index cdfed92..dfc58a1 100644
--- a/widgets/router.go
+++ b/widgets/router.go
@@ -24,6 +24,7 @@ func Router() *chi.Mux {
// income
r.Get("/income/month", GetMonthIncome)
+ r.Get("/income", IncomeByCategory)
return r
}
From 7b7abf5f6785819d1fa72f5ff4556e4ff276ddce Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Wed, 19 Apr 2023 01:12:10 +0000
Subject: [PATCH 6/8] fix another navbar bug
---
src/utils/utils.js | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/src/utils/utils.js b/src/utils/utils.js
index ed72eda..225f2a8 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,25 +1,28 @@
-export async function checkLogin() {
+export function checkLogin() {
var cookies = document.cookie.split(';');
if (getSessionKey() !== null) {
// The "session" cookie exists
- let result = await fetch('https://api.bb.gabefarrell.com/userinfo', {
+ fetch('https://api.bb.gabefarrell.com/userinfo', {
method: 'GET',
headers: {
'x-session-key': getSessionKey(),
},
})
- result = await result.json()
- if (result.status !== 200) {
- // key is invalid
- handleLogout()
- return false
- } else {
- // key is OK
- return true
- }
+ .then(data => data.json())
+ .then((result) => {
+ if (result.status !== 200) {
+ // key is invalid
+ handleLogout()
+ return false
+ } else {
+ // key is OK
+ return true
+ }
+ })
+ } else {
+ // The "session" cookie doesn't exist or is invalid
+ return false;
}
- // The "session" cookie doesn't exist or is invalid
- return false;
}
export function getSessionKey() {
From d2312648e7df01b43beb947246ddd6264a30dd93 Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 21:49:33 -0400
Subject: [PATCH 7/8] abandoned valid login check
---
src/utils/utils.js | 21 ++-------------------
1 file changed, 2 insertions(+), 19 deletions(-)
diff --git a/src/utils/utils.js b/src/utils/utils.js
index 225f2a8..c301b6c 100644
--- a/src/utils/utils.js
+++ b/src/utils/utils.js
@@ -1,26 +1,9 @@
export function checkLogin() {
- var cookies = document.cookie.split(';');
if (getSessionKey() !== null) {
// The "session" cookie exists
- fetch('https://api.bb.gabefarrell.com/userinfo', {
- method: 'GET',
- headers: {
- 'x-session-key': getSessionKey(),
- },
- })
- .then(data => data.json())
- .then((result) => {
- if (result.status !== 200) {
- // key is invalid
- handleLogout()
- return false
- } else {
- // key is OK
- return true
- }
- })
+ return true
} else {
- // The "session" cookie doesn't exist or is invalid
+ // The "session" cookie doesn't exist
return false;
}
}
From 3873bd1cdc614853e35bc7a08f91223b358aa0bd Mon Sep 17 00:00:00 2001
From: Gabe Farrell
Date: Tue, 18 Apr 2023 21:56:24 -0400
Subject: [PATCH 8/8] update API doc
---
API.md | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/API.md b/API.md
index 5201101..b7e0b8c 100644
--- a/API.md
+++ b/API.md
@@ -248,3 +248,20 @@ Response:
"decimal": int,
}
```
+## GET /income
+Get the total amount of income in each category
+
+Response:
+```json
+{
+ "status": int,
+ "income_by_category": {
+ "example_category": {
+ "currency": string,
+ "whole": int,
+ "decimal": int,
+ },
+ ...
+ }
+}
+```