Skip to content

Commit

Permalink
Add a second time slots query
Browse files Browse the repository at this point in the history
In case max bookable day count exceeds 90 days, fetch a second batch of
time slots.
  • Loading branch information
lyyder committed Aug 16, 2018
1 parent 91d4c80 commit 30a49d7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 48 additions & 16 deletions src/containers/ListingPage/ListingPage.duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 30a49d7

Please sign in to comment.