From a0f04051843478396bb9c6f21648728de198bcc0 Mon Sep 17 00:00:00 2001 From: Mahyar Khanbolooki Date: Mon, 7 Jul 2025 21:15:11 +0330 Subject: [PATCH] add: receipt store functionality to menu items --- src/app/menu/page.tsx | 138 +++++++++++++++++++++++++----------- src/zustand/receiptStore.ts | 52 ++++++++++++++ 2 files changed, 148 insertions(+), 42 deletions(-) create mode 100644 src/zustand/receiptStore.ts diff --git a/src/app/menu/page.tsx b/src/app/menu/page.tsx index 580464b..445b167 100644 --- a/src/app/menu/page.tsx +++ b/src/app/menu/page.tsx @@ -1,6 +1,6 @@ 'use client'; -import { Fragment, useCallback, useState } from "react"; +import { Fragment, useCallback, useMemo, useState } from "react"; import SearchBox from "@/components/input/SearchBox"; import CategoryItemRenderer from "@/components/listview/CategoryItemRenderer"; import HorizontalScrollView from "@/components/listview/HorizontalScrollView"; @@ -11,6 +11,7 @@ import PlusIcon from "@/components/icons/PlusIcon"; import MinusIcon from "@/components/icons/MinusIcon"; import VerticalScrollView from "@/components/listview/VerticalScrollView"; import MenuItemRenderer from "@/components/listview/MenuItemRenderer"; +import { useReceiptStore } from "@/zustand/receiptStore"; const categories = [ { @@ -140,9 +141,8 @@ const foods = [ contains: 'مرغ / کاهو / گوجه / خیار' }, ]; - -export default function MenuIndex() { - const [search, setSearch] = useState(''); +const MenuIndex = () => { + const [search, setSearch] = useState(""); const [selectedCategory, setSelectedCategory] = useState(0); const updateSearch = useCallback((e: React.ChangeEvent) => { @@ -153,32 +153,52 @@ export default function MenuIndex() { setSelectedCategory(id); }, []); + const incrementItem = useReceiptStore((state) => state.increment); + const decrementItem = useReceiptStore((state) => state.decrement); + const receiptItems = useReceiptStore((state) => state.items); + + const filteredReceiptItems = useMemo(() => { + return foods.filter((item) => item.category === selectedCategory); + }, [selectedCategory]); return (
- { - return updateCategory(index)} key={index}>{child} - }}> + { + return ( + updateCategory(index)} + key={index} + > + {child} + + ); + }} + > {categories.map((item, index) => { return (
category image {item.title}
- ) + ); })}
- {categories[selectedCategory].title} + + {categories[selectedCategory].title} +
+ ) : ( + <> + +
+ {(receiptItem?.quantity || 0)} +
+ + + )} +
- - - ) - })} - + + ); + })} - - ); -} +}; + +export default MenuIndex; diff --git a/src/zustand/receiptStore.ts b/src/zustand/receiptStore.ts new file mode 100644 index 0000000..70d80e9 --- /dev/null +++ b/src/zustand/receiptStore.ts @@ -0,0 +1,52 @@ +// zustand/userStore.ts +import { create } from 'zustand' +import { createJSONStorage, persist } from 'zustand/middleware' + +type ReceiptItem = { + id: number + quantity: number +} + +type AuthStore = { + items: Array + increment: (id: number) => void + decrement: (id: number) => void + clear: () => void +} + +export const useReceiptStore = create()( + persist( + (set) => ({ + items: [], + clear: () => + set(() => ({ + items: [] + })), + increment: (id) => + set((state) => { + if (state.items.find(x => x.id === id) === undefined) { + return ({ + items: [...state.items, {id, quantity: 1}] + }) + } + return ({ + items: state.items.map((item) => + item.id === id ? { ...item, quantity: item.quantity + 1 } : item + ), + }) + }), + decrement: (id) => + set((state) => ({ + items: state.items.map((item) => + item.id === id && item.quantity > 0 + ? { ...item, quantity: item.quantity - 1 } + : item + ), + })), + }), + { + name: 'receipt-storage', // Key in localStorage + storage: createJSONStorage(() => sessionStorage), + } + ) +)