-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bookings countdown: extend bookings debug to allow for updates to ext…
…eded date as well * create BookingDateDebugController * update the UI to accommodate for extended functionality
- Loading branch information
Showing
9 changed files
with
243 additions
and
114 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
packages/client/src/controllers/BookingDateDebugController.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { DateTime } from "luxon"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import { BookingDateDebugDialog as DebugDialog } from "@eisbuk/ui"; | ||
import { updateLocalDocuments } from "@eisbuk/react-redux-firebase-firestore"; | ||
import { OrgSubCollection } from "@eisbuk/shared"; | ||
|
||
import { db } from "@/setup"; | ||
|
||
import { getOrganization } from "@/lib/getters"; | ||
|
||
import { | ||
getBookingsCustomer, | ||
getMonthDeadline, | ||
} from "@/store/selectors/bookings"; | ||
import { | ||
getCalendarDay, | ||
getSecretKey, | ||
getSystemDate, | ||
} from "@/store/selectors/app"; | ||
import { resetSystemDate, setSystemDate } from "@/store/actions/appActions"; | ||
|
||
import { | ||
FirestoreVariant, | ||
doc, | ||
getBookingsDocPath, | ||
getDoc, | ||
} from "@/utils/firestore"; | ||
|
||
const BookingDateDebugDialog: React.FC = () => { | ||
const secretKey = useSelector(getSecretKey) || ""; | ||
const currentDate = useSelector(getCalendarDay); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
const systemDateValue = useSelector(getSystemDate).value; | ||
const systemDate = useDate({ | ||
value: systemDateValue, | ||
onChange: (date) => dispatch(setSystemDate(date)), | ||
// Reset the system date on unmount, as it could have only been used (if it had been used at all) for debugging of | ||
// the current page | ||
onDestroy: () => dispatch(resetSystemDate()), | ||
}); | ||
|
||
const customer = useSelector(getBookingsCustomer(secretKey)); | ||
const extendedDateValue = DateTime.fromISO( | ||
customer?.extendedDate || getMonthDeadline(currentDate).toISODate() | ||
); | ||
// On extended date change, we're not persisting the change, merely updating the local (redux) state | ||
const handleExtendedDateChange = (date: DateTime) => { | ||
const data = { ...customer, extendedDate: date.toISODate() }; | ||
dispatch( | ||
updateLocalDocuments(OrgSubCollection.Bookings, { [secretKey]: data }) | ||
); | ||
}; | ||
const resetBookingsCustomer = async () => { | ||
const firestore = FirestoreVariant.client({ instance: db }); | ||
const docRef = doc( | ||
firestore, | ||
getBookingsDocPath(getOrganization(), secretKey) | ||
); | ||
const data = await getDoc(docRef).then((d) => d.data()!); | ||
dispatch( | ||
updateLocalDocuments(OrgSubCollection.Bookings, { [secretKey]: data }) | ||
); | ||
}; | ||
const extendedDate = useDate({ | ||
value: extendedDateValue, | ||
onChange: handleExtendedDateChange, | ||
onDestroy: resetBookingsCustomer, | ||
}); | ||
|
||
return <DebugDialog systemDate={systemDate} extendedDate={extendedDate} />; | ||
}; | ||
|
||
type UseDateParams = { | ||
value: DateTime; | ||
onChange: (date: DateTime) => void; | ||
onDestroy?: () => void; | ||
}; | ||
|
||
const useDate = ({ value, onChange, onDestroy }: UseDateParams) => { | ||
useEffect(() => { | ||
return onDestroy; | ||
}, []); | ||
|
||
const navigate = (days: -1 | 1) => () => onChange(value.plus({ days })); | ||
|
||
const [localDate, setLocalDate] = useState(value.toISODate()); | ||
useEffect(() => { | ||
setLocalDate(value.toISODate()); | ||
}, [value]); | ||
const handleChange = (_date: string) => { | ||
if (isIsoDate(_date)) { | ||
// If date is a valid ISO string, update the DateTime value | ||
// Local value is updated as side effect | ||
onChange(DateTime.fromISO(_date)); | ||
} | ||
// Update the local value | ||
setLocalDate(_date); | ||
}; | ||
|
||
return { value: localDate, navigate, onChange: handleChange }; | ||
}; | ||
|
||
const isIsoDate = (date: string): boolean => /^\d{4}-\d{2}-\d{2}$/.test(date); | ||
|
||
export default BookingDateDebugDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/ui/src/BookingDateDebugDialog/BookingDateDebugDialog.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react"; | ||
import { ComponentMeta } from "@storybook/react"; | ||
import { DateTime } from "luxon"; | ||
|
||
import BookingDateDebugDialog from "./BookingDateDebugDialog"; | ||
|
||
export default { | ||
title: "Booking Date Debug Dialog", | ||
component: BookingDateDebugDialog, | ||
} as ComponentMeta<typeof BookingDateDebugDialog>; | ||
|
||
export const Interactive = (): JSX.Element => { | ||
const [_systemDate, setSystemDate] = React.useState( | ||
DateTime.now().toISODate() | ||
); | ||
const systemDate = { | ||
value: _systemDate, | ||
onChange: setSystemDate, | ||
navigate: (delta: -1 | 1) => () => | ||
setSystemDate( | ||
DateTime.fromISO(_systemDate).plus({ days: delta }).toISODate() | ||
), | ||
}; | ||
|
||
const [_extendedDate, setExtendedDate] = React.useState( | ||
DateTime.now().toISODate() | ||
); | ||
const extendedDate = { | ||
value: _extendedDate, | ||
onChange: setExtendedDate, | ||
navigate: (delta: -1 | 1) => () => | ||
setExtendedDate( | ||
DateTime.fromISO(_extendedDate).plus({ days: delta }).toISODate() | ||
), | ||
}; | ||
|
||
return ( | ||
<BookingDateDebugDialog | ||
systemDate={systemDate} | ||
extendedDate={extendedDate} | ||
/> | ||
); | ||
}; |
50 changes: 50 additions & 0 deletions
50
packages/ui/src/BookingDateDebugDialog/BookingDateDebugDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react"; | ||
|
||
import { DateNavigation } from "../CalendarNav"; | ||
|
||
type DateFieldProps = { | ||
value: string; | ||
navigate: (days: -1 | 1) => () => void; | ||
onChange: (date: string) => void; | ||
onDestroy?: () => void; | ||
}; | ||
|
||
type Props = { | ||
systemDate: DateFieldProps; | ||
extendedDate: DateFieldProps; | ||
}; | ||
|
||
const DateDebug: React.FC<Props> = ({ systemDate, extendedDate }) => ( | ||
<div className="flex items-center justify-between flex-wrap gap-4 rounded-lg p-4 border-2 border-gray-300"> | ||
<div className="flex items-center gap-4"> | ||
<span className="text-base font-semibold">System date:</span> | ||
<DateNavigation | ||
className="w-full md:w-[320px] border-gray-300 border-2 rounded" | ||
onPrev={systemDate.navigate(-1)} | ||
onNext={systemDate.navigate(1)} | ||
> | ||
<input | ||
className="h-full w-full border-none text-center" | ||
value={systemDate.value} | ||
onChange={(e) => systemDate.onChange(e.target.value)} | ||
/> | ||
</DateNavigation> | ||
</div> | ||
|
||
<div className="flex items-center gap-4"> | ||
<span className="text-base font-semibold">Extended booking date:</span> | ||
<DateNavigation | ||
className="w-full md:w-[320px] border-gray-300 border-2 rounded" | ||
onPrev={extendedDate.navigate(-1)} | ||
onNext={extendedDate.navigate(1)} | ||
> | ||
<input | ||
className="h-full w-full border-none text-center" | ||
value={extendedDate.value} | ||
onChange={(e) => extendedDate.onChange(e.target.value)} | ||
/> | ||
</DateNavigation> | ||
</div> | ||
</div> | ||
); | ||
export default DateDebug; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "./BookingDateDebugDialog"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.