add: receipt store functionality to menu items
This commit is contained in:
@@ -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<ReceiptItem>
|
||||
increment: (id: number) => void
|
||||
decrement: (id: number) => void
|
||||
clear: () => void
|
||||
}
|
||||
|
||||
export const useReceiptStore = create<AuthStore>()(
|
||||
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),
|
||||
}
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user