Initial commit: Harat's Booking monorepo with deploy setup
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { create } from 'zustand';
|
||||
|
||||
interface BookingDish {
|
||||
dishId: number;
|
||||
title: string;
|
||||
price: number;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface BookingState {
|
||||
pubId: number | null;
|
||||
tableId: number | null;
|
||||
date: string;
|
||||
time: string;
|
||||
numPeople: number;
|
||||
includeDishes: boolean;
|
||||
dishes: BookingDish[];
|
||||
|
||||
setPubId: (id: number) => void;
|
||||
setTableId: (id: number | null) => void;
|
||||
setDate: (date: string) => void;
|
||||
setTime: (time: string) => void;
|
||||
setNumPeople: (n: number) => void;
|
||||
setIncludeDishes: (v: boolean) => void;
|
||||
addDish: (dish: BookingDish) => void;
|
||||
removeDish: (dishId: number) => void;
|
||||
updateDishQty: (dishId: number, qty: number) => void;
|
||||
getTotalCost: () => number;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
const initialState = {
|
||||
pubId: null as number | null,
|
||||
tableId: null as number | null,
|
||||
date: '',
|
||||
time: '',
|
||||
numPeople: 2,
|
||||
includeDishes: false,
|
||||
dishes: [] as BookingDish[],
|
||||
};
|
||||
|
||||
export const useBookingStore = create<BookingState>((set, get) => ({
|
||||
...initialState,
|
||||
|
||||
setPubId: (id) => set({ pubId: id, tableId: null }),
|
||||
setTableId: (id) => set({ tableId: id }),
|
||||
setDate: (date) => set({ date, tableId: null }),
|
||||
setTime: (time) => set({ time, tableId: null }),
|
||||
setNumPeople: (n) => set({ numPeople: n }),
|
||||
setIncludeDishes: (v) => set({ includeDishes: v, dishes: v ? get().dishes : [] }),
|
||||
|
||||
addDish: (dish) => {
|
||||
const existing = get().dishes.find(d => d.dishId === dish.dishId);
|
||||
if (existing) {
|
||||
set({
|
||||
dishes: get().dishes.map(d =>
|
||||
d.dishId === dish.dishId ? { ...d, quantity: d.quantity + 1 } : d
|
||||
),
|
||||
});
|
||||
} else {
|
||||
set({ dishes: [...get().dishes, dish] });
|
||||
}
|
||||
},
|
||||
|
||||
removeDish: (dishId) => {
|
||||
set({ dishes: get().dishes.filter(d => d.dishId !== dishId) });
|
||||
},
|
||||
|
||||
updateDishQty: (dishId, qty) => {
|
||||
if (qty <= 0) {
|
||||
set({ dishes: get().dishes.filter(d => d.dishId !== dishId) });
|
||||
} else {
|
||||
set({
|
||||
dishes: get().dishes.map(d =>
|
||||
d.dishId === dishId ? { ...d, quantity: qty } : d
|
||||
),
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
getTotalCost: () => {
|
||||
return get().dishes.reduce((sum, d) => sum + d.price * d.quantity, 0);
|
||||
},
|
||||
|
||||
reset: () => set(initialState),
|
||||
}));
|
||||
Reference in New Issue
Block a user