Add ensure function for time slots

Also fixes time slots test data creation by setting the date time
correctly to UTC.
This commit is contained in:
Hannu Lyytikainen 2018-07-26 14:39:36 +03:00
parent df4c217cc9
commit 4be615c352
4 changed files with 34 additions and 13 deletions

View file

@ -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 {

View file

@ -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),

View file

@ -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,
},
};
});

View file

@ -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,
}),