Skip to content

Commit

Permalink
Fix: date handling in VehicleScheduler and CarFilter components
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Jan 23, 2025
1 parent 3024d9c commit 82706f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
11 changes: 7 additions & 4 deletions backend/src/components/VehicleScheduler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,24 @@ const VehicleScheduler = (
}
]

const dateBetween = new Date(query.end.getTime() - Math.ceil(query.end.getTime() - query.start.getTime()) / 2)
dateBetween.setHours(23, 59, 0, 0)

const payload: bookcarsTypes.GetBookingsPayload = {
suppliers,
statuses,
filter: {
// from: query.view !== 'day' ? query.start : undefined,
from: query.view !== 'day' ? new Date(query.start.getFullYear(), query.start.getMonth() - 1, 1) : undefined,
dateBetween: query.view === 'day' ? new Date(query.end.getFullYear(), query.end.getMonth(), query.end.getDate(), 0, 0, 0) : undefined,
// to: query.view === 'month' ? query.end : new Date(query.end.getFullYear(), query.end.getMonth() + 1, 0),
to: new Date(query.end.getFullYear(), query.end.getMonth() + 1, 0),
dateBetween: query.view === 'day' ? dateBetween : undefined,
to: query.view === 'month' ? new Date(query.end.getFullYear(), query.end.getMonth() + 1, 0) : new Date(query.end.getFullYear(), query.end.getMonth() + 2, 0),
pickupLocation: filter?.pickupLocation,
dropOffLocation: filter?.dropOffLocation,
keyword: filter?.keyword,
},
user: (user && user._id) || undefined,
}
console.log(payload.filter?.dateBetween)
console.log(payload.filter?.to)

const data = await BookingService.getBookings(payload, 1, 10000)
const _data = data && data.length > 0 ? data[0] : { pageInfo: { totalRecord: 0 }, resultData: [] }
Expand Down
32 changes: 13 additions & 19 deletions frontend/src/components/CarFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const CarFilter = ({
const [from, setFrom] = useState<Date | undefined>(filterFrom)
const [to, setTo] = useState<Date | undefined>(filterTo)
const [minDate, setMinDate] = useState<Date>()
const [maxDate, setMaxDate] = useState<Date>()
const [pickupLocation, setPickupLocation] = useState<bookcarsTypes.Location | null | undefined>(filterPickupLocation)
const [dropOffLocation, setDropOffLocation] = useState<bookcarsTypes.Location | null | undefined>(filterDropOffLocation)
const [sameLocation, setSameLocation] = useState(filterPickupLocation === filterDropOffLocation)
Expand All @@ -57,14 +56,6 @@ const CarFilter = ({
}
}, [filterFrom])

useEffect(() => {
if (filterTo) {
const __maxDate = new Date(filterTo)
__maxDate.setDate(__maxDate.getDate() - 1)
setMaxDate(__maxDate)
}
}, [filterTo])

useEffect(() => {
setPickupLocation(filterPickupLocation)
}, [filterPickupLocation])
Expand Down Expand Up @@ -163,7 +154,7 @@ const CarFilter = ({
label={strings.PICK_UP_DATE}
value={from}
minDate={_minDate}
maxDate={maxDate}
// maxDate={maxDate}
variant="standard"
required
onChange={(date) => {
Expand All @@ -173,6 +164,11 @@ const CarFilter = ({
setFrom(date)
setMinDate(__minDate)
setFromError(false)
if (to!.getTime() - date.getTime() < 24 * 60 * 60 * 1000) {
const _to = new Date(date)
_to.setDate(_to.getDate() + 3)
setTo(_to)
}
} else {
setFrom(undefined)
setMinDate(_minDate)
Expand All @@ -197,14 +193,10 @@ const CarFilter = ({
required
onChange={(date) => {
if (date) {
const _maxDate = new Date(date)
_maxDate.setDate(_maxDate.getDate() - 1)
setTo(date)
setMaxDate(_maxDate)
setToError(false)
} else {
setTo(undefined)
setMaxDate(undefined)
}
}}
onError={(err: DateTimeValidationError) => {
Expand Down Expand Up @@ -261,7 +253,7 @@ const CarFilter = ({
label={strings.PICK_UP_DATE}
value={from}
minDate={_minDate}
maxDate={maxDate}
// maxDate={maxDate}
variant="standard"
required
onChange={(date) => {
Expand All @@ -271,6 +263,12 @@ const CarFilter = ({
setFrom(date)
setMinDate(__minDate)
setFromError(false)

if (to!.getTime() - date.getTime() < 24 * 60 * 60 * 1000) {
const _to = new Date(date)
_to.setDate(_to.getDate() + 3)
setTo(_to)
}
} else {
setFrom(undefined)
setMinDate(_minDate)
Expand Down Expand Up @@ -325,14 +323,10 @@ const CarFilter = ({
required
onChange={(date) => {
if (date) {
const _maxDate = new Date(date)
_maxDate.setDate(_maxDate.getDate() - 1)
setTo(date)
setMaxDate(_maxDate)
setToError(false)
} else {
setTo(undefined)
setMaxDate(undefined)
}
}}
onError={(err: DateTimeValidationError) => {
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/components/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ const SearchForm = ({
setFrom(date)
setMinDate(__minDate)
setFromError(false)
const _to = new Date(date)
_to.setDate(date.getDate() + 3)
setTo(_to)

if (to!.getTime() - date.getTime() < 24 * 60 * 60 * 1000) {
const _to = new Date(date)
_to.setDate(_to.getDate() + 3)
setTo(_to)
}
} else {
setFrom(undefined)
setMinDate(_minDate)
Expand Down

0 comments on commit 82706f8

Please sign in to comment.