diff --git a/Backend/Schema.ts b/Backend/Schema.ts index 398d8ac..38c43ca 100644 --- a/Backend/Schema.ts +++ b/Backend/Schema.ts @@ -62,6 +62,7 @@ export const typeDefs = `#graphql sort: String ): [Game!]! wishlist(userId: Int!): [Game!]! + isGameWishlisted(userId: Int!, gameId: Int!): Boolean! game(appid: Int!): Game genres: [String!]! profile(userId: Int!): Profile diff --git a/Backend/resolvers/WishlistResolvers.ts b/Backend/resolvers/WishlistResolvers.ts index 20deb02..70345ba 100644 --- a/Backend/resolvers/WishlistResolvers.ts +++ b/Backend/resolvers/WishlistResolvers.ts @@ -12,16 +12,34 @@ export const wishlistQueryResolvers = { { token }: GraphQLContext ): Promise => { await RequireUser(token, userId) - + const { rows } = await pool.query( `SELECT ${GAME_COLUMNS} - FROM wishlist w - JOIN games g ON g.game_id = w.game_id - WHERE w.user_id = $1 - ORDER BY g.game_name`, + FROM wishlist w + JOIN games g ON g.game_id = w.game_id + WHERE w.user_id = $1 + ORDER BY g.game_name`, [userId] ) return rows + }, + isGameWishlisted: async ( + _parent: unknown, + { userId, gameId }: { userId: number; gameId: number }, + { token }: GraphQLContext + ): Promise => { + await RequireUser(token, userId) + + const { rows } = await pool.query( + `SELECT EXISTS ( + SELECT 1 + FROM wishlist + WHERE user_id = $1 + AND game_id = $2 + ) AS "isGameWishlisted"`, + [userId, gameId] + ) + return rows[0].isGameWishlisted } } diff --git a/E2E/Mainpage.spec.ts b/E2E/Mainpage.spec.ts index 0ffd9b5..0af3207 100644 --- a/E2E/Mainpage.spec.ts +++ b/E2E/Mainpage.spec.ts @@ -2,7 +2,7 @@ import { test, expect } from './fixtures' import { openMainPage, testCarousel } from './helpers' -const GAME_PAGE = /#\/gamepage\/\d+$/ +const GAME_PAGE = /#\/GamePage\/\d+$/ async function checkFeaturedGames(page: import('@playwright/test').Page) { const section = page diff --git a/E2E/MainpageToGamepage.spec.ts b/E2E/MainpageToGamepage.spec.ts index 07de9db..1b8650d 100644 --- a/E2E/MainpageToGamepage.spec.ts +++ b/E2E/MainpageToGamepage.spec.ts @@ -3,7 +3,7 @@ import { test, expect } from './fixtures' import { openMainPage } from './helpers' import type { Page } from '@playwright/test' -const GAME_PAGE = /#\/gamepage\/\d+$/ +const GAME_PAGE = /#\/GamePage\/\d+$/ const WISHLIST_PAGE = /#\/WishlistPage$/ async function openTopGameAndToggleWishlist(page: Page) { diff --git a/E2E/Reviews.spec.ts b/E2E/Reviews.spec.ts index c1e14b5..decb2e9 100644 --- a/E2E/Reviews.spec.ts +++ b/E2E/Reviews.spec.ts @@ -3,7 +3,7 @@ import { test, expect } from './fixtures' import { openMainPage } from './helpers' import type { Page } from '@playwright/test' -const GAME_PAGE = /#\/gamepage\/\d+$/ +const GAME_PAGE = /#\/GamePage\/\d+$/ async function navigateFromRecentReviewToGame( page: Page, diff --git a/E2E/Wishlist.spec.ts b/E2E/Wishlist.spec.ts index 7d3c0c7..4eed4e1 100644 --- a/E2E/Wishlist.spec.ts +++ b/E2E/Wishlist.spec.ts @@ -4,7 +4,7 @@ import { openMainPage } from './helpers' import type { Page } from '@playwright/test' const WISHLIST_PAGE = /#\/WishlistPage$/ -const GAME_PAGE = /#\/gamepage\/\d+$/ +const GAME_PAGE = /#\/GamePage\/\d+$/ async function openDesktopWishlistPage( page: Page, @@ -48,7 +48,7 @@ async function waitForWishlistPage(page: Page) { } async function openWishlistedGameAndGoBack(page: Page) { - const firstGame = page.locator('a[href*="gamepage"]').first() + const firstGame = page.locator('a[href*="GamePage"]').first() await expect(firstGame).toBeVisible() await firstGame.click() @@ -78,7 +78,7 @@ async function sortWishlistGames(page: Page) { } async function removeGameFromWishlist(page: Page) { - const firstGame = page.locator('a[href*="gamepage"]').first() + const firstGame = page.locator('a[href*="GamePage"]').first() await expect(firstGame).toBeVisible() diff --git a/E2E/helpers.ts b/E2E/helpers.ts index 1de3ce2..71e666d 100644 --- a/E2E/helpers.ts +++ b/E2E/helpers.ts @@ -36,7 +36,7 @@ export async function testCarousel(page: Page, heading: string) { }) const gameCards = section.locator( - '.genre-games-row a[href*="gamepage/"]' + '.genre-games-row a[href*="GamePage/"]' ) await expect(nextButton).toBeVisible() diff --git a/Frontend/App.tsx b/Frontend/App.tsx index a83d330..b084660 100644 --- a/Frontend/App.tsx +++ b/Frontend/App.tsx @@ -16,7 +16,7 @@ function App() { } /> } /> - } /> + } /> } /> } /> } /> diff --git a/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts b/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts index e0709b9..f2ea76a 100644 --- a/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts +++ b/Frontend/src/FrontendLogic/WishlistLogicFrontend.ts @@ -51,3 +51,19 @@ export function SaveSortOptionWishlist(query: string) { export function GetSortOptionWishlist(): string { return ReadStored(WISHLIST_KEY, 'discount-desc', IsString) } + +// Frontend function for checking if a specific game is wishlisted +export async function FetchIsGameWishlistedFrontend( + userId: number, + gameId: number +): Promise { + const data = await Gql<{ isGameWishlisted: boolean }>( + `query($userId: Int!, $gameId: Int!) { + isGameWishlisted(userId: $userId, gameId: $gameId) + }`, + { userId, gameId } + ) + + return data.isGameWishlisted +} + diff --git a/Frontend/src/components/ActiveGameCard/ActiveGameCard.css b/Frontend/src/components/ActiveGameCard/ActiveGameCard.css index a2efe63..9493863 100644 --- a/Frontend/src/components/ActiveGameCard/ActiveGameCard.css +++ b/Frontend/src/components/ActiveGameCard/ActiveGameCard.css @@ -49,33 +49,6 @@ gap: 1.25rem; } -.activeGameInfoCard, -.activeGameReviewsCard { - min-width: 0; - background-color: var(--primary-component-color) !important; - color: var(--primary-text-color); - box-shadow: var(--card-shadow); -} - -.activeGameReviewsCard { - display: flex; - min-height: 0; -} - -.activeGameInfoContent.MuiCardContent-root { - display: flex; - flex-direction: column; - gap: 1rem; - padding: 1.25rem; -} - -.activeGameInfoHeader { - display: flex; - align-items: center; - justify-content: space-between; - gap: 1rem; -} - .activeGameTitle { min-width: 0; margin: 0; @@ -138,12 +111,6 @@ font-weight: 700; } -.activeGameInfoMiddle { - display: flex; - flex-direction: column; - gap: 1rem; -} - .activeGameDeveloper { line-height: 1.5; } @@ -165,11 +132,6 @@ color: var(--primary-text-color); } -.activeGameInfoContent .MuiDivider-root { - width: 100%; - border-color: rgb(0 0 0 / 20%); -} - @media (max-width: 64rem) { .activeGameLayout { margin-block: 1rem; @@ -180,18 +142,9 @@ align-items: start; gap: 1rem; } - - .activeGameInfoContent.MuiCardContent-root { - padding: 1.125rem; - } } @media (max-width: 26rem) { - .activeGameInfoHeader { - align-items: flex-start; - flex-direction: column; - } - .activeGamePriceGroup { justify-content: flex-start; } diff --git a/Frontend/src/components/ActiveGameCard/ActiveGameCard.tsx b/Frontend/src/components/ActiveGameCard/ActiveGameCard.tsx index 35b15c2..824496e 100644 --- a/Frontend/src/components/ActiveGameCard/ActiveGameCard.tsx +++ b/Frontend/src/components/ActiveGameCard/ActiveGameCard.tsx @@ -1,4 +1,4 @@ -import { Box, Button, Card, CardContent, CardMedia, Chip, Divider, Typography } from '@mui/material' +import { Box, Button, Card, CardMedia } from '@mui/material' import './ActiveGameCard.css' import type { GameData } from '../../types/Game' import { GetSavedUser } from '../../FrontendLogic/UserLogicFrontend' @@ -6,17 +6,10 @@ import { useState, useEffect } from 'react' import { RemoveFromWishlistFrontend, AddToWishlistFrontend, - FetchWishlistFrontend + FetchIsGameWishlistedFrontend } from '../../FrontendLogic/WishlistLogicFrontend' -import { - CreateReview, - DeleteReview, - GetReviewsForGame, - UpdateReview -} from '../../FrontendLogic/ReviewLogicFrontend' -import type { Review } from '../../types/Review' -import Reviews from '../Reviews/Reviews' -import { UseAuth } from '../../context/AuthContext' +import ActiveGameCardInfo from '../ActiveGameCardInfo/ActiveGameCardInfo.tsx' +import ActiveGameCardReviews from '../ActiveGameCardReviews/ActiveGameCardReviews.tsx' type ActiveGameCardProps = { game: GameData @@ -29,71 +22,32 @@ export default function ActiveGameCard({ onAddToWishlist, onRemoveFromWishlist }: ActiveGameCardProps) { - const { user } = UseAuth() const [isWishlisted, setIsWishlisted] = useState(false) const [isChecking, setIsChecking] = useState(() => GetSavedUser() !== null) const [isBusy, setIsBusy] = useState(false) const [error, setError] = useState(null) - const [reviews, setReviews] = useState([]) - const [reviewError, setReviewError] = useState(null) - const currentUserId = user?.id ?? null - - const isLoggedIn = GetSavedUser() !== null - - const isOnSale = - game.salePercent !== undefined && game.salePercent > 0 && game.salePercent <= 100 - const showOriginalPrice = - isOnSale && game.originalPrice !== undefined && game.originalPrice > game.price + + const user = GetSavedUser() + const userId = user?.id ?? null + const isLoggedIn = userId !== null - //Loads the game's reviews, again when the user signs in or out so their own review comes first + // Finds out if the signed in user already has this game on their wishlist useEffect(() => { let cancelled = false - GetReviewsForGame(game.id, currentUserId) - .then((loadedReviews) => { - if (cancelled) return - setReviews(loadedReviews) - setReviewError(null) - }) - .catch((loadError: unknown) => { - if (!cancelled) { - setReviewError( - loadError instanceof Error ? loadError.message : 'Could not load reviews.' - ) - } - }) - - return () => { - cancelled = true + if (userId == null) { + setIsChecking(false) + setIsWishlisted(false) + return } - }, [game.id, currentUserId]) - - async function HandleSubmitReview(rating: number, text: string) { - if (!user) throw new Error('Sign in to submit a review.') - setReviews(await CreateReview(game.id, user.id, rating, text)) - } - - async function HandleEditReview(reviewId: number, rating: number, text: string) { - if (!user) throw new Error('Sign in to edit a review.') - setReviews(await UpdateReview(game.id, reviewId, user.id, rating, text)) - } - async function HandleDeleteReview(reviewId: number) { - if (!user) throw new Error('Sign in to delete a review.') - setReviews(await DeleteReview(game.id, reviewId, user.id)) - } - - // Finds out if the signed in user already has this game on their wishlist - useEffect(() => { - let cancelled = false - const user = GetSavedUser() - - if (!user) return + setIsChecking(true) + setError(null) - FetchWishlistFrontend(user.id) - .then((wishlist) => { + FetchIsGameWishlistedFrontend(userId, game.id) + .then((wishlisted) => { if (!cancelled) - setIsWishlisted(wishlist.some((wishlisted) => wishlisted.appid === game.id)) + setIsWishlisted(wishlisted) }) .catch(() => { if (!cancelled) setError('Could not check your wishlist') @@ -105,7 +59,7 @@ export default function ActiveGameCard({ return () => { cancelled = true } - }, [game.id]) + }, [game.id, userId]) // Adds the game to the wishlist, or removes it if it is already there async function HandleToggleWishlist() { @@ -135,7 +89,7 @@ export default function ActiveGameCard({ } return ( - + )} - - - - - {game.name} - - - {isOnSale && ( - - )} - - {game.price.toFixed(2)} $ - - {showOriginalPrice && ( - - - Original price:{' '} - - {game.originalPrice!.toFixed(2)} $ - - )} - - - - - - {game.description} - - - - - {game.genre.map((genre) => ( - - ))} - - - - - - + + ) diff --git a/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.css b/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.css new file mode 100644 index 0000000..3716887 --- /dev/null +++ b/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.css @@ -0,0 +1,44 @@ +.activeGameInfoCard { + min-width: 0; + background-color: var(--primary-component-color) !important; + color: var(--primary-text-color); + box-shadow: var(--card-shadow); +} + +.activeGameInfoContent.MuiCardContent-root { + display: flex; + flex-direction: column; + gap: 1rem; + padding: 1.25rem; +} + +.activeGameInfoHeader { + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; +} + +.activeGameInfoMiddle { + display: flex; + flex-direction: column; + gap: 1rem; +} + +.activeGameInfoContent .MuiDivider-root { + width: 100%; + border-color: rgb(0 0 0 / 20%); +} + +@media (max-width: 64rem) { + .activeGameInfoContent.MuiCardContent-root { + padding: 1.125rem; + } +} + +@media (max-width: 26rem) { + .activeGameInfoHeader { + align-items: flex-start; + flex-direction: column; + } +} \ No newline at end of file diff --git a/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.tsx b/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.tsx new file mode 100644 index 0000000..a87b53d --- /dev/null +++ b/Frontend/src/components/ActiveGameCardInfo/ActiveGameCardInfo.tsx @@ -0,0 +1,64 @@ +import { Box, Card, CardContent, Chip, Divider, Typography } from '@mui/material' +import './ActiveGameCardInfo.css' +import type { GameData } from '../../types/Game' + +type ActiveGameInfoProps = { + game: GameData +} + +export default function ActiveGameCardInfo({ game }: ActiveGameInfoProps) { + const isOnSale = + game.salePercent !== undefined && game.salePercent > 0 && game.salePercent <= 100 + const showOriginalPrice = + isOnSale && game.originalPrice !== undefined && game.originalPrice > game.price + + return ( + + + + + {game.name} + + + {isOnSale && ( + + )} + + {game.price.toFixed(2)} $ + + {showOriginalPrice && ( + + Original price: + {game.originalPrice!.toFixed(2)} $ + + )} + + + + + + {game.description} + + + + + {game.genre.map((genre) => ( + + ))} + + + + ) +} \ No newline at end of file diff --git a/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.css b/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.css new file mode 100644 index 0000000..9b7aae3 --- /dev/null +++ b/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.css @@ -0,0 +1,11 @@ +.activeGameReviewsCard { + min-width: 0; + background-color: var(--primary-component-color) !important; + color: var(--primary-text-color); + box-shadow: var(--card-shadow); +} + +.activeGameReviewsCard { + display: flex; + min-height: 0; +} \ No newline at end of file diff --git a/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.tsx b/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.tsx new file mode 100644 index 0000000..9d4a40a --- /dev/null +++ b/Frontend/src/components/ActiveGameCardReviews/ActiveGameCardReviews.tsx @@ -0,0 +1,74 @@ +import { Card } from '@mui/material' +import './ActiveGameCardReviews.css' +import { useState, useEffect } from 'react' +import { + CreateReview, + DeleteReview, + GetReviewsForGame, + UpdateReview +} from '../../FrontendLogic/ReviewLogicFrontend' +import type { Review } from '../../types/Review' +import Reviews from '../Reviews/Reviews' +import { UseAuth } from '../../context/AuthContext' + +type ActiveGameReviewsProps = { + gameId: number +} + +export default function ActiveGameCardReviews({ gameId }: ActiveGameReviewsProps) { + const { user } = UseAuth() + const [reviews, setReviews] = useState([]) + const [reviewError, setReviewError] = useState(null) + const currentUserId = user?.id ?? null + + // Loads the game's reviews, again when the user signs in or out so their own review comes first + useEffect(() => { + let cancelled = false + + GetReviewsForGame(gameId, currentUserId) + .then((loadedReviews) => { + if (cancelled) return + setReviews(loadedReviews) + setReviewError(null) + }) + .catch((loadError: unknown) => { + if (!cancelled) { + setReviewError( + loadError instanceof Error ? loadError.message : 'Could not load reviews.' + ) + } + }) + + return () => { + cancelled = true + } + }, [gameId, currentUserId]) + + async function HandleSubmitReview(rating: number, text: string) { + if (!user) throw new Error('Sign in to submit a review.') + setReviews(await CreateReview(gameId, user.id, rating, text)) + } + + async function HandleEditReview(reviewId: number, rating: number, text: string) { + if (!user) throw new Error('Sign in to edit a review.') + setReviews(await UpdateReview(gameId, reviewId, user.id, rating, text)) + } + + async function HandleDeleteReview(reviewId: number) { + if (!user) throw new Error('Sign in to delete a review.') + setReviews(await DeleteReview(gameId, reviewId, user.id)) + } + + return ( + + + + ) +} \ No newline at end of file diff --git a/Frontend/src/components/PassiveGameCard/PassiveGameCard.tsx b/Frontend/src/components/PassiveGameCard/PassiveGameCard.tsx index 7e65127..be5e738 100644 --- a/Frontend/src/components/PassiveGameCard/PassiveGameCard.tsx +++ b/Frontend/src/components/PassiveGameCard/PassiveGameCard.tsx @@ -51,7 +51,7 @@ export default function PassiveGameCard({ game, rank }: Props) { + See this game )} diff --git a/Frontend/src/components/Reviews/Reviews.css b/Frontend/src/components/Reviews/Reviews.css index de5a440..d884546 100644 --- a/Frontend/src/components/Reviews/Reviews.css +++ b/Frontend/src/components/Reviews/Reviews.css @@ -32,6 +32,11 @@ text-transform: none; } +.reviewComposerToggleDisabled { + cursor: not-allowed !important; + opacity: 0.8; +} + .reviewComposerHint { margin: -0.5rem 0 0; color: var(--primary-text-color); diff --git a/Frontend/src/components/Reviews/Reviews.tsx b/Frontend/src/components/Reviews/Reviews.tsx index 1647fc8..d688fe7 100644 --- a/Frontend/src/components/Reviews/Reviews.tsx +++ b/Frontend/src/components/Reviews/Reviews.tsx @@ -105,7 +105,7 @@ export default function Reviews({ {(!hasCurrentUserReview || isComposerOpen) && (