-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from TaloDev/develop
Release 0.42.0
- Loading branch information
Showing
9 changed files
with
182 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { z } from 'zod' | ||
import api from './api' | ||
import makeValidatedRequest from './makeValidatedRequest' | ||
import { playerGameStatSchema } from '../entities/playerGameStat' | ||
|
||
const updateStatValue = makeValidatedRequest( | ||
(gameId: number, playerId: string, playerStatId: number, newValue: number) => { | ||
return api.patch(`/games/${gameId}/players/${playerId}/stats/${playerStatId}`, { newValue }) | ||
}, | ||
z.object({ | ||
playerStat: playerGameStatSchema | ||
}) | ||
) | ||
|
||
export default updateStatValue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { MouseEvent, useState } from 'react' | ||
import Modal from '../components/Modal' | ||
import TextInput from '../components/TextInput' | ||
import Button from '../components/Button' | ||
import buildError from '../utils/buildError' | ||
import ErrorMessage, { TaloError } from '../components/ErrorMessage' | ||
import { PlayerGameStat } from '../entities/playerGameStat' | ||
import { KeyedMutator } from 'swr/_internal' | ||
import updateStatValue from '../api/updateStatValue' | ||
import { Player } from '../entities/player' | ||
import activeGameState from '../state/activeGameState' | ||
import { useRecoilValue } from 'recoil' | ||
import { SelectedActiveGame } from '../state/activeGameState' | ||
import { upperFirst } from 'lodash-es' | ||
|
||
type UpdateStatValueProps = { | ||
modalState: [boolean, (open: boolean) => void] | ||
mutate: KeyedMutator<{ stats: PlayerGameStat[] }> | ||
editingStat: PlayerGameStat | ||
player: Player | ||
} | ||
|
||
export default function UpdateStatValue({ modalState, mutate, editingStat, player }: UpdateStatValueProps) { | ||
const [, setOpen] = modalState | ||
const [value, setValue] = useState(editingStat.value.toString()) | ||
const [isLoading, setLoading] = useState(false) | ||
const [error, setError] = useState<TaloError | null>(null) | ||
|
||
const activeGame = useRecoilValue(activeGameState) as SelectedActiveGame | ||
|
||
const onCreateClick = async (e: MouseEvent<HTMLElement>) => { | ||
e.preventDefault() | ||
setLoading(true) | ||
setError(null) | ||
|
||
try { | ||
const { playerStat } = await updateStatValue(activeGame.id, player.id, editingStat.id, Number(value)) | ||
mutate((data) => { | ||
return { | ||
...data, | ||
stats: [...data!.stats.filter((stat) => stat.id !== editingStat!.id), playerStat] | ||
} | ||
}, true) | ||
setOpen(false) | ||
} catch (err) { | ||
setError(buildError(err)) | ||
setLoading(false) | ||
} | ||
} | ||
|
||
return ( | ||
<Modal | ||
id='update-player-stat' | ||
title={upperFirst(editingStat.stat.name)} | ||
modalState={modalState} | ||
> | ||
<form> | ||
<div className='p-4 space-y-4'> | ||
<div> | ||
<p className='font-semibold'>Current value</p> | ||
<p>{editingStat.value}</p> | ||
</div> | ||
|
||
<TextInput | ||
id='value' | ||
variant='light' | ||
type='number' | ||
label='New value' | ||
placeholder='Stat value' | ||
onChange={setValue} | ||
value={value} | ||
inputClassName='border border-gray-200 focus:border-opacity-0' | ||
/> | ||
|
||
{error && <ErrorMessage error={error} />} | ||
</div> | ||
|
||
<div className='flex flex-col md:flex-row-reverse md:justify-between space-y-4 md:space-y-0 p-4 border-t border-gray-200'> | ||
<div className='w-full md:w-32'> | ||
<Button | ||
disabled={!value} | ||
isLoading={isLoading} | ||
onClick={onCreateClick} | ||
> | ||
Update | ||
</Button> | ||
</div> | ||
<div className='w-full md:w-32'> | ||
<Button type='button' variant='grey' onClick={() => setOpen(false)}>Cancel</Button> | ||
</div> | ||
</div> | ||
</form> | ||
</Modal> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters