From 5c4bd31b40dafff9420a79a553ab8e182503b221 Mon Sep 17 00:00:00 2001 From: zntb Date: Mon, 4 Mar 2024 14:37:12 +0200 Subject: [PATCH] Update order API --- src/api/MyRestaurantApi.tsx | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/api/MyRestaurantApi.tsx b/src/api/MyRestaurantApi.tsx index 50b80c6..7f784bd 100644 --- a/src/api/MyRestaurantApi.tsx +++ b/src/api/MyRestaurantApi.tsx @@ -141,3 +141,55 @@ export const useGetMyRestaurantOrders = () => { return { orders, isLoading }; }; + +type UpdateOrderStatusRequest = { + orderId: string; + status: string; +}; + +export const useUpdateMyRestaurantOrder = () => { + const { getAccessTokenSilently } = useAuth0(); + + const updateMyRestaurantOrder = async ( + updateStatusOrderRequest: UpdateOrderStatusRequest + ) => { + const accessToken = await getAccessTokenSilently(); + + const response = await fetch( + `${API_BASE_URL}/api/my/restaurant/order/${updateStatusOrderRequest.orderId}/status`, + { + method: 'PATCH', + headers: { + Authorization: `Bearer ${accessToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ status: updateStatusOrderRequest.status }), + } + ); + + if (!response.ok) { + throw new Error('Failed to update status'); + } + + return response.json(); + }; + + const { + mutateAsync: updateRestaurantStatus, + isLoading, + isError, + isSuccess, + reset, + } = useMutation(updateMyRestaurantOrder); + + if (isSuccess) { + toast.success('Order updated'); + } + + if (isError) { + toast.error('Unable to update order'); + reset(); + } + + return { updateRestaurantStatus, isLoading }; +};