Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* @flow */
  2.  
  3. import React from 'react';
  4. import moment from 'moment';
  5. import PropTypes from 'prop-types';
  6. import classNames from 'classnames';
  7.  
  8. const DayCell: DayCell = ({startHour, endHour, intervals}) => {
  9.   const getHoursPeriod: Function = () => {
  10.     const HOURS: Object[] = [];
  11.     for (let i: moment = moment(startHour, 'HH:mm'); i.isBefore(moment(endHour, 'HH:mm')); i.add(1, 'h')) {
  12.       HOURS.push(moment(i));
  13.     }
  14.     return HOURS;
  15.   };
  16.  
  17.   const getInterval: Function = (hour) => {
  18.     return intervals.find(interval => hour.isSame(moment(interval.from, 'HH:mm')));
  19.   };
  20.  
  21.   return (
  22.     <div className="schedules-table-day">
  23.       { getHoursPeriod().map((hour, index) => {
  24.         const interval: Object = getInterval(hour);
  25.         return interval ?
  26.           <Cell
  27.             key={index}
  28.             reserved={hour.isSameOrBefore(moment(interval.to, 'HH:mm')) && hour.isSameOrAfter(moment(interval.from, 'HH:mm'))}
  29.             firstHour={hour.isSame(moment(interval.from, 'HH:mm'))}
  30.             interval={interval}
  31.           /> : <Cell key={index} />;
  32.       })
  33.       }
  34.     </div>
  35.   );
  36. };
  37.  
  38. const Cell: Cell = ({reserved, firstHour, interval = {}}) => {
  39.   const getFirstLetter: Function = ([letter]) => {
  40.     return letter;
  41.   };
  42.   const initials: string = interval.employee ? `${getFirstLetter(interval.employee.firstName)}${getFirstLetter(interval.employee.lastName)}` : '';
  43.  
  44.   return (
  45.     <div
  46.       className={classNames('schedules-table-cell', {
  47.         'reserved': reserved,
  48.       })}>
  49.       {firstHour && initials}
  50.     </div>
  51.   );
  52. };
  53.  
  54. Cell.propTypes = {
  55.   reserved: PropTypes.bool,
  56.   firstHour: PropTypes.bool,
  57.   interval: PropTypes.object,
  58. };
  59.  
  60. DayCell.propTypes = {
  61.   startHour: PropTypes.string.isRequired,
  62.   endHour: PropTypes.string.isRequired,
  63.   intervals: PropTypes.array,
  64. };
  65.  
  66.  
  67. export default DayCell;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement