diff --git a/src/components/FieldDateInput/DateInput.js b/src/components/FieldDateInput/DateInput.js index f2569932..87bb843c 100644 --- a/src/components/FieldDateInput/DateInput.js +++ b/src/components/FieldDateInput/DateInput.js @@ -16,8 +16,9 @@ import { intlShape, injectIntl } from 'react-intl'; import classNames from 'classnames'; import moment from 'moment'; import config from '../../config'; -import { propTypes } from '../../util/types'; +import { propTypes, TIME_SLOT_DAY } from '../../util/types'; import { dateFromAPIToLocalNoon } from '../../util/dates'; +import { ensureTimeSlot } from '../../util/data'; import NextMonthIcon from './NextMonthIcon'; import PreviousMonthIcon from './PreviousMonthIcon'; @@ -106,12 +107,16 @@ const defaultProps = { // Checks if time slot (propTypes.timeSlot) start time equals a day (moment) const timeSlotEqualsDay = (timeSlot, day) => { - // Time slots describe available dates by providing a start and - // an end date which is the following day. In the single date picker - // the start date is used to represent available dates. - const localStartDate = dateFromAPIToLocalNoon(timeSlot.attributes.start); + if (ensureTimeSlot(timeSlot).attributes.type === TIME_SLOT_DAY) { + // Time slots describe available dates by providing a start and + // an end date which is the following day. In the single date picker + // the start date is used to represent available dates. + const localStartDate = dateFromAPIToLocalNoon(timeSlot.attributes.start); - return isSameDay(day, moment(localStartDate)); + return isSameDay(day, moment(localStartDate)); + } else { + return false; + } }; class DateInputComponent extends Component { diff --git a/src/util/data.js b/src/util/data.js index 45595c7b..8d53d015 100644 --- a/src/util/data.js +++ b/src/util/data.js @@ -133,7 +133,7 @@ export const denormalisedResponseEntities = sdkResponse => { /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} transaction entity object, which is to be ensured agains null values + * @param {Object} transaction entity object, which is to be ensured against null values */ export const ensureTransaction = (transaction, booking = null, listing = null, provider = null) => { const empty = { @@ -150,7 +150,7 @@ export const ensureTransaction = (transaction, booking = null, listing = null, p /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} booking entity object, which is to be ensured agains null values + * @param {Object} booking entity object, which is to be ensured against null values */ export const ensureBooking = booking => { const empty = { id: null, type: 'booking', attributes: {} }; @@ -160,7 +160,7 @@ export const ensureBooking = booking => { /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} listing entity object, which is to be ensured agains null values + * @param {Object} listing entity object, which is to be ensured against null values */ export const ensureListing = listing => { const empty = { @@ -175,7 +175,7 @@ export const ensureListing = listing => { /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} listing entity object, which is to be ensured agains null values + * @param {Object} listing entity object, which is to be ensured against null values */ export const ensureOwnListing = listing => { const empty = { @@ -190,7 +190,7 @@ export const ensureOwnListing = listing => { /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} user entity object, which is to be ensured agains null values + * @param {Object} user entity object, which is to be ensured against null values */ export const ensureUser = user => { const empty = { id: null, type: 'user', attributes: { profile: {} } }; @@ -200,13 +200,23 @@ export const ensureUser = user => { /** * Create shell objects to ensure that attributes etc. exists. * - * @param {Object} current user entity object, which is to be ensured agains null values + * @param {Object} current user entity object, which is to be ensured against null values */ export const ensureCurrentUser = user => { const empty = { id: null, type: 'currentUser', attributes: { profile: {} }, profileImage: {} }; return { ...empty, ...user }; }; +/** + * Create shell objects to ensure that attributes etc. exists. + * + * @param {Object} time slot entity object, which is to be ensured against null values + */ +export const ensureTimeSlot = timeSlot => { + const empty = { id: null, type: 'timeSlot', attributes: {} }; + return { ...empty, ...timeSlot }; +}; + /** * Get the display name of the given user. This function handles * missing data (e.g. when the user object is still being downloaded), diff --git a/src/util/test-data.js b/src/util/test-data.js index 993be60c..a910f074 100644 --- a/src/util/test-data.js +++ b/src/util/test-data.js @@ -8,6 +8,7 @@ import { TX_TRANSITION_ACTOR_CUSTOMER, TX_TRANSITION_ACTOR_PROVIDER, LISTING_STATE_PUBLISHED, + TIME_SLOT_DAY, } from '../util/types'; const { UUID, LatLng, Money } = sdkTypes; @@ -222,7 +223,7 @@ export const createReview = (id, attributes = {}, includes = {}) => { * @return {Array} array of time slots */ export const createTimeSlots = (startDate, numberOfDays) => { - const startTime = moment(startDate).startOf('day'); + const startTime = moment.utc(startDate).startOf('day'); return Array.from({ length: numberOfDays }, (v, i) => i).map(i => { return { @@ -235,6 +236,7 @@ export const createTimeSlots = (startDate, numberOfDays) => { end: moment(startTime) .add(i + 1, 'days') .toDate(), + type: TIME_SLOT_DAY, }, }; }); diff --git a/src/util/types.js b/src/util/types.js index e43d6edf..b06a3962 100644 --- a/src/util/types.js +++ b/src/util/types.js @@ -187,11 +187,15 @@ propTypes.booking = shape({ }), }); +// A time slot that covers one day, having a start and end date. +export const TIME_SLOT_DAY = 'time-slot/day'; + // Denormalised time slot object propTypes.timeSlot = shape({ id: propTypes.uuid.isRequired, type: propTypes.value('timeSlot').isRequired, attributes: shape({ + type: oneOf([TIME_SLOT_DAY]).isRequired, end: instanceOf(Date).isRequired, start: instanceOf(Date).isRequired, }),