From e8ed94d84989aca252dbc82424cdc920c625ca65 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Sat, 8 May 2021 21:53:10 +0000 Subject: [PATCH] Default [] value for locations array on booking page --- pages/[user]/book.tsx | 39 +++++++++++++++++++++++++-------------- pages/success.tsx | 10 +++++----- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/pages/[user]/book.tsx b/pages/[user]/book.tsx index ad59279ec7..bb3ae85e26 100644 --- a/pages/[user]/book.tsx +++ b/pages/[user]/book.tsx @@ -13,13 +13,16 @@ import { LocationType } from '../../lib/location'; export default function Book(props) { const router = useRouter(); const { date, user } = router.query; - const [ selectedLocation, setSelectedLocation ] = useState(props.eventType.locations.length === 1 ? props.eventType.locations[0].type : ''); + + const locations = props.eventType.locations || []; + + const [ selectedLocation, setSelectedLocation ] = useState(locations.length === 1 ? locations[0].type : ''); const telemetry = useTelemetry(); useEffect(() => { telemetry.withJitsu(jitsu => jitsu.track(telemetryEventTypes.timeSelected, collectPageParameters())); }); - const locationInfo = (type: LocationType) => props.eventType.locations.find( + const locationInfo = (type: LocationType) => locations.find( (location) => location.type === type ); @@ -32,20 +35,23 @@ export default function Book(props) { const bookingHandler = event => { event.preventDefault(); - const locationText = selectedLocation === LocationType.Phone ? event.target.phone.value : locationInfo(selectedLocation).address; + let payload = { + start: dayjs(date).format(), + end: dayjs(date).add(props.eventType.length, 'minute').format(), + name: event.target.name.value, + email: event.target.email.value, + notes: event.target.notes.value + }; + + if (selectedLocation) { + payload['location'] = selectedLocation === LocationType.Phone ? event.target.phone.value : locationInfo(selectedLocation).address; + } telemetry.withJitsu(jitsu => jitsu.track(telemetryEventTypes.bookingConfirmed, collectPageParameters())); const res = fetch( '/api/book/' + user, { - body: JSON.stringify({ - start: dayjs(date).format(), - end: dayjs(date).add(props.eventType.length, 'minute').format(), - name: event.target.name.value, - email: event.target.email.value, - location: locationText, - notes: event.target.notes.value - }), + body: JSON.stringify(payload), headers: { 'Content-Type': 'application/json' }, @@ -53,7 +59,12 @@ export default function Book(props) { } ); - router.push(`/success?date=${date}&type=${props.eventType.id}&user=${props.user.username}&location=${encodeURIComponent(locationText)}`); + let successUrl = `/success?date=${date}&type=${props.eventType.id}&user=${props.user.username}`; + if (payload['location']) { + successUrl += "&location=" + encodeURIComponent(payload['location']); + } + + router.push(successUrl); } return ( @@ -98,10 +109,10 @@ export default function Book(props) { - {props.eventType.locations.length > 1 && ( + {locations.length > 1 && (
Location - {props.eventType.locations.map( (location) => ( + {locations.map( (location) => (