Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 状態の重複が発生しないようにする (chair status更新前に状態をチェック後に実施) #789

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions frontend/app/components/hooks/use-emulator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect } from "react";
import { apiBaseURL } from "~/api/api-base-url";
import {
ChairGetNotificationResponse,
fetchChairPostCoordinate,
fetchChairPostRideStatus,
} from "~/api/api-components";
Expand Down Expand Up @@ -37,14 +39,53 @@ const move = (
}
};

function jsonFromSseResult<T>(value: string) {
const data = value.slice("data:".length).trim();
return JSON.parse(data) as T;
}

const notificationFetch = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nits) internalな関数なのでgetCurrentRideStatus にして、以下を中で実行しても良さそうに思います

const getStatus = async () => {
  const notification = await notificationFetch();
  return notification?.data?.status;
};

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<ChairGetNotificationResponse["data"]>(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({
body: 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" },
Expand All @@ -54,7 +95,10 @@ 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" },
pathParams: {
Expand Down