Skip to content
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
25 changes: 24 additions & 1 deletion frontend/src/pages/GameDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { Image, Stack } from "react-bootstrap";
import { Alert, Image, Stack } from "react-bootstrap";
import { useParams } from "react-router-dom";
import { gql } from "@apollo/client";
import { useMutation, useQuery } from "@apollo/client/react";
Expand Down Expand Up @@ -117,6 +117,8 @@ export default function GameDetails() {
const isMobile = useIsMobile();
const userId = useMemo(() => getUserId(), []);
const imageSize: number = isMobile ? 150 : 220;
const [alertingUser, setAlertingUser] = useState(false);
const alertTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);

const { data, loading, error } = useQuery<GameData>(GET_GAME, {
variables: { id },
Expand All @@ -127,6 +129,12 @@ export default function GameDetails() {
});
const [rateGame] = useMutation(RATE_GAME);

useEffect(() => {
return () => {
if (alertTimeout.current) clearTimeout(alertTimeout.current);
};
}, []);

const game = data?.game;
const savedScore =
myReviewData?.myReviews.find((review) => review.gameId === id)?.score ??
Expand Down Expand Up @@ -186,12 +194,27 @@ export default function GameDetails() {
function handleRate(score: number) {
if (!id) return;
setOverride({ id, score });
setAlertingUser(true);
if (alertTimeout.current) clearTimeout(alertTimeout.current);
alertTimeout.current = setTimeout(() => setAlertingUser(false), 3000);
}

return (
<>
<Header />
<Layout>
{alertingUser && (
<Alert
variant="info"
className="position-fixed start-50 translate-middle-x shadow"
style={{
top: "calc(var(--header-height) + 1rem)",
zIndex: 1050,
}}
>
Your rating is locally stored and will be updated next time you are here.
</Alert>
)}
<div className="ps-2 pe-2 pt-3 pb-3">
{loading && !data && <p className="text-muted">Loading…</p>}
{!loading && (error || !game) && (
Expand Down