From e43415c08ef4a6ea9369e93234c28af1bb2be09d Mon Sep 17 00:00:00 2001 From: imamiya-masaki Date: Sat, 7 Dec 2024 23:46:56 +0900 Subject: [PATCH 1/2] cherry-pick --- frontend/app/components/hooks/use-emulator.ts | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/frontend/app/components/hooks/use-emulator.ts b/frontend/app/components/hooks/use-emulator.ts index b750ac4a..07b83bf6 100644 --- a/frontend/app/components/hooks/use-emulator.ts +++ b/frontend/app/components/hooks/use-emulator.ts @@ -1,5 +1,7 @@ import { useEffect } from "react"; +import { apiBaseURL } from "~/api/api-base-url"; import { + ChairGetNotificationResponse, fetchChairPostCoordinate, fetchChairPostRideStatus, } from "~/api/api-components"; @@ -37,6 +39,42 @@ const move = ( } }; +function jsonFromSseResult(value: string) { + const data = value.slice("data:".length).trim(); + return JSON.parse(data) as T; +} + +const notificationFetch = async () => { + try { + const notification = await fetch(`${apiBaseURL}/chair/notification`); + const isEventStream = !!notification?.headers + .get("Content-type") + ?.split(";")?.[0] + .includes("text/event-stream"); + + if (isEventStream) { + const reader = notification.body?.getReader(); + const decoder = new TextDecoder(); + const readed = (await reader?.read())?.value; + const decoded = decoder.decode(readed); + const json = + jsonFromSseResult(decoded); + return { data: json }; + } + const json = (await notification.json()) as + | ChairGetNotificationResponse + | undefined; + return json; + } catch (error) { + console.error(error); + } +}; + +const getStatus = async () => { + const notification = await notificationFetch(); + return notification?.data?.status; +}; + const currentCoodinatePost = (coordinate: Coordinate) => { setSimulatorCurrentCoordinate(coordinate); return fetchChairPostCoordinate({ @@ -44,7 +82,10 @@ const currentCoodinatePost = (coordinate: Coordinate) => { }); }; -const postEnroute = (rideId: string, coordinate: Coordinate) => { +const postEnroute = async (rideId: string, coordinate: Coordinate) => { + if ((await getStatus()) !== "MATCHING") { + return; + } setSimulatorStartCoordinate(coordinate); return fetchChairPostRideStatus({ body: { status: "ENROUTE" }, @@ -54,9 +95,12 @@ const postEnroute = (rideId: string, coordinate: Coordinate) => { }); }; -const postCarring = (rideId: string) => { +const postCarring = async (rideId: string) => { + if ((await getStatus()) !== "PICKUP") { + return; + } return fetchChairPostRideStatus({ - body: { status: "CARRYING" }, + body: { status: "CsRRYING" }, pathParams: { rideId, }, From 63119e316cf0c9d88f1ae7c3c47283490568b93d Mon Sep 17 00:00:00 2001 From: imamiya-masaki <63019386+imamiya-masaki@users.noreply.github.com> Date: Sat, 7 Dec 2024 23:51:01 +0900 Subject: [PATCH 2/2] Update frontend/app/components/hooks/use-emulator.ts Co-authored-by: narirou --- frontend/app/components/hooks/use-emulator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/components/hooks/use-emulator.ts b/frontend/app/components/hooks/use-emulator.ts index 07b83bf6..f2055e30 100644 --- a/frontend/app/components/hooks/use-emulator.ts +++ b/frontend/app/components/hooks/use-emulator.ts @@ -100,7 +100,7 @@ const postCarring = async (rideId: string) => { return; } return fetchChairPostRideStatus({ - body: { status: "CsRRYING" }, + body: { status: "CARRYING" }, pathParams: { rideId, },