From 639405bf9fce790db11ab3cb1f4b538ec5182101 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sun, 29 May 2022 10:28:53 +0200 Subject: [PATCH] Adds a details page for a single gamenight. --- backend/src/api.rs | 8 +++-- backend/src/schema/gamenight.rs | 16 ++++++--- frontend/src/App.js | 8 +++-- frontend/src/components/AddGameNight.jsx | 10 +----- frontend/src/components/Gamenight.jsx | 42 +++++++++++++++--------- frontend/src/components/Gamenights.jsx | 8 ++--- 6 files changed, 54 insertions(+), 38 deletions(-) diff --git a/backend/src/api.rs b/backend/src/api.rs index 260fea7..92f186e 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -137,6 +137,7 @@ pub struct GamenightOutput { #[serde(flatten)] gamenight: Gamenight, game_list: Vec, + participants: Vec, } #[get("/gamenights")] @@ -150,12 +151,15 @@ pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant { let game_results : Result, DatabaseError> = join_all(gamenights.iter().map(|gn| async move { let games = get_games_of_gamenight(conn_ref, gn.id).await?; + let participants = load_participants(conn_ref, gn.id).await?; Ok(GamenightOutput{ gamenight: gn.clone(), - game_list: games + game_list: games, + participants: participants, }) })).await.into_iter().collect(); + match game_results { Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))), Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))) @@ -338,7 +342,7 @@ pub async fn games_unauthorized() -> ApiResponseVariant { #[get("/participants", format = "application/json", data = "")] pub async fn get_participants(conn: DbConn, _user: User, gamenight_id_json: Json) -> ApiResponseVariant { - match load_participants(&conn, gamenight_id_json.into_inner()).await { + match load_participants(&conn, gamenight_id_json.into_inner().gamenight_id).await { Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))), } diff --git a/backend/src/schema/gamenight.rs b/backend/src/schema/gamenight.rs index 5177515..754bd11 100644 --- a/backend/src/schema/gamenight.rs +++ b/backend/src/schema/gamenight.rs @@ -1,3 +1,4 @@ +use crate::schema::users::{users, User}; use crate::schema::{DatabaseError, DbConn}; use diesel::{Connection, ExpressionMethods, QueryDsl, RunQueryDsl}; use serde::{Deserialize, Serialize}; @@ -160,12 +161,17 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec) -> Result<( Ok(()) } -pub async fn load_participants(conn: &DbConn, gamenight_id: GamenightId) -> Result, DatabaseError> { +pub async fn load_participants(conn: &DbConn, gamenight_id: Uuid) -> Result, DatabaseError> { Ok(conn - .run(move |c| { - gamenight_participants::table - .filter(gamenight_participants::gamenight_id.eq(gamenight_id.gamenight_id)) - .load::(c) + .run::<_, Result, _>>(move |c| { + let linked_participants = gamenight_participants::table + .filter(gamenight_participants::gamenight_id.eq(gamenight_id)) + .load::(c)?; + linked_participants.iter().map(|l| { + users::table + .filter(users::id.eq(l.user_id)) + .first::(c) + }).collect() }) .await?) } diff --git a/frontend/src/App.js b/frontend/src/App.js index f710b24..d2164e6 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -50,6 +50,10 @@ function App() { setUser({...user}); }; + const dismissActiveGamenight = () => { + setActiveGamenight(null); + }; + useEffect(() => { if (user !== null) { const requestOptions = { @@ -116,12 +120,12 @@ function App() { setFlash={setFlash} refetchGamenights={refetchGamenights} gamenights={gamenights} - setActiveGamenight={setActiveGamenight}/> + onSelectGamenight={(g) => setActiveGamenight(g)}/> } else { mainview = } diff --git a/frontend/src/components/AddGameNight.jsx b/frontend/src/components/AddGameNight.jsx index 53667d9..9349c68 100644 --- a/frontend/src/components/AddGameNight.jsx +++ b/frontend/src/components/AddGameNight.jsx @@ -2,21 +2,13 @@ import React, { useEffect, useState } from 'react'; import DateTime from 'react-datetime'; import GameAdder from './GameAdder'; -import Autocomplete from '@mui/material/Autocomplete'; - import "react-datetime/css/react-datetime.css"; function AddGameNight(props) { const [expanded, setExpanded] = useState(false); const [gameName, setGameName] = useState(""); const [date, setDate] = useState(Date.now()); - const [gameList, setGameList] = useState([]); - - const emptyUuid = "00000000-0000-0000-0000-000000000000"; - - //temp hack: - props.games = [{id: emptyUuid, name: "mystic vale"}, {id: emptyUuid, name: "Crew"}]; - + const [gameList, setGameList] = useState([]); const handleNameChange = (event) => { setGameName(event.target.value); diff --git a/frontend/src/components/Gamenight.jsx b/frontend/src/components/Gamenight.jsx index 35f688f..da56d99 100644 --- a/frontend/src/components/Gamenight.jsx +++ b/frontend/src/components/Gamenight.jsx @@ -1,26 +1,38 @@ import * as React from 'react'; function Gamenight(props) { - console.log(props.gamenight); let games = props.gamenight.game_list.map(g => - ( -
  • - {g.name} -
  • - ) - ); + ( +
  • + {g.name} +
  • + ) + ); + + let participants = props.gamenight.participants.map(p => + ( +
  • + {p.username} +
  • + ) + ) return ( -
    - -

    {props.gamenight.name}

    - {props.gamenight.datetime} -
      - {games} -
    -
    +
    +

    {props.gamenight.name}

    + + {props.gamenight.datetime} +

    Games:

    +
      + {games} +
    +

    Participants:

    +
      + {participants} +
    +
    ) } diff --git a/frontend/src/components/Gamenights.jsx b/frontend/src/components/Gamenights.jsx index de6d76a..0f843a2 100644 --- a/frontend/src/components/Gamenights.jsx +++ b/frontend/src/components/Gamenights.jsx @@ -29,16 +29,14 @@ function Gamenights(props) { }) .then(() => props.refetchGamenights()); } - } + } let gamenights = props.gamenights.map(g => ( -
  • {console.log(g); props.setActiveGamenight(g);}}> +
  • props.onSelectGamenight(g)}> {g.name} {(props.user.id === g.owner_id || props.user.role === "Admin") && - + }
  • )