|
1 | | -import React from "react"; |
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { useLocation } from "react-router-dom"; // Import useLocation |
2 | 3 | import { UnifiedHeader } from "../../components/UnifiedHeader"; |
3 | 4 | import { Community } from "./sections/Community"; |
4 | 5 | import { Footer } from "./sections/Footer"; |
5 | 6 | import { IngredientInventory } from "./sections/IngredientInventory"; |
6 | 7 | import { MenuInventory } from "./sections/MenuInventory"; |
7 | 8 | import "./style.css"; |
8 | 9 |
|
| 10 | +const INITIAL_INGREDIENTS = [ |
| 11 | + { id: 1, name: "양파", category: "채소류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-1.png", expiryDays: 5 }, |
| 12 | + { id: 2, name: "닭고기", category: "육류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 3 }, |
| 13 | + { id: 3, name: "고추장", category: "가공류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 30 }, |
| 14 | + { id: 4, name: "고춧가루", category: "가공류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 60 }, |
| 15 | + { id: 5, name: "양배추", category: "채소류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 7 }, |
| 16 | + { id: 6, name: "스테비아", category: "가공류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 90 }, |
| 17 | + { id: 7, name: "마늘", category: "채소류", image: "https://c.animaapp.com/sjWITF5i/img/ingredientimage-7@2x.png", expiryDays: 14 }, |
| 18 | +]; |
| 19 | + |
9 | 20 | export const Desktop = () => { |
| 21 | + const [ingredients, setIngredients] = useState([]); |
| 22 | + const location = useLocation(); // Initialize useLocation |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + // This effect will run on mount and when location changes, |
| 26 | + // ensuring fresh data when returning from AddIngredientPage |
| 27 | + const storedIngredients = JSON.parse(localStorage.getItem("userIngredients") || "[]"); |
| 28 | + if (storedIngredients.length > 0) { |
| 29 | + setIngredients(storedIngredients); |
| 30 | + } else { |
| 31 | + setIngredients(INITIAL_INGREDIENTS); |
| 32 | + localStorage.setItem("userIngredients", JSON.stringify(INITIAL_INGREDIENTS)); |
| 33 | + } |
| 34 | + }, [location.pathname]); // Re-run when pathname changes |
| 35 | + |
| 36 | + const handleAddIngredient = (newIngredient) => { |
| 37 | + setIngredients((prevIngredients) => { |
| 38 | + const updatedIngredients = [...prevIngredients, newIngredient]; |
| 39 | + localStorage.setItem("userIngredients", JSON.stringify(updatedIngredients)); |
| 40 | + // TODO: Backend Integration: Send newIngredient to backend |
| 41 | + console.log("Added ingredient to mock DB:", newIngredient); |
| 42 | + return updatedIngredients; |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + const handleAddMultipleIngredients = (newIngredients) => { |
| 47 | + setIngredients((prevIngredients) => { |
| 48 | + const updatedIngredients = [...prevIngredients, ...newIngredients]; |
| 49 | + localStorage.setItem("userIngredients", JSON.stringify(updatedIngredients)); |
| 50 | + // TODO: Backend Integration: Send newIngredients to backend |
| 51 | + console.log("Added multiple ingredients to mock DB:", newIngredients); |
| 52 | + return updatedIngredients; |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
10 | 56 | return ( |
11 | 57 | <div className="desktop" data-model-id="45:2"> |
12 | 58 | <UnifiedHeader /> |
13 | 59 | <div className="site-header-name"> |
14 | 60 | <div className="choose-your-receipt">오늘의 메뉴를 골라보세요</div> |
15 | 61 | </div> |
16 | 62 |
|
17 | | - <IngredientInventory /> |
| 63 | + <IngredientInventory |
| 64 | + ingredients={ingredients} |
| 65 | + onAddIngredient={handleAddIngredient} |
| 66 | + onAddMultipleIngredients={handleAddMultipleIngredients} |
| 67 | + /> |
18 | 68 | <MenuInventory /> |
19 | 69 | <Community /> |
20 | 70 | <Footer /> |
|
0 commit comments