22
33import Background from '@/../public/background.png' ;
44import NebulaLogo from '@/components/NebulaLogo' ;
5- import snapTime , { defaultEndTime , defaultStartTime } from '@/lib/snapTime' ;
5+ import {
6+ defaultEndTime ,
7+ defaultStartTime ,
8+ snapTime ,
9+ validTime ,
10+ } from '@/lib/timeUtils' ;
611import { Checkbox , CircularProgress , FormControlLabel } from '@mui/material' ;
712import Button from '@mui/material/Button' ;
813import { DatePicker } from '@mui/x-date-pickers/DatePicker' ;
914import { TimePicker } from '@mui/x-date-pickers/TimePicker' ;
1015import dayjs , { Dayjs } from 'dayjs' ;
1116import Image from 'next/image' ;
1217import { useRouter } from 'next/navigation' ;
13- import React , { useRef , useState , useTransition } from 'react' ;
18+ import React , { useState , useTransition } from 'react' ;
1419
1520/**
1621 * Returns the home page with Nebula Branding and search options
@@ -40,10 +45,6 @@ export default function Home() {
4045 dayjs ( endTime , 'HH:mm' ) . isBefore ( dayjs ( startTime , 'HH:mm' ) ) ,
4146 ) ;
4247
43- // for saving the input values on change but only updating them onBlur or onKeyDown+enter
44- const startTimeChange = useRef < Dayjs | null > ( startTime ) ;
45- const endTimeChange = useRef < Dayjs | null > ( endTime ) ;
46-
4748 const [ nearby , setNearby ] = useState ( false ) ;
4849
4950 // only show checkbox if location is possible
@@ -134,10 +135,8 @@ export default function Home() {
134135 label = "Start time"
135136 value = { startTime }
136137 timeSteps = { { minutes : 15 } }
137- onChange = { ( newValue ) => ( startTimeChange . current = newValue ) }
138- onAccept = { ( newValue ) =>
139- setStartTime ( newValue == null ? null : snapTime ( newValue ) )
140- }
138+ onChange = { ( newValue ) => setStartTime ( newValue ) }
139+ onAccept = { ( newValue ) => setStartTime ( snapTime ( validTime ( newValue ) ) ) }
141140 className = "w-full"
142141 slotProps = { {
143142 actionBar : {
@@ -151,35 +150,15 @@ export default function Home() {
151150 } ,
152151 error : error ,
153152 helperText : error && 'Start time must be before end time' ,
154- onBlur : ( ) => {
155- setStartTime (
156- startTimeChange . current == null ||
157- ! startTimeChange . current . isValid ( )
158- ? null
159- : snapTime ( startTimeChange . current ) ,
160- ) ;
161- } ,
162- onKeyDown : ( e ) => {
163- if ( e . key === 'Enter' ) {
164- setStartTime (
165- startTimeChange . current == null ||
166- ! startTimeChange . current . isValid ( )
167- ? null
168- : snapTime ( startTimeChange . current ) ,
169- ) ;
170- }
171- } ,
172153 } ,
173154 } }
174155 />
175156 < TimePicker
176157 label = "End time"
177158 value = { endTime }
178159 timeSteps = { { minutes : 15 } }
179- onChange = { ( newValue ) => ( endTimeChange . current = newValue ) }
180- onAccept = { ( newValue ) =>
181- setEndTime ( newValue == null ? null : snapTime ( newValue ) )
182- }
160+ onChange = { ( newValue ) => setEndTime ( newValue ) }
161+ onAccept = { ( newValue ) => setEndTime ( snapTime ( validTime ( newValue ) ) ) }
183162 className = "w-full"
184163 slotProps = { {
185164 actionBar : {
@@ -193,25 +172,6 @@ export default function Home() {
193172 } ,
194173 error : error ,
195174 helperText : error && 'Start time must be before end time' ,
196- onBlur : ( ) => {
197- console . log ( endTimeChange . current ) ;
198- setEndTime (
199- endTimeChange . current == null ||
200- ! endTimeChange . current . isValid ( )
201- ? null
202- : snapTime ( endTimeChange . current ) ,
203- ) ;
204- } ,
205- onKeyDown : ( e ) => {
206- if ( e . key === 'Enter' ) {
207- setEndTime (
208- endTimeChange . current == null ||
209- ! endTimeChange . current . isValid ( )
210- ? null
211- : snapTime ( endTimeChange . current ) ,
212- ) ;
213- }
214- } ,
215175 } ,
216176 } }
217177 />
0 commit comments