@@ -35,6 +35,7 @@ import { getGroup } from '@/lib/api'
35
35
import { GroupFormValues , groupFormSchema } from '@/lib/schemas'
36
36
import { zodResolver } from '@hookform/resolvers/zod'
37
37
import { Save , Trash2 } from 'lucide-react'
38
+ import { useEffect , useState } from 'react'
38
39
import { useFieldArray , useForm } from 'react-hook-form'
39
40
40
41
export type Props = {
@@ -68,15 +69,25 @@ export function GroupForm({
68
69
keyName : 'key' ,
69
70
} )
70
71
71
- let activeUser = 'None'
72
+ const [ activeUser , setActiveUser ] = useState < string | null > ( null )
73
+ useEffect ( ( ) => {
74
+ if ( activeUser === null ) {
75
+ const currentActiveUser =
76
+ fields . find (
77
+ ( f ) => f . id === localStorage . getItem ( `${ group ?. id } -activeUser` ) ,
78
+ ) ?. name || 'None'
79
+ setActiveUser ( currentActiveUser )
80
+ }
81
+ } , [ activeUser , fields , group ?. id ] )
72
82
73
83
const updateActiveUser = ( ) => {
84
+ if ( ! activeUser ) return
74
85
if ( group ?. id ) {
75
86
const participant = group . participants . find ( ( p ) => p . name === activeUser )
76
87
if ( participant ?. id ) {
77
88
localStorage . setItem ( `${ group . id } -activeUser` , participant . id )
78
89
} else {
79
- localStorage . setItem ( `${ group . id } -newUser ` , activeUser )
90
+ localStorage . setItem ( `${ group . id } -activeUser ` , activeUser )
80
91
}
81
92
} else {
82
93
localStorage . setItem ( 'newGroup-activeUser' , activeUser )
@@ -228,39 +239,35 @@ export function GroupForm({
228
239
</ CardHeader >
229
240
< CardContent >
230
241
< div className = "grid sm:grid-cols-2 gap-4" >
231
- < FormItem >
232
- < FormLabel > Active user</ FormLabel >
233
- < FormControl >
234
- < Select
235
- onValueChange = { ( value ) => {
236
- activeUser = value
237
- } }
238
- defaultValue = {
239
- fields . find (
240
- ( f ) =>
241
- f . id ===
242
- localStorage . getItem ( `${ group ?. id } -activeUser` ) ,
243
- ) ?. name || 'None'
244
- }
245
- >
246
- < SelectTrigger >
247
- < SelectValue placeholder = "Select a participant" />
248
- </ SelectTrigger >
249
- < SelectContent >
250
- { [ { name : 'None' } , ...form . watch ( 'participants' ) ]
251
- . filter ( ( item ) => item . name . length > 0 )
252
- . map ( ( { name } ) => (
253
- < SelectItem key = { name } value = { name } >
254
- { name }
255
- </ SelectItem >
256
- ) ) }
257
- </ SelectContent >
258
- </ Select >
259
- </ FormControl >
260
- < FormDescription >
261
- User used as default for paying expenses.
262
- </ FormDescription >
263
- </ FormItem >
242
+ { activeUser !== null && (
243
+ < FormItem >
244
+ < FormLabel > Active user</ FormLabel >
245
+ < FormControl >
246
+ < Select
247
+ onValueChange = { ( value ) => {
248
+ setActiveUser ( value )
249
+ } }
250
+ defaultValue = { activeUser }
251
+ >
252
+ < SelectTrigger >
253
+ < SelectValue placeholder = "Select a participant" />
254
+ </ SelectTrigger >
255
+ < SelectContent >
256
+ { [ { name : 'None' } , ...form . watch ( 'participants' ) ]
257
+ . filter ( ( item ) => item . name . length > 0 )
258
+ . map ( ( { name } ) => (
259
+ < SelectItem key = { name } value = { name } >
260
+ { name }
261
+ </ SelectItem >
262
+ ) ) }
263
+ </ SelectContent >
264
+ </ Select >
265
+ </ FormControl >
266
+ < FormDescription >
267
+ User used as default for paying expenses.
268
+ </ FormDescription >
269
+ </ FormItem >
270
+ ) }
264
271
</ div >
265
272
</ CardContent >
266
273
</ Card >
0 commit comments