@@ -22,19 +22,30 @@ type CreateEventFormState = {
2222 description : string
2323}
2424
25- const initialFormState : CreateEventFormState = {
26- orgId : '' ,
27- title : '' ,
28- location : '' ,
29- eventTime : '' ,
30- description : '' ,
25+ function getCurrentDateTimeInputValue ( ) {
26+ const now = new Date ( )
27+ now . setSeconds ( 0 , 0 )
28+
29+ const timezoneOffset = now . getTimezoneOffset ( )
30+ const localDate = new Date ( now . getTime ( ) - timezoneOffset * 60_000 )
31+ return localDate . toISOString ( ) . slice ( 0 , 16 )
32+ }
33+
34+ function createInitialFormState ( ) : CreateEventFormState {
35+ return {
36+ orgId : '' ,
37+ title : '' ,
38+ location : '' ,
39+ eventTime : getCurrentDateTimeInputValue ( ) ,
40+ description : '' ,
41+ }
3142}
3243
3344function toEventTimeISOString ( value : string ) {
34- if ( ! value ) return undefined
45+ if ( ! value ) return null
3546
3647 const date = new Date ( value )
37- return Number . isNaN ( date . getTime ( ) ) ? undefined : date . toISOString ( )
48+ return Number . isNaN ( date . getTime ( ) ) ? null : date . toISOString ( )
3849}
3950
4051/**
@@ -54,18 +65,29 @@ export function CreateEventModal({ isOpen, onClose, onCreated }: CreateEventModa
5465 isLoading : isLoadingMyOrganizations ,
5566 error : myOrganizationsError ,
5667 } = useUserOrganizations ( organizations , user ?. uid , isAuthed , isOpen ? 1 : 0 )
57- const [ formState , setFormState ] = useState < CreateEventFormState > ( initialFormState )
68+ const [ formState , setFormState ] = useState < CreateEventFormState > ( ( ) => createInitialFormState ( ) )
5869 const [ isSubmitting , setIsSubmitting ] = useState ( false )
5970 const [ error , setError ] = useState < string | null > ( null )
6071
6172 useEffect ( ( ) => {
6273 if ( ! isOpen ) {
63- setFormState ( initialFormState )
74+ setFormState ( createInitialFormState ( ) )
6475 setIsSubmitting ( false )
6576 setError ( null )
6677 return
6778 }
6879
80+ setFormState ( ( current ) => {
81+ if ( current . eventTime . length > 0 ) {
82+ return current
83+ }
84+
85+ return {
86+ ...current ,
87+ eventTime : getCurrentDateTimeInputValue ( ) ,
88+ }
89+ } )
90+
6991 const onKeyDown = ( event : KeyboardEvent ) => {
7092 if ( event . key === 'Escape' && ! isSubmitting ) {
7193 onClose ( )
0 commit comments