Skip to content

Commit

Permalink
working websocket on race detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
therungg committed Dec 10, 2023
1 parent 488702c commit 133ab5a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NEXT_PUBLIC_MEDIA_URL=https://d73zqe3h0v0mi.cloudfront.net/therun
NEXT_PUBLIC_BASE_URL=http://localhost:3000
NEXT_PUBLIC_VERCEL_URL=${VERCEL_URL}
NEXT_PUBLIC_TWITCH_OAUTH_CLIENT_ID=
NEXT_PUBLIC_RACE_API_URL=https://8rcz1rfrqj.execute-api.eu-west-1.amazonaws.com/prod/
NEXT_PUBLIC_RACE_API_URL=https://6nkfyze0o7.execute-api.eu-west-1.amazonaws.com/prod

DEV_API_KEY=05a83c7f-2802-4427-b21a-9efb4c5669d7
PROD_API_KEY=
Expand Down
21 changes: 18 additions & 3 deletions app/races/[race]/race-view.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { Race } from "~app/races/races.types";
import { Race, RaceParticipant } from "~app/races/races.types";
import { arrayToMap } from "~src/utils/array";
import { User } from "../../../types/session.types";
import { Button, Table } from "react-bootstrap";
Expand All @@ -17,8 +17,10 @@ interface RaceDetailProps {
export const RaceDetail = ({ race, user }: RaceDetailProps) => {
const router = useRouter();
const [readyLoading, setReadyLoading] = useState(false);
const [raceParticipantsMap, setRaceParticipantsMap] = useState<
Map<string, RaceParticipant>
>(arrayToMap(race.participants || [], "user"));

const raceParticipantsMap = arrayToMap(race.participants || [], "user");
const raceIsPending = race.status === "pending";
const userParticipates = user && raceParticipantsMap.has(user.username);

Expand Down Expand Up @@ -50,12 +52,25 @@ export const RaceDetail = ({ race, user }: RaceDetailProps) => {
lastMessage.data.raceId
) {
// eslint-disable-next-line no-console
console.log("New race event", lastMessage.data);
console.log("New race event", lastMessage.data, lastMessage.type);

if (lastMessage.type === "participantUpdate") {
// Create a new Map for the updated state
const updatedMap = new Map(raceParticipantsMap);

updatedMap.set(
(lastMessage.data as RaceParticipant).user,
lastMessage.data as RaceParticipant
);

setRaceParticipantsMap(updatedMap);
}
}
}, [lastMessage]);

return (
<div>
{JSON.stringify(Array.from(raceParticipantsMap))}
{readyLoading && <div>Setting ready/unready</div>}
{raceIsPending && userParticipates && (
<div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/topbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const Topbar = ({
<Nav.Link href="/live">
<b>Live</b>
</Nav.Link>
<Nav.Link href="/races">
<b>Races</b>
</Nav.Link>
<Nav.Link href="/games/">Games</Nav.Link>
<Nav.Link href="/patron">
Support <PatreonBunnySvgWithoutLink />
Expand Down

0 comments on commit 133ab5a

Please sign in to comment.