From fc78cde8b8454bd1099b48b4093292ff1ac57dd0 Mon Sep 17 00:00:00 2001 From: Jonathan Gruber Date: Mon, 5 Jun 2023 18:03:04 +0200 Subject: [PATCH] Use the reducer pattern to edit matches --- src/hooks/useSteamGames.ts | 10 +- src/pages/AddMatch.tsx | 38 ++-- src/pages/EditMatch.tsx | 343 ++++++++++++++++++++----------------- src/types/index.ts | 6 +- 4 files changed, 223 insertions(+), 174 deletions(-) diff --git a/src/hooks/useSteamGames.ts b/src/hooks/useSteamGames.ts index 6a8dc40..65c6fe9 100644 --- a/src/hooks/useSteamGames.ts +++ b/src/hooks/useSteamGames.ts @@ -17,18 +17,18 @@ interface Response { } export const useSteamGames = () => { - const { data: user } = useSteamUser(); + const { data: steamUser } = useSteamUser(); const url = new URL( 'https://api.jogruber.de/steam/IPlayerService/GetOwnedGames/v0001?format=json&include_appinfo=1', ); - if (user) { - url.searchParams.append('steamid', user.id); + if (steamUser) { + url.searchParams.append('steamid', steamUser.id); } return useQuery({ - queryKey: ['steam-apps', user?.id], + queryKey: ['steam-apps', steamUser?.id], queryFn: () => axios .get(url.toString()) @@ -36,6 +36,6 @@ export const useSteamGames = () => { .then(games => games.sort((a, b) => b.playtime_forever - a.playtime_forever), ), - enabled: Boolean(user), + enabled: Boolean(steamUser), }); }; diff --git a/src/pages/AddMatch.tsx b/src/pages/AddMatch.tsx index 4b05a94..f4342c7 100644 --- a/src/pages/AddMatch.tsx +++ b/src/pages/AddMatch.tsx @@ -62,6 +62,7 @@ const AddMatch: FunctionComponent = () => { const [game, setGame] = useState(null); const [date, setDate] = useState(defaultDate); + const [maxPlayers, setMaxPlayers] = useState(''); const [description, setDescription] = useState(); const [selfJoinMatch, setSelfJoinMatch] = useState(true); @@ -78,18 +79,19 @@ const AddMatch: FunctionComponent = () => { setLoading(true); - const match: NewMatch = { + const match = { created: Timestamp.fromDate(new Date()), createdBy: authUser.uid, date: Timestamp.fromDate(date), game: { name: game.name, - ...(isSteamGame(game) && { steamAppId: game.appid }), + steamAppId: isSteamGame(game) ? game.appid : null, + maxPlayers: null, }, players: [], reactions: [], - ...(description && { description }), - }; + description: description ?? null, + } satisfies NewMatch; addDoc(getCollectionRef('matches'), match) .then(doc => { @@ -121,7 +123,8 @@ const AddMatch: FunctionComponent = () => { {!steamUser && ( - Melde dich bei Steam an, um all deine verfügbaren Spiele zu laden. + Melde dich bei Steam an, um all deine verfügbaren Spiele + anzuzeigen. @@ -131,17 +134,32 @@ const AddMatch: FunctionComponent = () => {
addMatch(event, authUser)}> - + - + setMaxPlayers(event.target.value)} variant="outlined" + fullWidth + /> + + + setDescription(event.target.value)} multiline rows={3} + variant="outlined" fullWidth /> @@ -154,7 +172,7 @@ const AddMatch: FunctionComponent = () => { } label="Selbst mitspielen" /> - + - - - - + + { + dispatch({ + type: 'set_max_players', + maxPlayers: event.target.value, + }); + }} + variant="outlined" + fullWidth + /> + + + + dispatch({ + type: 'set_description', + description: event.target.value, + }) + } + multiline + rows={3} + variant="outlined" + fullWidth + /> + + + + + + + + - - - + + + ); }; +const updatePlayerDates = ( + players: Array, + updatedDate: Date, +): Array => + players.map(player => { + const updateDate = { + year: updatedDate.getFullYear(), + month: updatedDate.getMonth(), + date: updatedDate.getDate(), + }; + + return { + uid: player.uid, + from: Timestamp.fromDate(setDate(player.from.toDate(), updateDate)), + until: Timestamp.fromDate(setDate(player.until.toDate(), updateDate)), + }; + }); + export default EditMatch; diff --git a/src/types/index.ts b/src/types/index.ts index 115b59f..eccc2ae 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -14,7 +14,7 @@ export interface NewMatch { createdBy: Uid; date: Timestamp; game: Game; - description?: string; + description: string | null; players: Array; reactions?: Array; } @@ -26,8 +26,8 @@ export interface Reaction { export interface Game { name: string; - steamAppId?: number; - maxPlayers?: number; + steamAppId: number | null; + maxPlayers: number | null; } export interface Player {