Add a second time slots query

In case max bookable day count exceeds 90 days, fetch a second batch of
time slots.
This commit is contained in:
Hannu Lyytikainen 2018-08-16 14:47:15 +03:00
parent 91d4c808de
commit 30a49d7e18
2 changed files with 50 additions and 17 deletions

View file

@ -42,7 +42,8 @@ const fetchAvailableTimeSlots = true;
// A maximum number of days forwards during which a booking can be made.
// This is limited due to Stripe holding funds up to 90 days from the
// moment they are charged.
// moment they are charged. Also note that available time slots can only
// be fetched for 180 days in the future.
const dayCountAvailableForBooking = 90;
// To pass environment variables to the client app in the build

View file

@ -185,13 +185,54 @@ export const fetchReviews = listingId => (dispatch, getState, sdk) => {
});
};
export const fetchTimeSlots = params => (dispatch, getState, sdk) => {
const timeSlotsRequest = params => (dispatch, getState, sdk) => {
return sdk.timeslots.query(params).then(response => {
return denormalisedResponseEntities(response);
});
};
export const fetchTimeSlots = listingId => (dispatch, getState, sdk) => {
dispatch(fetchTimeSlotsRequest);
return sdk.timeslots
.query(params)
.then(response => {
const timeSlots = denormalisedResponseEntities(response);
dispatch(fetchTimeSlotsSuccess(timeSlots));
// Time slots can be fetched for 90 days at a time,
// for at most 180 days from now. If max number of bookable
// day exceeds 90, a second request is made.
const maxTimeSlots = 90;
// booking range: today + bookable days -1
const bookingRange = config.dayCountAvailableForBooking - 1;
const timeSlotsRange = Math.min(bookingRange, maxTimeSlots);
const start = moment().toDate();
const end = moment()
.add(timeSlotsRange, 'days')
.toDate();
const params = { listingId, start, end };
return dispatch(timeSlotsRequest(params))
.then(timeSlots => {
const secondRequest = bookingRange > maxTimeSlots;
// time slots request end can be at most 179 days from now
const secondMaxTimeSlots = maxTimeSlots - 1;
if (secondRequest) {
const secondRange = Math.min(secondMaxTimeSlots, bookingRange - maxTimeSlots);
const secondParams = {
listingId,
start: end,
end: moment(end)
.add(secondRange, 'days')
.toDate(),
};
dispatch(timeSlotsRequest(secondParams)).then(secondBatch => {
const combined = timeSlots.concat(secondBatch);
dispatch(fetchTimeSlotsSuccess(combined));
});
} else {
dispatch(fetchTimeSlotsSuccess(timeSlots));
}
})
.catch(e => {
dispatch(fetchTimeSlotsError(storableError(e)));
@ -231,18 +272,9 @@ export const loadData = (params, search) => dispatch => {
}
if (config.fetchAvailableTimeSlots) {
// fetch time slots for 90 days starting today
// as the booking can only be done for 90 days
// in the future due to Stripe limitations
const start = moment().toDate();
const end = moment()
.add(config.dayCountAvailableForBooking, 'days')
.toDate();
const timeSlotsParams = { listingId, start, end };
return Promise.all([
dispatch(showListing(listingId)),
dispatch(fetchTimeSlots(timeSlotsParams)),
dispatch(fetchTimeSlots(listingId)),
dispatch(fetchReviews(listingId)),
]);
} else {