Advertisement
cindex1a

bwa mearn

Jan 20th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2.  
  3. import propTypes from "prop-types";
  4.  
  5. import Button from "elements/Button";
  6. import { InputNumber, InputDate } from "elements/Form";
  7.  
  8. export default class BookingForm extends Component {
  9.   constructor(props) {
  10.     super(props);
  11.     this.state = {
  12.       data: {
  13.         duration: 1,
  14.         date: {
  15.           startDate: new Date(),
  16.           endDate: new Date(),
  17.           key: "selection",
  18.         },
  19.       },
  20.     };
  21.   }
  22.  
  23.   updateData = (e) => {
  24.     this.setState({
  25.       ...this.state,
  26.       data: {
  27.         ...this.state.data,
  28.         [e.target.name]: e.target.value,
  29.       },
  30.     });
  31.   };
  32.  
  33.   componentDidUpdate(prevProps, prevState) {
  34.     const { data } = this.state;
  35.  
  36.     if (prevState.data.date !== data.date) {
  37.       const startDate = new Date(data.date.startDate);
  38.       const endDate = new Date(data.date.endDate);
  39.       const countDuration = new Date(endDate - startDate).getDate();
  40.       this.setState({
  41.         data: {
  42.           ...this.state.data,
  43.           duration: countDuration,
  44.         },
  45.       });
  46.     }
  47.  
  48.     if (prevState.data.duration !== data.duration) {
  49.       const startDate = new Date(data.date.startDate);
  50.       const endDate = new Date(
  51.         startDate.setDate(startDate.getDate() + +data.duration - 1)
  52.       );
  53.  
  54.       this.setState({
  55.         ...this.state,
  56.         data: {
  57.           ...this.state.data.date,
  58.           endDate: endDate,
  59.         },
  60.       });
  61.     }
  62.   }
  63.   render() {
  64.     const { data } = this.state;
  65.     const { itemDetails } = this.props;
  66.     return (
  67.       <div className="card bordered" style={{ padding: "60px 80px" }}>
  68.         <h4 className="mb-3">Start Booking</h4>
  69.         <h5 className="h2 text-teal mb-4">
  70.           ${itemDetails.price}{" "}
  71.           <span className="text-gray-500 font-weight-light">
  72.             per {itemDetails.unit}
  73.           </span>
  74.         </h5>
  75.  
  76.         <label htmlFor="duration">How long you will stay?</label>
  77.         <InputNumber
  78.           max={30}
  79.           suffix={" night"}
  80.           isSuffixPlural
  81.           onChange={this.updateData}
  82.           name="duration"
  83.           value={data.duration}
  84.         />
  85.       </div>
  86.     );
  87.   }
  88. }
  89.  
  90. BookingForm.propTypes = {
  91.   itemDetails: propTypes.object,
  92.   startBooking: propTypes.func,
  93. };
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement