Skip to content

Commit

Permalink
setTokenを可能にさせる
Browse files Browse the repository at this point in the history
  • Loading branch information
imamiya-masaki committed Dec 4, 2024
1 parent e6670c4 commit 4454f37
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
9 changes: 0 additions & 9 deletions frontend/app/components/hooks/use-subdomain.ts
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
import { useEffect, useState } from "react";

export const useSubDomain = () => {
const [subDomain, setSubDomain] = useState<string>();
useEffect(() => {
setSubDomain(location.hostname.split(".").shift());
}, [setSubDomain]);
return subDomain;
};
29 changes: 15 additions & 14 deletions frontend/app/contexts/simulator-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useState,
} from "react";
import type { Coordinate } from "~/apiClient/apiSchemas";
import { getSimulateChair } from "~/utils/get-initial-data";
import { getSimulateChair, getSimulateChairFromToken } from "~/utils/get-initial-data";

import { apiBaseURL } from "~/apiClient/APIBaseURL";
import {
Expand All @@ -16,7 +16,7 @@ import {
} from "~/apiClient/apiComponents";
import type { ClientChairRide, SimulatorChair } from "~/types";
import { getSimulatorCurrentCoordinate } from "~/utils/storage";
import { useSubDomain } from "~/components/hooks/use-subdomain";
import { getCookieValue } from "~/utils/get-cookie-value";

type ClientSimulatorContextProps = {
targetChair?: SimulatorChair;
Expand Down Expand Up @@ -184,23 +184,23 @@ export const useClientChairNotification = (id?: string) => {
return responseClientAppRequest;
};



export const SimulatorProvider = ({ children }: { children: ReactNode }) => {
const subDomain = useSubDomain();
const [token, setToken] = useState<string>()

const simulateChairData = useMemo(() => {
switch (subDomain) {
case "fe001":
return getSimulateChair(0);
case "fe002":
return getSimulateChair(1);
case "fe003":
return getSimulateChair(2);
default:
return getSimulateChair();
}
}, [subDomain]);
return token ? getSimulateChairFromToken(token) : getSimulateChair()
}, [token]);

useEffect(() => {

const token = getCookieValue(document.cookie, "chair_session")
if (token) {
setToken(token)
return;
}
// TODO: tokenがなければUI上で選択させるようにする
if (simulateChairData?.token) {
document.cookie = `chair_session=${simulateChairData.token}; path=/`;
}
Expand All @@ -224,6 +224,7 @@ export const SimulatorProvider = ({ children }: { children: ReactNode }) => {
setter: setCurrentCoordinate,
coordinate: currentCoodinate,
},
setToken,
}
: undefined,
}}
Expand Down
1 change: 1 addition & 0 deletions frontend/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type SimulatorChair = {
name: string;
model: string;
token: string;
setToken: React.Dispatch<React.SetStateAction<string | undefined>>
coordinateState: {
coordinate?: Coordinate;
setter: (coordinate: Coordinate) => void;
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/utils/get-initial-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export const getSimulateChair = (index?: number): InitialChair | undefined => {
? initialOwnerData?.simulatorChairs[index]
: initialOwnerData?.simulatorChairs[0];
};

export const getSimulateChairFromToken = (token: string): InitialChair | undefined => {
return initialOwnerData?.simulatorChairs.find(c => c.token === token);
};

0 comments on commit 4454f37

Please sign in to comment.