File size: 10,737 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
/**
* CalendarCard represents a day of schedulable concierge chats. Each card is expandable to
* allow the user to select a specific time on the day. If the day has no availability, it will
* not be expandable. When you stack a group of these cards together you'll get the scheduling
* calendar view.
*/
import 'moment-timezone'; // monkey patches the existing moment.js
import config from '@automattic/calypso-config';
import { Button, Gridicon, FoldableCard, FormLabel } from '@automattic/components';
import { getLanguage } from '@automattic/i18n-utils';
import { localize } from 'i18n-calypso';
import { isEmpty } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import day from 'calypso/assets/images/quick-start/day.svg';
import night from 'calypso/assets/images/quick-start/night.svg';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation';
import SelectOptGroups from 'calypso/components/forms/select-opt-groups';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
const defaultLanguage = getLanguage( config( 'i18n_default_locale_slug' ) ).name;
class CalendarCard extends Component {
static propTypes = {
actionText: PropTypes.string.isRequired,
date: PropTypes.number.isRequired,
disabled: PropTypes.bool.isRequired,
isDefaultLocale: PropTypes.bool.isRequired,
onSubmit: PropTypes.func.isRequired,
times: PropTypes.arrayOf( PropTypes.number ).isRequired,
timezone: PropTypes.string.isRequired,
};
constructor( props ) {
super( props );
let closestTimestamp;
let selectedTimeGroup;
let selectedMorningTime;
let selectedEveningTime;
const { moment, times, morningTimes, eveningTimes, date } = this.props;
// If there are times passed in, pick a sensible default.
if ( Array.isArray( times ) && ! isEmpty( times ) ) {
// To find the best default time for the time picker, we're going to pick the time that's
// closest to the current time of day. To do this first we find out how many seconds it's
// been since midnight on the current real world day...
const millisecondsSinceMidnight = moment().diff( this.withTimezone().startOf( 'day' ) );
// Then we'll use that to find the same time of day on the Card's given date. This will be the
// target timestamp we're trying to get as close to as possible.
const targetTimestamp = this.withTimezone( date )
.startOf( 'day' )
.add( millisecondsSinceMidnight );
// Default to the first timestamp and calculate how many seconds it's offset from the target
closestTimestamp = times[ 0 ];
let closestTimeOffset = Math.abs( closestTimestamp - targetTimestamp );
// Then look through all timestamps to find which one is the closest to the target
times.forEach( ( time ) => {
const offset = Math.abs( time - targetTimestamp );
if ( offset < closestTimeOffset ) {
closestTimestamp = time;
closestTimeOffset = offset;
}
} );
}
if ( morningTimes.includes( closestTimestamp ) ) {
selectedTimeGroup = 'morning';
selectedMorningTime = closestTimestamp;
selectedEveningTime = eveningTimes[ 0 ];
} else {
selectedTimeGroup = 'evening';
selectedEveningTime = closestTimestamp;
selectedMorningTime = morningTimes[ morningTimes.length - 1 ];
}
this.state = {
selectedTimeGroup,
selectedMorningTime,
selectedEveningTime,
};
}
withTimezone( dateTime ) {
const { moment, timezone } = this.props;
return moment( dateTime ).tz( timezone );
}
formatTimeDisplay( time ) {
const formattedTime = this.withTimezone( time ).format( 'LT' );
if ( [ '12:00 AM', '00:00' ].includes( formattedTime ) ) {
return this.props.translate( 'Midnight' );
}
return formattedTime;
}
getSelectOptGroup( times ) {
const { translate } = this.props;
const earlyMorningOptGroup = {
label: translate( 'Early Morning' ),
options: [],
isEarlyMorningTime: ( hour ) => hour >= 0 && hour < 6,
};
const morningOptGroup = {
label: translate( 'Morning' ),
options: [],
isMorningTime: ( hour ) => hour >= 6 && hour < 12,
};
const afternoonOptGroup = {
label: translate( 'Afternoon' ),
options: [],
isAfternoonTime: ( hour ) => hour >= 12 && hour < 18,
};
const eveningOptGroup = {
label: translate( 'Evening' ),
options: [],
};
times.forEach( ( time ) => {
const hour = this.withTimezone( time ).format( 'HH' );
if ( earlyMorningOptGroup.isEarlyMorningTime( hour ) ) {
earlyMorningOptGroup.options.push( {
label: this.formatTimeDisplay( time ),
value: time,
} );
} else if ( morningOptGroup.isMorningTime( hour ) ) {
morningOptGroup.options.push( {
label: this.formatTimeDisplay( time ),
value: time,
} );
} else if ( afternoonOptGroup.isAfternoonTime( hour ) ) {
afternoonOptGroup.options.push( {
label: this.formatTimeDisplay( time ),
value: time,
} );
} else {
eveningOptGroup.options.push( {
label: this.formatTimeDisplay( time ),
value: time,
} );
}
} );
const options = [
earlyMorningOptGroup,
morningOptGroup,
afternoonOptGroup,
eveningOptGroup,
].filter( ( optGroup ) => optGroup.options.length > 0 );
return options;
}
/**
* Returns a string representing the day of the week, with certain dates using natural
* language like "Today" or "Tomorrow" instead of the name of the day.
* @param {number} date Timestamp of the date
* @returns {string} The name for the day of the week
*/
getDayOfWeekString( date ) {
const { translate } = this.props;
const today = this.withTimezone().startOf( 'day' );
const dayOffset = today.diff( date.startOf( 'day' ), 'days' );
switch ( dayOffset ) {
case 0:
return translate( 'Today' );
case -1:
return translate( 'Tomorrow' );
}
return date.format( 'dddd' );
}
renderHeader() {
// The "Header" is that part of the foldable card that you click on to expand it.
const date = this.withTimezone( this.props.date );
return (
<div className="shared__available-time-card-header">
<Gridicon icon="calendar" className="shared__available-time-card-header-icon" />
<span>
<b>{ this.getDayOfWeekString( date ) } —</b> { date.format( ' MMMM D' ) }
</span>
</div>
);
}
onChange = ( { target } ) => {
const key =
this.state.selectedTimeGroup === 'morning' ? 'selectedMorningTime' : 'selectedEveningTime';
this.setState( { [ key ]: target.value } );
};
submitForm = () => {
const selectedTime =
this.state.selectedTimeGroup === 'morning'
? this.state.selectedMorningTime
: this.state.selectedEveningTime;
this.props.onSubmit( selectedTime );
};
handleFilterClick = ( timeGroup ) => {
this.setState( { selectedTimeGroup: timeGroup } );
};
render() {
const {
actionText,
disabled,
isDefaultLocale,
times,
translate,
morningTimes,
eveningTimes,
timezone,
moment,
// Temporarily harcoding durationInMinutes
// appointmentTimespan,
// moment,
} = this.props;
// Temporarily hardcoded to 30mins.
const durationInMinutes = 30; // moment.duration( appointmentTimespan, 'seconds' ).minutes();
const userTimezoneAbbr = moment.tz( timezone ).format( 'z' );
// Moment timezone does not display the abbreviation for some countries(e.g. for Singapore, it shows timezone abbr as +08 ).
// Don't display the timezone for such countries.
const shouldDisplayTzAbbr = /[a-zA-Z]/g.test( userTimezoneAbbr );
const description = isDefaultLocale
? translate( 'Sessions are %(durationInMinutes)d minutes long.', {
args: { durationInMinutes },
} )
: translate( 'Sessions are %(durationInMinutes)d minutes long and in %(defaultLanguage)s.', {
args: { defaultLanguage, durationInMinutes },
} );
const isMorningTimeGroupSelected = this.state.selectedTimeGroup === 'morning';
let timesForTimeGroup;
let btnMorningTitle;
let btnEveningTitle;
let btnMorningTimeGroup = 'shared__time-group';
let btnEveningTimeGroup = 'shared__time-group';
if ( isMorningTimeGroupSelected ) {
timesForTimeGroup = morningTimes;
btnMorningTimeGroup += ' is-selected';
btnEveningTitle =
eveningTimes.length === 0 ? translate( 'No afternoon slots available' ) : '';
} else {
timesForTimeGroup = eveningTimes;
btnEveningTimeGroup += ' is-selected';
btnMorningTitle = morningTimes.length === 0 ? translate( 'No morning slots available' ) : '';
}
return (
<FoldableCard
className="shared__available-time-card"
clickableHeader={ ! isEmpty( times ) }
compact
disabled={ isEmpty( times ) }
summary={ isEmpty( times ) ? translate( 'No sessions available' ) : null }
header={ this.renderHeader() }
>
<FormFieldset>
<FormLabel htmlFor="concierge-start-time-group">
{ translate( 'Choose time of day' ) }
</FormLabel>
<Button
className={ btnMorningTimeGroup }
onClick={ () => this.handleFilterClick( 'morning' ) }
disabled={ morningTimes.length === 0 }
title={ btnMorningTitle }
>
<div className="shared__time-group-filter">
<img src={ day } alt="" />
{ translate( 'Morning' ) }
</div>
</Button>
<Button
className={ btnEveningTimeGroup }
onClick={ () => this.handleFilterClick( 'evening' ) }
disabled={ eveningTimes.length === 0 }
title={ btnEveningTitle }
>
<div className="shared__time-group-filter">
<img src={ night } alt="" />
<span>{ translate( 'Afternoon' ) }</span>
</div>
</Button>
<br />
<br />
<FormLabel htmlFor="concierge-start-time">
{ translate( 'Choose a starting time' ) }
</FormLabel>
<SelectOptGroups
name="concierge-time-groups"
optGroups={ this.getSelectOptGroup( timesForTimeGroup ) }
id="concierge-start-time"
disabled={ disabled }
onChange={ this.onChange }
value={
isMorningTimeGroupSelected
? this.state.selectedMorningTime
: this.state.selectedEveningTime
}
/>
<FormSettingExplanation>{ description }</FormSettingExplanation>
{ shouldDisplayTzAbbr && (
<FormSettingExplanation>
{ translate( 'All times are in %(userTimezoneAbbr)s timezone.', {
args: { userTimezoneAbbr },
} ) }
</FormSettingExplanation>
) }
</FormFieldset>
<FormFieldset>
<Button disabled={ disabled } primary onClick={ this.submitForm }>
{ actionText }
</Button>
</FormFieldset>
</FoldableCard>
);
}
}
export default localize( withLocalizedMoment( CalendarCard ) );
|