diff --git a/frontend/app/contexts/owner-context.tsx b/frontend/app/contexts/owner-context.tsx index e7b35869..08eb56d1 100644 --- a/frontend/app/contexts/owner-context.tsx +++ b/frontend/app/contexts/owner-context.tsx @@ -77,7 +77,6 @@ export const OwnerProvider = ({ children }: { children: ReactNode }) => { void (async () => { try { const sales = await fetchOwnerGetSales({ - // TODO: 機能していない? queryParams: { since: timestamp(since), until: timestamp(until), diff --git a/frontend/app/contexts/simulator-context.tsx b/frontend/app/contexts/simulator-context.tsx index dec60fc9..9a57546d 100644 --- a/frontend/app/contexts/simulator-context.tsx +++ b/frontend/app/contexts/simulator-context.tsx @@ -18,14 +18,16 @@ import { fetchChairGetNotification, } from "~/api/api-components"; import { SimulatorChair } from "~/types"; -import { getSimulatorCurrentCoordinate } from "~/utils/storage"; import { getCookieValue } from "~/utils/get-cookie-value"; +import { Message, MessageTypes } from "~/utils/post-message"; +import { getSimulatorCurrentCoordinate } from "~/utils/storage"; type SimulatorContextProps = { chair?: SimulatorChair; data?: ChairGetNotificationResponse["data"]; setCoordinate?: (coordinate: Coordinate) => void; setToken?: (token: string) => void; + isAnotherSimulatorBeingUsed?: boolean; }; const SimulatorContext = createContext({}); @@ -186,6 +188,26 @@ export const SimulatorProvider = ({ children }: { children: ReactNode }) => { return coordinate ?? { latitude: 0, longitude: 0 }; }); + const [clientRideId, setClientRideId] = useState(); + const isAnotherSimulatorBeingUsed = !clientRideId && !!data?.ride_id; + + console.log(isAnotherSimulatorBeingUsed); + + useEffect(() => { + const onMessage = ({ + data, + }: MessageEvent) => { + const isSameOrigin = origin == location.origin; + if (isSameOrigin && data.type === MessageTypes.ClientRideRequested) { + setClientRideId(data?.payload?.rideId); + } + }; + window.addEventListener("message", onMessage); + return () => { + window.removeEventListener("message", onMessage); + }; + }, []); + return ( { chair: simulateChair ? { ...simulateChair, coordinate } : undefined, setCoordinate, setToken, + isAnotherSimulatorBeingUsed, }} > {children} diff --git a/frontend/app/routes/client._index/route.tsx b/frontend/app/routes/client._index/route.tsx index b3212836..380900ba 100644 --- a/frontend/app/routes/client._index/route.tsx +++ b/frontend/app/routes/client._index/route.tsx @@ -16,8 +16,8 @@ import { Button } from "~/components/primitives/button/button"; import { Modal } from "~/components/primitives/modal/modal"; import { Text } from "~/components/primitives/text/text"; import { useClientContext } from "~/contexts/client-context"; -import { NearByChair, isClientApiError } from "~/types"; -import { sendClientReady } from "~/utils/post-message"; +import { NearByChair } from "~/types"; +import { sendClientReady, sendClientRideRequested } from "~/utils/post-message"; import { Arrived } from "./driving-state/arrived"; import { Carrying } from "./driving-state/carrying"; import { Enroute } from "./driving-state/enroute"; @@ -36,6 +36,7 @@ type EstimatePrice = { fare: number; discount: number }; export default function Index() { const { data } = useClientContext(); + const emulateChairs = useGhostChairs(); const [internalRideStatus, setInternalRideStatus] = useState(); const [currentLocation, setCurrentLocation] = useState(); const [destLocation, setDestLocation] = useState(); @@ -44,28 +45,27 @@ export default function Index() { const [displayedChairs, setDisplayedChairs] = useState([]); const [centerCoordinate, setCenterCoodirnate] = useState(); const onCenterMove = useCallback( - (coordinate: Coordinate) => { - setCenterCoodirnate(coordinate); - }, - [setCenterCoodirnate], + (coordinate: Coordinate) => setCenterCoodirnate(coordinate), + [], + ); + const onSelectMove = useCallback( + (coordinate: Coordinate) => setSelectedLocation(coordinate), + [], ); - const onSelectMove = useCallback((coordinate: Coordinate) => { - setSelectedLocation(coordinate); - }, []); const [isLocationSelectorModalOpen, setLocationSelectorModalOpen] = useState(false); const locationSelectorModalRef = useRef void }>( null, ); + const statusModalRef = useRef void }>(null); + const [estimatePrice, setEstimatePrice] = useState(); const handleConfirmLocation = useCallback(() => { if (direction === "from") { setCurrentLocation(selectedLocation); } else if (direction === "to") { setDestLocation(selectedLocation); } - if (locationSelectorModalRef.current) { - locationSelectorModalRef.current.close(); - } + locationSelectorModalRef.current?.close(); }, [direction, selectedLocation]); const isStatusModalOpen = useMemo(() => { @@ -77,10 +77,6 @@ export default function Index() { ); }, [internalRideStatus]); - const statusModalRef = useRef void }>(null); - const [estimatePrice, setEstimatePrice] = useState(); - const emulateChairs = useGhostChairs(); - useEffect(() => { setInternalRideStatus(data?.status); }, [data?.status]); @@ -110,16 +106,15 @@ export default function Index() { } setInternalRideStatus("MATCHING"); try { - void (await fetchAppPostRides({ + const { ride_id } = await fetchAppPostRides({ body: { pickup_coordinate: currentLocation, destination_coordinate: destLocation, }, - })); + }); + sendClientRideRequested(window.parent, { rideId: ride_id }); } catch (error) { - if (isClientApiError(error)) { - console.error(error); - } + console.error(error); } }, [currentLocation, destLocation]); @@ -129,8 +124,7 @@ export default function Index() { let abortController: AbortController | undefined; let timeoutId: NodeJS.Timeout | undefined; - const updateNearByChairs = async (coordinate: Coordinate) => { - const { latitude, longitude } = coordinate; + const updateNearByChairs = async ({ latitude, longitude }: Coordinate) => { try { abortController?.abort(); abortController = new AbortController(); @@ -227,7 +221,7 @@ export default function Index() { ref={locationSelectorModalRef} onClose={() => setLocationSelectorModalOpen(false)} > -
+
, +) => { + target.postMessage({ type: MessageTypes.ClientRideRequested, payload }, "*"); +}; + export const sendSimulatorConfig = ( target: Window, payload: NonNullable,