import {React, useState} from "react"; import NavBar from "../nav-bar/nav-bar"; import axios from "axios"; import "./budget-tracker.css" const BudgetTracker = () => { var curr_event_budget = window.localStorage.getItem("curr_event_budget"); var curr_event_spent = parseInt(window.localStorage.getItem("curr_event_spent")); var event_spent_desc = JSON.parse(window.localStorage.getItem("spent_desc")); var email = window.localStorage.getItem("email"); var curr_event = window.localStorage.getItem("curr_event"); const [expense, setExpense] = useState("") const [expenseDesc, setExpenseDesc] = useState("") const [expenseOver, setExpenseOver] = useState("False") const [spent, setSpent] = useState(curr_event_spent) const [expenseList, setExpenseList] = useState(event_spent_desc) // delete from event_spent and array of expenses in backend const handleDeleteExpense = (e) => { var desc_and_expense = e.target.value.split(','); e.preventDefault(); // confirm if user wants to delete expense var result = window.confirm("Are you sure you want to delete this expense?"); // if confirmed if (result === true) { // negative because we do curr_spent + (negative) = less curr_spent var new_expense = desc_and_expense[1] * -1; var expenseDesc = desc_and_expense[0]; const config = { method: "post", url: "http://localhost:3001/change-event-spent", data: { curr_event, email, new_expense, expenseDesc, }, }; axios(config) .then((result) => { // update expenses list window.localStorage.setItem('spent_desc', JSON.stringify(result.data.event_spent_desc)); setExpenseList(result.data.event_spent_desc); console.log("updated expense list", result.data.event_spent_desc); // update spent window.localStorage.setItem('curr_event_spent', parseInt(result.data.event_spent)); setSpent(parseInt(result.data.event_spent)); }) .catch((error) => { console.log(error); }); } } // make list of expenses already in DB var expenses_list = []; for (var i = 0; i < expenseList.length; i++) { expenses_list.push(
Budget: ${curr_event_budget}
Spent: ${spent}