Skip to content

Commit

Permalink
Update order API
Browse files Browse the repository at this point in the history
  • Loading branch information
zntb committed Mar 4, 2024
1 parent 3b84406 commit 5c4bd31
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/api/MyRestaurantApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

0 comments on commit 5c4bd31

Please sign in to comment.