File tree Expand file tree Collapse file tree 2 files changed +28
-11
lines changed
Expand file tree Collapse file tree 2 files changed +28
-11
lines changed Original file line number Diff line number Diff line change 1+ import { atom } from 'jotai' ;
12import { atomWithStorage } from 'jotai/utils' ;
2- import type { LocalGameFormSchema } from '@/game/local/setup/_components/form' ;
3+ import type { DeepPartial } from 'react-hook-form' ;
4+ import type { LocalGameFormSchema } from '../local/setup/_components/form' ;
35
4- export const gameSettingsAtom = atomWithStorage < LocalGameFormSchema > (
6+ const baseGameSettingsAtom = atomWithStorage < LocalGameFormSchema > (
57 'gameSettings' ,
68 {
79 players : [ ] ,
810 numberOfSpies : '1' ,
911 randomNumberOfSpies : false ,
1012 } ,
1113) ;
14+
15+ export const gameSettingsAtom = atom (
16+ ( get ) => get ( baseGameSettingsAtom ) ,
17+ (
18+ get ,
19+ set ,
20+ update :
21+ | DeepPartial < LocalGameFormSchema >
22+ | ( ( prev : LocalGameFormSchema ) => LocalGameFormSchema ) ,
23+ ) => {
24+ const newValue =
25+ typeof update === 'function' ? update ( get ( baseGameSettingsAtom ) ) : update ;
26+ set ( baseGameSettingsAtom , {
27+ players : ( newValue . players ?? [ ] ) . map ( ( player ) => ( {
28+ name : player ?. name ?? '' ,
29+ } ) ) ,
30+ numberOfSpies : newValue . numberOfSpies ?? '1' ,
31+ randomNumberOfSpies : newValue . randomNumberOfSpies ?? false ,
32+ } ) ;
33+ } ,
34+ ) ;
Original file line number Diff line number Diff line change @@ -76,18 +76,12 @@ export const LocalUsersForm: FC<{ dict: Dictionary; lang: string }> = ({
7676
7777 useEffect ( ( ) => {
7878 const { unsubscribe } = form . watch ( ( value ) => {
79- const result = localGameFormSchema . safeParse ( value ) ;
80- if ( form . formState . isDirty && result . success ) {
81- setGameSettings ( result . data ) ;
79+ if ( form . formState . isDirty ) {
80+ setGameSettings ( value ) ;
8281 }
8382 } ) ;
8483 return ( ) => unsubscribe ( ) ;
85- } , [
86- form . watch ,
87- form . formState . isDirty ,
88- localGameFormSchema ,
89- setGameSettings ,
90- ] ) ;
84+ } , [ form . watch , form . formState . isDirty , setGameSettings ] ) ;
9185
9286 useEffect ( ( ) => {
9387 if ( hasHydrated . current ) {
You can’t perform that action at this time.
0 commit comments