Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Schedules
  2.  
  3. /* @flow */
  4.  
  5. import React, { PureComponent } from 'react';
  6. import PropTypes from 'prop-types';
  7. import autobind from 'autobind-decorator';
  8. import moment from 'moment';
  9. moment.locale('pl');
  10.  
  11. import { LogoIconSVG, BigAvatarIconSVG } from 'Icons';
  12. import { SCHEDULES_WELCOME as LANGUAGE } from 'Languages';
  13. import { Scrollbars } from 'Modules/react-custom-scrollbars';
  14. import { RSQFlatInput, RSQCheckbox, RSQCheckButton } from 'Utils';
  15.  
  16. type PropsType = {
  17.   goBackToDepartment: Function,
  18.   onSaveRequest: Function,
  19. };
  20.  
  21. type StateType = {
  22.   week: Array<{}>
  23. };
  24.  
  25. class Schedules extends PureComponent<PropsType, StateType> {
  26.   day: Object;
  27.   firstHourPicker: ?Object;
  28.   secondHourPicker: ?Object;
  29.   days: Array<?Object>;
  30.  
  31.   constructor(props: Object) {
  32.     super(props);
  33.     this.days = [];
  34.   }
  35.  
  36.   getDayValue(dayIndex: number): Object {
  37.     if (this.days[dayIndex]) return this.days[dayIndex].getValues();
  38.     return {};
  39.   }
  40.  
  41.   @autobind
  42.   save() {
  43.     this.props.onSaveRequest({
  44.       startDate: this.getFirstPickerValue(),
  45.       endDate: this.getSecondPickerValue(),
  46.       schedules: {
  47.         ...this.getDayValue(0),
  48.         ...this.getDayValue(1),
  49.         ...this.getDayValue(2),
  50.         ...this.getDayValue(3),
  51.         ...this.getDayValue(4),
  52.         ...this.getDayValue(5),
  53.         ...this.getDayValue(6),
  54.       },
  55.     });
  56.   }
  57.  
  58.   getFirstPickerValue(): ?string {
  59.     if (this.firstHourPicker) return this.firstHourPicker.getDate();
  60.     return null;
  61.   }
  62.  
  63.   getSecondPickerValue(): ?string {
  64.     if (this.secondHourPicker) return this.secondHourPicker.getDate();
  65.     return null;
  66.   }
  67.  
  68.   render(): React$Element<'div'> {
  69.     return (
  70.       <div className="welcome-schedules">
  71.         <section className="welcome-schedules__left-part left-part">
  72.           <div className="left-part__top-bar">
  73.             <div className="left-part__top-bar--logo"><LogoIconSVG height="100" width="100"/></div>
  74.             <div className="left-part__top-bar--dude"><BigAvatarIconSVG width="100"/></div>
  75.           </div>
  76.           <section className="welcome-schedules__describe">{LANGUAGE.intro}</section>
  77.         </section>
  78.         <section className="welcome-schedules__schedule schedule">
  79.           <div className="schedule__content content">
  80.             <div className="content__form form">
  81.               <h1 className="form__title">{LANGUAGE.title}</h1>
  82.               <section className="form__container container">
  83.                 <Scrollbars>
  84.                   <div className="container__range range">
  85.                     <h3 className="range__title">{LANGUAGE.range}</h3>
  86.                     <div className="range__content">
  87.                       <div className="range__content--date">
  88.                         <RSQFlatInput.DatePicker ref={node => {this.firstHourPicker = node;}} label={LANGUAGE.applyFrom}/>
  89.                       </div>
  90.                       <div className="range__content--date">
  91.                         <RSQFlatInput.DatePicker ref={node => {this.secondHourPicker = node;}} label={LANGUAGE.applyTo}/>
  92.                       </div>
  93.                     </div>
  94.                   </div>
  95.                   <div className="container__schedule schedule">
  96.                     <h3 className="schedule__title">{LANGUAGE.workDays}</h3>
  97.                     {moment.weekdays(true).map((dayName, index) => <Day key={index} ref={node => this.days.push(node)} dayName={dayName} dayIndex={index + 1} />)}
  98.                   </div>
  99.                 </Scrollbars>
  100.               </section>
  101.  
  102.             </div>
  103.             <div className="schedule__buttons buttons">
  104.               <div className="buttons__content">
  105.                 <RSQCheckButton
  106.                   onClickFirst={this.props.goBackToDepartment}
  107.                   onClickSecond={this.save}
  108.                   firstName={LANGUAGE.back}
  109.                   secondName={LANGUAGE.save}
  110.                   firstInactive
  111.                   secondActive
  112.                 />
  113.               </div>
  114.             </div>
  115.           </div>
  116.         </section>
  117.       </div>
  118.     );
  119.   }
  120. }
  121.  
  122. type DayPropsType = {
  123.   dayIndex: number;
  124.   dayName: string;
  125. };
  126.  
  127. type DayStateType = {
  128.   isActive: boolean,
  129. };
  130.  
  131. class Day extends PureComponent<DayPropsType, DayStateType> {
  132.   checkbox: ?Object;
  133.   timeBoundaryInput: ?Object;
  134.   breaksInput: ?Object;
  135.   slotInput: ?Object;
  136.  
  137.   constructor(props: Object) {
  138.     super(props);
  139.     this.state = {
  140.       isActive: false,
  141.     };
  142.   }
  143.  
  144.   @autobind
  145.   onClickActive() {
  146.     this.setState({isActive: !this.state.isActive});
  147.   }
  148.  
  149.   @autobind
  150.   getValues(): Object {
  151.     return {
  152.       [this.props.dayIndex]: !this.state.isActive ? null : {
  153.         startTime: this.timeBoundaryInput && this.timeBoundaryInput.getFirstPickerValue(),
  154.         endTime: this.timeBoundaryInput && this.timeBoundaryInput.getSecondPickerValue(),
  155.         startBreak: this.breaksInput && this.breaksInput.getFirstPickerValue(),
  156.         endBreak: this.breaksInput && this.breaksInput.getSecondPickerValue(),
  157.         slots: this.slotInput && this.slotInput.getTimePickerValue(),
  158.       },
  159.     };
  160.   }
  161.  
  162.   render(): React$Element<'div'>  {
  163.     return (
  164.       <div className="schedule__day day">
  165.         <h6 className="day__title">
  166.           <RSQCheckbox isActive={this.state.isActive} onClick={this.onClickActive} white square disableAutoActive/>
  167.           <p className="day__title--text">{this.props.dayName}</p>
  168.         </h6>
  169.         {
  170.           this.state.isActive &&
  171.           [
  172.             <TimeRowInputs ref={node => {this.timeBoundaryInput = node;}} key="1" labelFirst={LANGUAGE.startHour} labelSecond={LANGUAGE.endHour} />,
  173.             <TimeRowInputs ref={node => {this.breaksInput = node;}} key="2" labelFirst={LANGUAGE.breakFrom} labelSecond={LANGUAGE.breakTo} />,
  174.             <TimeRowInputs ref={node => {this.slotInput = node;}} key="3" labelFirst={LANGUAGE.slots} labelSecond={null}/>,
  175.           ]
  176.         }
  177.       </div>
  178.     );
  179.   }
  180. }
  181.  
  182. Day.propTypes = {
  183.   dayName: PropTypes.string,
  184.   dayIndex: PropTypes.number,
  185. };
  186.  
  187. type TimeRowInputsPropsType = {
  188.   labelFirst: string,
  189.   labelSecond: ?string,
  190. }
  191.  
  192. class TimeRowInputs extends PureComponent<TimeRowInputsPropsType> {
  193.   firstPicker: ?Object;
  194.   secondPicker: ?Object;
  195.   timepicker: ?Object;
  196.  
  197.   @autobind
  198.   getFirstPickerValue(): ?string {
  199.     if (this.firstPicker) return this.firstPicker.getHour();
  200.     return null;
  201.   }
  202.  
  203.   @autobind
  204.   getSecondPickerValue(): ?string {
  205.     if (this.secondPicker) return this.secondPicker.getHour();
  206.     return null;
  207.   }
  208.  
  209.   @autobind
  210.   getTimePickerValue(): ?string {
  211.     if (this.timepicker) return this.timepicker.getValue();
  212.     return null;
  213.   }
  214.  
  215.   render(): React$Element<'div'>  {
  216.     return (
  217.       <div className="day__time">
  218.         {
  219.           this.props.labelSecond ? [
  220.             <div key="1" className="day__time--hour">
  221.               <RSQFlatInput.HourPicker label={this.props.labelFirst} ref={node => {this.firstPicker = node;}} />
  222.             </div>,
  223.             <div key="2" className="day__time--hour">
  224.               <RSQFlatInput.HourPicker label={this.props.labelSecond} ref={node => {this.secondPicker = node;}} />
  225.             </div>,
  226.           ] :
  227.             <div className="day__time--hour">
  228.               <RSQFlatInput.TimePicker label={this.props.labelFirst} ref={node => {this.timepicker = node;}} />
  229.             </div>
  230.         }
  231.       </div>
  232.     );
  233.   }
  234. }
  235.  
  236.  
  237. TimeRowInputs.propTypes = {
  238.   labelFirst: PropTypes.string,
  239.   labelSecond: PropTypes.string,
  240. };
  241.  
  242. export default Schedules;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement