Skip to content

Commit 33ca4f6

Browse files
committed
Ask for event date when new event + autoFocus
1 parent 894c2b2 commit 33ca4f6

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/events/new/NewEventDialog.tsx

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Button, Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material'
2-
import * as React from 'react'
1+
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, Typography } from '@mui/material'
2+
import { useEffect } from 'react'
33
import { useSelector } from 'react-redux'
44
import { selectUserIdOpenPlanner } from '../../auth/authReducer'
55
import { FormContainer, TextFieldElement, useForm } from 'react-hook-form-mui'
@@ -12,6 +12,7 @@ import { NewEvent } from '../../types'
1212
import { serverTimestamp } from 'firebase/firestore'
1313
import { useNotification } from '../../hooks/notificationHook'
1414
import { generateApiKey } from '../../utils/generateApiKey'
15+
1516
export type NewEventDialogProps = {
1617
isOpen: boolean
1718
onClose: (eventId: string | null) => void
@@ -20,6 +21,8 @@ export type NewEventDialogProps = {
2021
const schema = yup
2122
.object({
2223
name: yup.string().required(),
24+
startDate: yup.date().nullable().optional(),
25+
endDate: yup.date().nullable().optional(),
2326
})
2427
.required()
2528

@@ -28,12 +31,18 @@ export const NewEventDialog = ({ isOpen, onClose }: NewEventDialogProps) => {
2831

2932
const { createNotification } = useNotification()
3033
const formContext = useForm()
31-
const { formState } = formContext
34+
const { formState, watch, setValue } = formContext
3235

3336
const mutation = useFirestoreCollectionMutation(collections.events)
3437

3538
return (
36-
<Dialog open={isOpen} onClose={() => onClose(null)} maxWidth="md" fullWidth={true} scroll="body">
39+
<Dialog
40+
open={isOpen}
41+
onClose={() => onClose(null)}
42+
maxWidth="md"
43+
fullWidth={true}
44+
scroll="body"
45+
disableRestoreFocus={true}>
3746
<DialogTitle>New event creation</DialogTitle>
3847

3948
<FormContainer
@@ -48,8 +57,8 @@ export const NewEventDialog = ({ isOpen, onClose }: NewEventDialogProps) => {
4857
scheduleVisible: true,
4958
conferenceHallId: null,
5059
dates: {
51-
start: null,
52-
end: null,
60+
start: data.startDate ? new Date(data.startDate) : null,
61+
end: data.endDate ? new Date(data.endDate) : null,
5362
},
5463
formats: [],
5564
categories: [],
@@ -97,6 +106,38 @@ export const NewEventDialog = ({ isOpen, onClose }: NewEventDialogProps) => {
97106
label="Event name"
98107
name="name"
99108
variant="filled"
109+
autoFocus={isOpen}
110+
disabled={formState.isSubmitting}
111+
/>
112+
<Typography sx={{ marginTop: 2 }}>Event dates (optional, can be set later on)</Typography>
113+
<TextFieldElement
114+
margin="normal"
115+
fullWidth
116+
id="startDate"
117+
label="Start date (optional)"
118+
name="startDate"
119+
type="datetime-local"
120+
InputLabelProps={{ shrink: true }}
121+
variant="filled"
122+
disabled={formState.isSubmitting}
123+
onBlur={(e) => {
124+
if (e.target.value && !watch('endDate')) {
125+
const start = new Date(e.target.value)
126+
const end = new Date(start)
127+
end.setHours(end.getHours() + 8)
128+
setValue('endDate', end.toISOString().slice(0, 16))
129+
}
130+
}}
131+
/>
132+
<TextFieldElement
133+
margin="normal"
134+
fullWidth
135+
id="endDate"
136+
label="End date (optional)"
137+
name="endDate"
138+
variant="filled"
139+
type="datetime-local"
140+
InputLabelProps={{ shrink: true }}
100141
disabled={formState.isSubmitting}
101142
/>
102143
</DialogContent>

0 commit comments

Comments
 (0)