Skip to content

Commit

Permalink
fix: dynamic route for staying in lobby after refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
edalholt committed Sep 8, 2023
1 parent 3dcc4b4 commit 8fde88b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 54 deletions.
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function App() {
}
/>
<Route
path="/lobby"
path="/lobby/:groupSlug"
element={
<>
<HeaderAction />
Expand Down
20 changes: 5 additions & 15 deletions frontend/src/components/Groups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ import { checkedInState, checkedInType } from "../utils/Context";
export function Groups() {
const { classes } = useStyles();
const navigate = useNavigate();
const {
checkedIn,
setCheckedIn,
groupSlug,
setGroupSlug,
groupName,
setGroupName,
} = useContext(checkedInState) as checkedInType;
const { setCheckedIn } = useContext(checkedInState) as checkedInType;

let [userData, setUserData] = useState<UserDataResponseType | undefined>(
const [userData, setUserData] = useState<UserDataResponseType | undefined>(
undefined
);
const fetchData = async () => {
Expand All @@ -32,11 +25,9 @@ export function Groups() {
});
setUserData(userData);
};
const checkinNavigate = (groupSlug: string, groupName: string) => {
const checkinNavigate = (groupSlug: string) => {
setCheckedIn(false);
setGroupSlug(groupSlug);
setGroupName(groupName);
navigate("/lobby");
navigate("/lobby/" + groupSlug);
};

useEffect(() => {
Expand Down Expand Up @@ -91,8 +82,7 @@ export function Groups() {
className: classes.box,
}
: {
onClick: () =>
checkinNavigate(group.groupSlug, group.groupName),
onClick: () => checkinNavigate(group.groupSlug),
className: classes.activeBox,
})}
>
Expand Down
20 changes: 5 additions & 15 deletions frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import axios from "axios";
import { useNavigate } from "react-router-dom";
import logo from "../assets/logoHeader.svg";
import logoSmall from "../assets/ntnuiLogo.svg";
import {
Header,
Container,
Group,
Text,
Space,
Image,
Modal,
} from "@mantine/core";
import { Header, Container, Group, Text, Image, Modal } from "@mantine/core";
import { useMediaQuery, useDisclosure } from "@mantine/hooks";
import { useStyles } from "../styles/headerStyles";
import { QrCode } from "./QrCode";
Expand All @@ -22,9 +14,7 @@ export function HeaderAction() {
const matches = useMediaQuery("(min-width: 400px)");
const { classes } = useStyles();
const navigate = useNavigate();
const { checkedIn, setCheckedIn, groupSlug, setGroupSlug } = useContext(
checkedInState
) as checkedInType;
const { checkedIn } = useContext(checkedInState) as checkedInType;

const logOut = async () => {
await axios
Expand All @@ -51,10 +41,10 @@ export function HeaderAction() {
fullScreen={isMobile}
zIndex={2}
>
{checkedIn && groupSlug ? (
{checkedIn ? (
<QrCode />
) : (
<Text>Check out successfull. You can now leave the room</Text>
<Text>Check out successful. You can now leave the room</Text>
)}
</Modal>

Expand Down Expand Up @@ -84,7 +74,7 @@ export function HeaderAction() {
></Image>
)}
</Group>
{checkedIn && groupSlug ? (
{checkedIn ? (
<Text
className={classes.button}
onClick={open}
Expand Down
12 changes: 3 additions & 9 deletions frontend/src/components/QrCode.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Loader } from "@mantine/core";
import { useContext, useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { QRCodeSVG } from "qrcode.react";
import { getQrInfo } from "../services/qr";
import logo from "../assets/ntnuiColor.svg";
import { useNavigate } from "react-router-dom";
import { checkedInState, checkedInType } from "../utils/Context";
import { useParams } from "react-router-dom";

export function QrCode() {
const { groupSlug } = useContext(checkedInState) as checkedInType;
const navigate = useNavigate();
const { groupSlug } = useParams() as { groupSlug: string };
let [access, setAccess] = useState<string>();
let [time, setTime] = useState<number>(Date.now());
const getCredentials = async () => {
Expand All @@ -17,10 +15,6 @@ export function QrCode() {

// Update timestamp every 10 seconds
useEffect(() => {
if (!groupSlug) {
navigate("/start");
}

const interval = setInterval(() => setTime(Date.now()), 10000);
return () => {
clearInterval(interval);
Expand Down
33 changes: 23 additions & 10 deletions frontend/src/pages/AssemblyPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
import { QrCode } from "../components/QrCode";
import useWebSocket from "react-use-websocket";
import { WaitingRoom } from "../components/WaitingRoom";
Expand All @@ -10,20 +10,24 @@ import { Box, Image, Text } from "@mantine/core";
import Arrow from "../assets/Arrow.svg";
import { getCurrentVotationByGroup } from "../services/votation";
import { LimitedVoteType } from "../types/votes";
import { UserDataResponseType } from "../types/user";
import { getUserData } from "../services/organizer";

export function AssemblyLobby() {
let navigate = useNavigate();
const { groupSlug } = useParams() as { groupSlug: string };

const [kickedOut, setKickedOut] = useState<boolean>(false);
const [activeVotation, setActiveVotation] = useState<boolean>(false);
const [currentVotation, setCurrentVotation] = useState<
LimitedVoteType | undefined
>(undefined);
const [voted, setVoted] = useState<boolean>(false);
const { lastMessage } = useWebSocket(
import.meta.env.VITE_SOCKET_URL + "/lobby"
);
const { checkedIn, setCheckedIn, groupSlug, setGroupSlug, groupName } =
useContext(checkedInState) as checkedInType;
let navigate = useNavigate();
const { lastMessage } = useWebSocket(import.meta.env.VITE_SOCKET_URL);
const { checkedIn, setCheckedIn } = useContext(
checkedInState
) as checkedInType;
const [groupName, setGroupName] = useState<string>("");

useEffect(() => {
// Redirect to waiting room if already checked in
Expand All @@ -39,9 +43,18 @@ export function AssemblyLobby() {
}
} else {
setCheckedIn(false);
setGroupSlug("");
}
};
const getGroupName = async () => {
const userData = await getUserData();

userData.groups.forEach((group) => {
if (group.groupSlug == groupSlug) {
setGroupName(group.groupName);
}
});
};
getGroupName();
isCheckedIn();
}, []);

Expand Down Expand Up @@ -104,7 +117,7 @@ export function AssemblyLobby() {
</Box>
)}
<Text mt={100} mb={20}>
{groupName} assembly
{groupName && <>{groupName} assembly</>}
</Text>

{kickedOut ? (
Expand All @@ -125,7 +138,7 @@ export function AssemblyLobby() {
<WaitingRoom message={"There are currently no active vote, look up!"} />
) : (
<>
<Text size={"xl"}>Check-in for {groupName.toUpperCase()}</Text>
<Text size={"xl"}>Check-in for {groupName}</Text>
<QrCode />
</>
)}
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/utils/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import React from "react";

export interface checkedInType {
checkedIn: boolean;
groupSlug: string;
groupName: string;
setCheckedIn: (state: boolean) => void;
setGroupSlug: (sate: string) => void;
setGroupName: (state: string) => void;
}
export const checkedInState = React.createContext<checkedInType | null>(null);

0 comments on commit 8fde88b

Please sign in to comment.