simple-kiosk-app/hooks/useOrder.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-04-20 19:58:18 +09:00
import { useReducer } from "react";
export interface Order {
name: string;
quantity: number;
HOT: boolean;
price: number;
};
interface OrderState {
orders: Order[];
};
const initialState = {
orders: [],
} as OrderState;
export type OrderAction =
| { type: "ADD_ORDER"; order: Order }
| { type: "REMOVE_ORDER"; order: Order }
| { type: "UPDATE_ORDER"; order: Order };
function compareOrder(a: Order, b: Order) {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
if (a.HOT && !b.HOT) {
return -1;
}
if (!a.HOT && b.HOT) {
return 1;
}
return 0;
}
function reducer(state: OrderState, action: OrderAction): OrderState {
switch (action.type) {
case "ADD_ORDER":
if (state.orders.find(order => compareOrder(order, action.order) === 0)) {
return {
orders: state.orders.map(order => compareOrder(order, action.order) === 0 ? {
...order,
quantity: Math.max(order.quantity + action.order.quantity, 0)
} : order),
};
}
return {
orders: [...state.orders, action.order],
};
case "REMOVE_ORDER":
return {
orders: state.orders.filter(order => compareOrder(order, action.order) !== 0),
};
case "UPDATE_ORDER":
return {
orders: state.orders.map(order => compareOrder(order, action.order) === 0 ? action.order : order),
};
default:
return state;
}
}
export function useOrder() {
const [state, dispatch] = useReducer(reducer, initialState);
return { state, dispatch };
}