Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import moment from 'moment';
  3. import lodashGet from 'lodash/get';
  4.  
  5. import useSheet from '../../services/jss';
  6.  
  7. import DropdownSelector from '../dropdown-selector/DropdownSelectorComponent.jsx';
  8.  
  9. const { PropTypes } = React;
  10.  
  11. const styles = {
  12.   content: {
  13.     color: '#4a4a4a',
  14.   },
  15.   selectorWrapper: {
  16.     display: 'inline-block',
  17.     width: '40%',
  18.     verticalAlign: 'top',
  19.     position: 'relative',
  20.     '&:first-child': {
  21.       width: '25%'
  22.     },
  23.     '&:last-child': {
  24.       width: '25%'
  25.     },
  26.   },
  27.   dateSelector: {
  28.     display: 'block',
  29.   },
  30.   placeHolderPrefix: {
  31.     color: '#9b9b9b',
  32.   },
  33. };
  34.  
  35. const yearLowerBound = 1930;
  36.  
  37. export class DateSelector extends  React.PureComponent {
  38.   constructor(props) {
  39.     super(props);
  40.  
  41.     this.mapToSelectorContent = this.mapToSelectorContent.bind(this);
  42.     this.notifySubscribers = this.notifySubscribers.bind(this);
  43.     this.getDaysUntil = this.getDaysUntil.bind(this);
  44.     this.onDateChange = this.onDateChange.bind(this);
  45.     this.getDaysUntil = this.getDaysUntil.bind(this);
  46.     this.getMonthsUntil = this.getMonthsUntil.bind(this);
  47.     this.getYearsUntil = this.getYearsUntil.bind(this);
  48.     this.getMonths = this.getMonths.bind(this);
  49.     this.getYears = this.getYears.bind(this);
  50.     this.getDays = this.getDays.bind(this);
  51.  
  52.     const now = moment();
  53.     let currMoment = now;
  54.     let placeHolders;
  55.  
  56.     if (props.activeDate) {
  57.       currMoment = moment(props.activeDate, 'DD-MMM-YYYY');
  58.       placeHolders = this.setPlaceHolders(currMoment);
  59.     }
  60.  
  61.     const months = this.getMonths(currMoment);
  62.     const years = this.getYears();
  63.     const days = this.getDays(currMoment);
  64.  
  65.     this.state = {
  66.       currMoment,
  67.       months,
  68.       years,
  69.       days,
  70.       ...placeHolders,
  71.     }
  72.   }
  73.  
  74.   setPlaceHolders(currMoment) {
  75.    
  76.     this.dateSelected = true;
  77.     this.yearSelected = true;
  78.     this.monthSelected = true;
  79.    
  80.     return {
  81.       dateValue: {
  82.         label: currMoment.date(),
  83.       },
  84.       monthValue: {
  85.         label: currMoment.format('MMMM'),
  86.       },
  87.       yearValue: {
  88.         label: currMoment.format('YYYY'),
  89.       },
  90.     }
  91.   }
  92.  
  93.   getDays(currMoment) {
  94.     const now = moment();
  95.  
  96.     const isBefore = currMoment.isBefore(now, 'year') || currMoment.isBefore(now, 'month');
  97.     const daysCount = isBefore ? currMoment.daysInMonth() : now.date();
  98.  
  99.     return this.getDateEntities(this.getDaysUntil, daysCount);
  100.   }
  101.  
  102.   getYears() {
  103.     return this.getDateEntities(this.getYearsUntil, moment().year());
  104.   }
  105.  
  106.   getMonths(currMoment) {
  107.     const defaultMonthsToGenerate = 11;
  108.     const now = moment();
  109.  
  110.     const isBefore = currMoment.isBefore(now, 'year');
  111.  
  112.     const monthsCount = isBefore ? defaultMonthsToGenerate : now.month();
  113.  
  114.     return this.getDateEntities(this.getMonthsUntil, monthsCount);
  115.   }
  116.  
  117.  
  118.   getDateEntities(datesGenerator, date) {
  119.     return datesGenerator(date).map(this.mapToSelectorContent);
  120.   }
  121.  
  122.   mapToSelectorContent(content) {
  123.     return {
  124.       label: content,
  125.       style: this.props.sheet.classes.content,
  126.     };
  127.   }
  128.  
  129.   getDaysUntil(day) {
  130.     return this.generateWith(day, (v,i) => i + 1);
  131.   }
  132.  
  133.   getMonthsUntil(months) {
  134.     return this.generateWith(months+1, (x,i) => moment.months(i));
  135.   }
  136.  
  137.   getYearsUntil(year) {
  138.     const size = year - yearLowerBound + 1;
  139.  
  140.     return this.generateWith(size, (x,i) => i + yearLowerBound);
  141.   }
  142.  
  143.   generateWith(size, generateFunc) {
  144.     return Array.from(
  145.       new Array(size),
  146.       generateFunc
  147.     );
  148.   }
  149.  
  150.   onDateChange(name, value) {
  151.     const { currMoment } = this.state;
  152.     const newType = value.label;
  153.     const newMoment = currMoment[name](newType);
  154.  
  155.     this[`${name}Selected`] = true;
  156.  
  157.     const months = this.getMonths(currMoment);
  158.     const days = this.getDays(currMoment);
  159.  
  160.     const newDate = newMoment.date();
  161.     if (days.length < newDate) newMoment.date(days.length);
  162.  
  163.     const newMonth = newMoment.month();
  164.     if (months.length < newMonth + 1) newMoment.month(months.length-1);
  165.  
  166.     this.setState({
  167.       months,
  168.       days,
  169.       newMoment,
  170.       yearValue: { label: newMoment.format('YYYY') },
  171.       dateValue: { label: newMoment.format('D') },
  172.       monthValue: { label: newMoment.format('MMMM') },
  173.     });
  174.   }
  175.  
  176.   notifySubscribers(newMoment) {
  177.     const { dateSelected, monthSelected, yearSelected } = this;
  178.  
  179.     if (dateSelected && monthSelected && yearSelected) {
  180.       this.props.onValueChange(newMoment);
  181.     }
  182.   }
  183.  
  184.  
  185.   render() {
  186.     const { classes } = this.props.sheet;
  187.  
  188.     return (
  189.       <div className={classes.dateSelector}>
  190.         <div className={classes.selectorWrapper}>
  191.           <DropdownSelector
  192.             placeHolderPrefixCls={classes.placeHolderPrefix}
  193.             placeholder="Day"
  194.             isAbsolutePositionedMenu
  195.             name="date"
  196.             value={this.state.dateValue}
  197.             options={this.state.days}
  198.             onChange={this.onDateChange}
  199.           />
  200.         </div>
  201.         <div className={classes.selectorWrapper}>
  202.           <DropdownSelector
  203.             placeHolderPrefixCls={classes.placeHolderPrefix}
  204.             placeholder="Month"
  205.             isAbsolutePositionedMenu
  206.             name="month"
  207.             value={this.state.monthValue}
  208.             options={this.state.months}
  209.             onChange={this.onDateChange}
  210.           />
  211.         </div>
  212.         <div className={classes.selectorWrapper}>
  213.           <DropdownSelector
  214.             value={this.state.yearValue}
  215.             placeHolderPrefixCls={classes.placeHolderPrefix}
  216.             placeholder="Year"
  217.             isAbsolutePositionedMenu
  218.             name="year"
  219.             options={this.state.years}
  220.             onChange={this.onDateChange}
  221.           />
  222.         </div>
  223.       </div>
  224.     );
  225.   }
  226. }
  227.  
  228. DateSelector.propTypes = {
  229.   onValueChange: PropTypes.func,
  230. };
  231.  
  232. export default useSheet(styles)(DateSelector);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement