diff --git a/src/components/pages/Expense/Common/ExpenseEditPage.js b/src/components/pages/Expense/Common/ExpenseEditPage.js index 34e247e..766d538 100644 --- a/src/components/pages/Expense/Common/ExpenseEditPage.js +++ b/src/components/pages/Expense/Common/ExpenseEditPage.js @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react"; import { useLocation, useNavigate } from "react-router-dom"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowLeft } from "@fortawesome/free-solid-svg-icons"; -import "../Common/ExpenseEditPage.css" +import "../Common/ExpenseEditPage.css"; function ExpenseEditPage() { const location = useLocation(); @@ -17,14 +17,27 @@ function ExpenseEditPage() { const now = new Date(); const formattedDateTime = now.toLocaleString(); setCurrentDateTime(formattedDateTime); - }, []); + + + if (!location.state?.id) { + const storedEntries = JSON.parse(localStorage.getItem("entries")) || []; + const entryToEdit = storedEntries.find( + (entry) => entry.id === location.state?.id + ); + + if (entryToEdit) { + setAmount(entryToEdit.amount); + setCategory(entryToEdit.category); + } + } + }, [location.state]); const handleBackClick = () => { navigate("/expense-records"); }; const handleSave = () => { - setIsLoading(true); + setIsLoading(true); setError(null); const updatedEntry = { @@ -34,16 +47,14 @@ function ExpenseEditPage() { category: category, }; - const storedEntries = JSON.parse(localStorage.getItem("entries")) || []; const updatedEntries = storedEntries.map((entry) => entry.id === updatedEntry.id ? updatedEntry : entry ); localStorage.setItem("entries", JSON.stringify(updatedEntries)); - - console.log("Entry updated in local storage:", updatedEntry); - navigate("/expense-successfully"); + // console.log("Entry updated in local storage:", updatedEntry); + navigate("/expense-successfully", { state: updatedEntry }); setIsLoading(false); }; diff --git a/src/components/pages/Expense/ExpenseSuccessfully/ExpenseSuccessfully.js b/src/components/pages/Expense/ExpenseSuccessfully/ExpenseSuccessfully.js index 6efab2d..5f0a51d 100644 --- a/src/components/pages/Expense/ExpenseSuccessfully/ExpenseSuccessfully.js +++ b/src/components/pages/Expense/ExpenseSuccessfully/ExpenseSuccessfully.js @@ -1,16 +1,16 @@ import React, { useEffect, useState } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowLeft } from "@fortawesome/free-solid-svg-icons"; -import { useNavigate , useLocation } from "react-router-dom"; +import { useNavigate, useLocation } from "react-router-dom"; import { Player } from "@lottiefiles/react-lottie-player"; import successAnimation from "../Common/Animation - 1727464749500.json"; -import "../ExpenseSuccessfully/ExpenseSuccessfully.css" +import "../ExpenseSuccessfully/ExpenseSuccessfully.css"; function ExpenseSuccessfully() { const [currentDate, setCurrentDate] = useState(new Date()); const navigate = useNavigate(); const location = useLocation(); - const entryType = location.state?.entryType; + const entryType = location.state?.entryType; useEffect(() => { const timer = setInterval(() => { @@ -21,22 +21,26 @@ function ExpenseSuccessfully() { }, []); const handleBackClick = () => { - navigate("/expense-editPage" , { state: { entryType } }); + const updatedEntry = { + id: location.state?.id, + amount: location.state?.amount, + category: location.state?.category, + dateTime: location.state?.dateTime, + }; + + navigate("/expense-editPage", { state: updatedEntry }); }; const handleBackToHome = () => { - navigate("/" , { state: { entryType } }); + navigate("/", { state: { entryType } }); }; const handleBackToRecord = () => { - navigate("/expense-records" , { state: { entryType } }) - } + navigate("/expense-records", { state: { entryType } }); + }; const options = { hour: "2-digit", minute: "2-digit", hour12: true }; - const formattedDate = `on ${currentDate.toLocaleDateString()} - ${currentDate.toLocaleTimeString( - [], - options - )}`; + const formattedDate = `on ${currentDate.toLocaleDateString()} - ${currentDate.toLocaleTimeString([], options)}`; return (