Guest User

Untitled

a guest
May 25th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. class StepThree extends Component {
  2. static getDerivedStateFromProps = (nextProps) => {
  3. const {
  4. stepThree: { dates }
  5. } = nextProps;
  6. const shouldUpdate = dates.length !== 0;
  7. if (shouldUpdate) {
  8. const newDates = dates.map(date => new Date(date));
  9. return {
  10. dates: newDates
  11. };
  12. }
  13. return null;
  14. };
  15.  
  16. state = {
  17. dates: []
  18. };
  19.  
  20. handleDayClick = (day, { selected }) => {
  21. const { dates } = this.state;
  22. if (selected) {
  23. const selectedIndex = dates.findIndex(selectedDay => DateUtils.isSameDay(selectedDay, day));
  24. dates.splice(selectedIndex, 1);
  25. } else {
  26. dates.push(day);
  27. }
  28. this.setState({ dates });
  29. };
  30.  
  31. handleNext = (e) => {
  32. e.preventDefault();
  33. this.props.setStepThree(this.state.dates);
  34. };
  35.  
  36. render() {
  37. return (
  38. <Col>
  39. <Col style={{ textAlign: 'center' }}>
  40. <DayPicker selectedDays={this.state.dates} onDayClick={this.handleDayClick} />
  41. </Col>
  42. <div
  43. style={{
  44. width: '100%',
  45. position: 'fixed',
  46. bottom: '0px',
  47. zIndex: '100',
  48. textAlign: 'center',
  49. padding: '10px',
  50. left: '0px'
  51. }}
  52. >
  53. <PreviousButton handleClick={this.props.handlePrevious} />
  54. <NextButton handleClick={this.handleNext} />
  55. </div>
  56. </Col>
  57. );
  58. }
  59. }
  60.  
  61. case ACTIONS.SET_STEP_THREE: {
  62. const newDates = action.dates.map(d => new Date(d));
  63. return {
  64. ...state,
  65. stepThree: {
  66. ...state.stepThree,
  67. dates: newDates
  68. }
  69. };
  70. }
  71.  
  72. handleDayClick = (day, { selected }) => {
  73. const { dates } = this.state;
  74. if (selected) {
  75. const selectedIndex = dates.findIndex(selectedDay =>
  76. DateUtils.isSameDay(selectedDay, day));
  77. dates.splice(selectedIndex, 1);
  78. } else {
  79. dates.push(day);
  80. }
  81. let newDatesRf = [...dates];//copy dates array to a new array
  82. this.setState({ dates:newDatesRf });
  83. };
Add Comment
Please, Sign In to add comment