Skip to content

Commit

Permalink
Fix date issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Sep 22, 2023
1 parent 799ad3b commit 2b80e2d
Show file tree
Hide file tree
Showing 21 changed files with 336 additions and 134 deletions.
24 changes: 19 additions & 5 deletions backend/src/components/BookingFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,37 @@ const BookingFilter = ({
<FormControl fullWidth margin="dense">
<DatePicker
label={commonStrings.FROM}
onChange={(from: Date) => {
setFrom(from)
setMinDate(from)
onChange={(date) => {
if (date) {

if (to && to.getTime() <= date.getTime()) {
setTo(undefined)
}

const minDate = new Date(date)
minDate.setDate(date.getDate() + 1)
setMinDate(minDate)

setFrom(date)
} else {
setMinDate(undefined)
}
}}
language={language}
variant="standard"
value={from}
/>
</FormControl>
<FormControl fullWidth margin="dense">
<DatePicker
label={commonStrings.TO}
minDate={minDate}
onChange={(to: Date) => {
setTo(to)
onChange={(date) => {
setTo(date || undefined)
}}
language={language}
variant="standard"
value={to}
/>
</FormControl>
<FormControl fullWidth margin="dense">
Expand Down
25 changes: 18 additions & 7 deletions backend/src/components/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { DatePicker as MuiDatePicker } from '@mui/x-date-pickers/DatePicker'
Expand All @@ -13,6 +13,7 @@ const DatePicker = (
required,
language,
variant,
readOnly,
onChange
}
: {
Expand All @@ -22,22 +23,32 @@ const DatePicker = (
required?: boolean
language?: string
variant?: TextFieldVariants
onChange: (value: Date) => void
readOnly?: boolean
onChange?: (value: Date | null) => void
}
) => {
const [value, setValue] = useState(dateValue || null)
const [value, setValue] = useState<Date | null>(null)

useEffect(() => {
setValue(dateValue || null)
}, [dateValue])

return (
<LocalizationProvider adapterLocale={language === 'fr' ? fr : enUS} dateAdapter={AdapterDateFns}>
<MuiDatePicker
label={label}
views={['year', 'month', 'day']}
value={value}
onChange={(value) => {
const date = value as Date
setValue(date)
readOnly={readOnly}
onAccept={(value) => {
if (value) {
const date = value as Date
date.setHours(10, 0, 0, 0)
}
setValue(value)

if (onChange) {
onChange(date)
onChange(value)
}
}}
minDate={minDate}
Expand Down
19 changes: 13 additions & 6 deletions backend/src/components/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { DateTimePicker as MuiDateTimePicker } from '@mui/x-date-pickers/DateTimePicker'
Expand All @@ -13,6 +13,7 @@ const DateTimePicker = (
required,
variant,
language,
readOnly,
onChange
}
: {
Expand All @@ -22,22 +23,28 @@ const DateTimePicker = (
required?: boolean
language?: string
variant?: TextFieldVariants
onChange: (value: Date) => void
readOnly?: boolean
onChange: (value: Date | null) => void
}
) => {
const [value, setValue] = useState(dateTimeValue || null)

useEffect(() => {
setValue(dateTimeValue || null)
}, [dateTimeValue])

return (
<LocalizationProvider adapterLocale={language === 'fr' ? fr : enUS} dateAdapter={AdapterDateFns}>
<MuiDateTimePicker
label={label}
// showToolbar
value={value}
onChange={(value) => {
const date = value as Date
setValue(date)
readOnly={readOnly}
onAccept={(value) => {
setValue(value)

if (onChange) {
onChange(date)
onChange(value)
}
}}
minDate={minDate}
Expand Down
36 changes: 23 additions & 13 deletions backend/src/pages/CreateBooking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const CreateBooking = () => {
const [collisionDamageWaiver, setCollisionDamageWaiver] = useState(false)
const [fullInsurance, setFullInsurance] = useState(false)
const [additionalDriver, setAdditionalDriver] = useState(false)
const [minDate, setMinDate] = useState(new Date())
const [minDate, setMinDate] = useState<Date>()
const [_fullName, set_FullName] = useState('')
const [_email, set_Email] = useState('')
const [_phone, set_Phone] = useState('')
Expand Down Expand Up @@ -308,14 +308,22 @@ const CreateBooking = () => {
<DateTimePicker
label={commonStrings.FROM}
value={from}
minDate={new Date()}
required
onChange={(from: Date) => {
setFrom(from)
onChange={(date) => {
if (date) {

const minDate = new Date(from)
minDate.setDate(minDate.getDate() + 1)
setMinDate(minDate)
if (to && to.getTime() <= date.getTime()) {
setTo(undefined)
}

const minDate = new Date(date)
minDate.setDate(minDate.getDate() + 1)
setMinDate(minDate)

setFrom(date)
} else {
setMinDate(undefined)
}
}}
language={UserService.getLanguage()}
/>
Expand All @@ -327,8 +335,8 @@ const CreateBooking = () => {
value={to}
minDate={minDate}
required
onChange={(to: Date) => {
setTo(to)
onChange={(date) => {
setTo(date||undefined)
}}
language={UserService.getLanguage()}
/>
Expand Down Expand Up @@ -463,11 +471,13 @@ const CreateBooking = () => {
<DatePicker
label={commonStrings.BIRTH_DATE}
required
onChange={(_birthDate: Date) => {
const _birthDateValid = _validateBirthDate(_birthDate)
onChange={(_birthDate) => {
if (_birthDate) {
const _birthDateValid = _validateBirthDate(_birthDate)

set_BirthDate(_birthDate)
set_BirthDateValid(_birthDateValid)
set_BirthDate(_birthDate)
set_BirthDateValid(_birthDateValid)
}
}}
language={UserService.getLanguage()}
/>
Expand Down
10 changes: 6 additions & 4 deletions backend/src/pages/CreateUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,13 @@ const CreateUser = () => {
label={strings.BIRTH_DATE}
value={birthDate}
required
onChange={(birthDate: Date) => {
const birthDateValid = validateBirthDate(birthDate)
onChange={(birthDate) => {
if (birthDate) {
const birthDateValid = validateBirthDate(birthDate)

setBirthDate(birthDate)
setBirthDateValid(birthDateValid)
setBirthDate(birthDate)
setBirthDateValid(birthDateValid)
}
}}
language={(user && user.language) || Env.DEFAULT_LANGUAGE}
/>
Expand Down
35 changes: 22 additions & 13 deletions backend/src/pages/UpdateBooking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,25 @@ const UpdateBooking = () => {
label={commonStrings.FROM}
value={from}
required
onChange={(from: Date) => {
if (from) {
booking.from = from
onChange={(date) => {
if (date) {
booking.from = date

Helper.price(
booking,
booking.car as bookcarsTypes.Car,
(price) => {
setBooking(booking)
setPrice(price)
setFrom(from)
setMinDate(from)
setFrom(date)

const minDate = new Date(date)
minDate.setDate(minDate.getDate() + 1)
setMinDate(minDate)

if (to && to.getTime() <= date.getTime()) {
setTo(undefined)
}
},
(err) => {
toastErr(err)
Expand All @@ -590,17 +597,17 @@ const UpdateBooking = () => {
value={to}
minDate={minDate}
required
onChange={(to: Date) => {
if (to) {
booking.to = to
onChange={(date) => {
if (date) {
booking.to = date

Helper.price(
booking,
booking.car as bookcarsTypes.Car,
(price) => {
setBooking(booking)
setPrice(price)
setTo(to)
setTo(date)
},
(err) => {
toastErr(err)
Expand Down Expand Up @@ -740,10 +747,12 @@ const UpdateBooking = () => {
label={commonStrings.BIRTH_DATE}
value={_birthDate}
required
onChange={(_birthDate: Date) => {
const _birthDateValid = _validateBirthDate(_birthDate)
set_BirthDate(_birthDate)
set_BirthDateValid(_birthDateValid)
onChange={(_birthDate) => {
if (_birthDate) {
const _birthDateValid = _validateBirthDate(_birthDate)
set_BirthDate(_birthDate)
set_BirthDateValid(_birthDateValid)
}
}}
language={UserService.getLanguage()}
/>
Expand Down
10 changes: 6 additions & 4 deletions backend/src/pages/UpdateUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,13 @@ const UpdateUser = () => {
label={cuStrings.BIRTH_DATE}
value={birthDate}
required
onChange={(birthDate: Date) => {
const birthDateValid = validateBirthDate(birthDate)
onChange={(birthDate) => {
if (birthDate) {
const birthDateValid = validateBirthDate(birthDate)

setBirthDate(birthDate)
setBirthDateValid(birthDateValid)
setBirthDate(birthDate)
setBirthDateValid(birthDateValid)
}
}}
language={(user && user.language) || Env.DEFAULT_LANGUAGE}
/>
Expand Down
24 changes: 19 additions & 5 deletions frontend/src/components/BookingFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,37 @@ const BookingFilter = ({
<FormControl fullWidth margin="dense">
<DatePicker
label={commonStrings.FROM}
onChange={(from: Date) => {
setFrom(from)
setMinDate(from)
onChange={(date) => {
if (date) {

if (to && to.getTime() <= date.getTime()) {
setTo(undefined)
}

const minDate = new Date(date)
minDate.setDate(date.getDate() + 1)
setMinDate(minDate)

setFrom(date)
} else {
setMinDate(undefined)
}
}}
language={language}
variant="standard"
value={from}
/>
</FormControl>
<FormControl fullWidth margin="dense">
<DatePicker
label={commonStrings.TO}
minDate={minDate}
onChange={(to: Date) => {
setTo(to)
onChange={(date) => {
setTo(date || undefined)
}}
language={language}
variant="standard"
value={to}
/>
</FormControl>
<FormControl fullWidth margin="dense">
Expand Down
Loading

0 comments on commit 2b80e2d

Please sign in to comment.