diff --git a/src/app/admin/manage/order/[[...orderId]]/page.tsx b/src/app/admin/manage/order/[[...orderId]]/page.tsx index a802ca5..3d2206f 100644 --- a/src/app/admin/manage/order/[[...orderId]]/page.tsx +++ b/src/app/admin/manage/order/[[...orderId]]/page.tsx @@ -115,6 +115,33 @@ const Page = ({ params }: { params: { orderId: string } }) => { .catch(error => setError(error.message)); } + const setOrderAsPaid = (_id: string, isPaid: boolean) => { + fetch(`/api/order/${_id}/pay`, { + method: 'PUT', + headers: headers, + body: JSON.stringify({ isPaid: isPaid }) + }) + .then(async response => { + const data = await response.json(); + if (!response.ok) { + const error = (data && data.message) || response.statusText; + throw new Error(error); + } + return data; + }) + .then(() => { + // Update the order by id + const newOrders = orders.map((order: OrderDocument) => { + if (order._id === _id) { + order.isPaid = isPaid; + } + return order; + }); + setOrders(newOrders); + }) + .catch(error => setError(error.message)); + } + /** * Function to convert barcode to order * @param barcode @@ -217,7 +244,7 @@ const Page = ({ params }: { params: { orderId: string } }) => { {food.name}
{food.dietary && {food.dietary}} + className="px-2 py-0.5 text-xs font-semibold text-white bg-blue-500 rounded-full">{food.dietary}} {food.type}
@@ -238,6 +265,14 @@ const Page = ({ params }: { params: { orderId: string } }) => { {state} ))} + ))} diff --git a/src/app/admin/prepare/page.jsx b/src/app/admin/prepare/page.jsx index 45db27e..1d2b01e 100644 --- a/src/app/admin/prepare/page.jsx +++ b/src/app/admin/prepare/page.jsx @@ -3,8 +3,7 @@ import {useEffect, useState} from "react"; import {getFromLocalStorage} from "@/lib/localStorage"; import ErrorMessage from "@/app/components/ErrorMessage.jsx"; -import {getDateFromTimeSlot} from "@/lib/time"; -import {formatDateTime} from "@/lib/time"; +import {formatDateTime, getDateFromTimeSlot} from "@/lib/time"; import WithAuth from "../WithAuth.jsx"; const getHeaders = () => { @@ -98,6 +97,9 @@ const Page = () => { newOrders[orderIndex] = newOrder; setOrders(newOrders); }, + undone: (_id) => { + updateOrderStatus(_id, 'pending') + }, done: (_id) => { updateOrderStatus(_id, 'baking') }, @@ -127,17 +129,17 @@ const Page = () => {
-
+
+ className="inline-flex items-center px-2.5 py-0.5 m-0.5 rounded-full text-sm font-medium bg-gray-100 text-gray-800"> {order._id} + className="inline-flex items-center px-2.5 py-0.5 m-0.5 rounded-full text-sm font-medium bg-gray-100 text-gray-800"> {order.name} @@ -182,6 +184,10 @@ const Page = () => { + } + }
diff --git a/src/app/page.jsx b/src/app/page.jsx index 01afaba..12a9dc2 100644 --- a/src/app/page.jsx +++ b/src/app/page.jsx @@ -160,10 +160,10 @@ const Page = () => { /** * Set the name of the order - * @param e + * @param name */ - const setName = (e) => { - updateOrder({name: e.target.value}); + const setName = (name) => { + updateOrder({name: name}); }; /** diff --git a/src/model/order.ts b/src/model/order.ts index b71ee9c..069eddd 100644 --- a/src/model/order.ts +++ b/src/model/order.ts @@ -1,29 +1,10 @@ import { type Document, Model, model, Schema } from "mongoose"; import { FoodDocument } from "./food"; -import { ORDER } from "@/config"; -export type OrderStatus = 'pending' | 'paid' | 'baking' | 'ready' | 'delivered' | 'cancelled'; -export const ORDER_STATES: OrderStatus[] = ['pending', 'paid', 'baking', 'ready', 'delivered', 'cancelled']; +export type OrderStatus = 'pending' | 'baking' | 'ready' | 'delivered' | 'cancelled'; +export const ORDER_STATES: OrderStatus[] = ['pending', 'baking', 'ready', 'delivered', 'cancelled']; -export const statusToText = (status: string) => { - if (status === 'ready') { - return 'Your order is ready for pickup!'; - } else if (status === 'pending') { - return 'Please pay at the counter.'; - } else if (status === 'baking') { - return 'Its getting hot 🔥:)'; - } else if (status === 'paid') { - return 'Your order is being prepared...'; - } else if (status === 'delivered') { - return 'Your order has been delivered!'; - } else if (status === 'cancelled') { - return 'Your order has been cancelled.'; - } else { - return 'Unknown status'; - } -} - export interface OrderDocument extends Document { _id: string; name: string; @@ -33,6 +14,7 @@ export interface OrderDocument extends Document { timeslot: string; totalPrice: number; finishedAt?: Date; + isPaid: boolean; status: OrderStatus; } @@ -68,6 +50,10 @@ const orderSchema = new Schema({ finishedAt: { type: Date, }, + isPaid: { + type: Boolean, + default: false, + }, status: { type: String, enum: ORDER_STATES,