Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///Problemem w tym kodzie jest bardzo duża ilość inputów(42) i nie miałem pomysłu w jaki sposób obsłużyc ich zmiany.
  2. Na dole przykład kodu w jaki sposób udało mi się to skrócić ale czuję, że nie jest to optymalne
  3.  
  4.  
  5.  
  6. ///handleChange
  7.   handleChange(date, i, name) {
  8.         if (name == `openAt_${i}`) {
  9.             let temp = this.state.parkingPrices;
  10.             temp[i].PStart = date;
  11.             this.setState({ parkingPrices: temp });
  12.             this.handleValidation();
  13.         }
  14.         if (name == `closeAt_${i}`) {
  15.             let temp = this.state.parkingPrices;
  16.             temp[i].PEnd = date;
  17.             this.setState({ parkingPrices: temp });
  18.             this.handleValidation();
  19.         }
  20.         if (name == `hourlyRate_${i}`) {
  21.             date.target.value = date.target.value.replace(/,/g, '.')
  22.             let temp = this.state.parkingPrices;
  23.             temp[i].HourlyRate = date.target.value
  24.             this.setState({ parkingPrices: temp })
  25.             this.handleValidation();
  26.         }
  27.  
  28.         if (name == `allDayRate_${i}`) {
  29.             date.target.value = date.target.value.replace(/,/g, '.')
  30.             let temp = this.state.parkingPrices;
  31.             temp[i].DailyRate = date.target.value
  32.             this.setState({ parkingPrices: temp })
  33.             this.handleValidation();
  34.         }  
  35.     }
  36.  
  37. handleHourlyCheckBoxChange = (i) => {
  38.  
  39.         let temp = this.state.parkingPrices;
  40.         let type = temp[i].Type;
  41.         if(type == PricingType.HourlyRate)    
  42.         {  
  43.         temp[i].Type = PricingType.Closed;
  44.         temp[i].IsActive = 0;
  45.         }
  46.         else if (type == PricingType.DailyRate)
  47.         {
  48.             temp[i].Type = PricingType.Unset;
  49.         temp[i].IsActive = 1;
  50.         }
  51.         else if (type == PricingType.Unset)
  52.         {
  53.          temp[i].Type = PricingType.DailyRate;
  54.         temp[i].IsActive = 1;
  55.         }
  56.         this.setState({ parkingPrices: temp});      
  57.     }
  58.     handleDailyCheckBoxChange = (i) => {
  59.         let temp = this.state.parkingPrices;
  60.         let type = temp[i].Type;
  61.         if(type == PricingType.HourlyRate)    
  62.         {  
  63.         temp[i].Type = PricingType.Unset;
  64.         temp[i].IsActive = 1;
  65.         }
  66.         else if (type == PricingType.DailyRate)
  67.         {
  68.             temp[i].Type = PricingType.Closed;
  69.         temp[i].IsActive = 0;
  70.         }
  71.         else if (type == PricingType.Unset)
  72.         {
  73.             temp[i].Type = PricingType.HourlyRate;
  74.         temp[i].IsActive = 1;
  75.         }
  76.         this.setState({ parkingPrices: temp });      
  77.     }
  78. ///RENDER
  79. {this.state.parkingPrices.map((row, i) =>
  80.                                         <tr key={`trowid_${i}`} id={`trowid_${i}`}>
  81.                                             <td key={`chbid_${i}`} className="row CheckBox">
  82.                                                 <input type="checkbox" style={{ float: "left" }}
  83.                                                     className="form-check-input daysChb"
  84.                                                     defaultChecked={row.IsActive == 1 && row.Type != PricingType.Closed}
  85.                                                     checked={row.IsActive == 1 && row.Type != PricingType.Closed}
  86.                                                     onChange={() => this.handleCheckboxChange(i)} id={`checkbxid${i}`} />
  87.                                                 <span className="weekDay">{WeekDay[row.WeekDay]}</span>
  88.  
  89.                                             </td>
  90.                                             <td key={`datepicker_${i}`} className="datePicker">
  91.                                                 <DatePicker
  92.                                                     selected={Moment.utc(row.PStart)}
  93.                                                     onChange={(date) => this.handleChange(date, i, `openAt_${i}`)}
  94.                                                     showTimeSelect
  95.                                                     showTimeSelectOnly
  96.                                                     name={`openAt_${i}`}
  97.                                                     timeIntervals={60}
  98.                                                     disabled={row.IsActive == 0}
  99.                                                     dateFormat="LT"
  100.                                                     minTime={Moment().hours(0).minutes(0)}
  101.                                                     maxTime={Moment.utc(row.PEnd).add(-1,'hours')}
  102.                                                     className="form-control datePicker "
  103.                                                     readOnly
  104.                                                 />
  105.  
  106.                                             </td>
  107.                                             <td key={`datepicker2_${i}`} className="datePicker">
  108.                                                 <DatePicker
  109.                                                     selected={Moment.utc(row.PEnd)}
  110.                                                     onChange={(date) => this.handleChange(date, i, `closeAt_${i}`)}
  111.                                                     showTimeSelect
  112.                                                     showTimeSelectOnly
  113.                                                     name={`closeAt_${i}`}
  114.                                                     disabled={row.IsActive == 0}
  115.                                                     timeIntervals={60}
  116.                                                     dateFormat="LT"
  117.                                                     minTime={Moment.utc(row.PStart).add(1,'hours')}
  118.                                                     maxTime={Moment().hours(23).minutes(0)}
  119.                                                     className="form-control datePicker"
  120.                                                     readOnly
  121.                                                 />
  122.  
  123.                                             </td>
  124.                                             <td key={`hourly_${i}`}>
  125.                                             <input type="checkbox"
  126.                                                     className="form-check-input"
  127.                                                     disabled={row.IsActive == 0 || row.Type == PricingType.Closed}
  128.                                                     checked={(row.Type == PricingType.HourlyRate || row.Type == PricingType.Unset) && row.IsActive }
  129.                                                     onChange={() => this.handleHourlyCheckBoxChange(i)} />
  130.                                                 <span className="input-group pricesWithCheckbox">
  131.                                                     <span className="input-group-addon">$</span>
  132.                                                     <input
  133.                                                         defaultValue={String(row.HourlyRate)}
  134.                                                         value=  {row.IsActive ==1 && (row.Type == PricingType.HourlyRate || row.Type == PricingType.Unset)? row.HourlyRate: ""}
  135.                                                         maxLength={7}
  136.                                                         disabled={row.IsActive == 0 || row.Type == PricingType.DailyRate || row.type == PricingType.Closed }
  137.                                                         style={this.state.parkingPrices[i].HourlyRate >= this.props.tezMin ? {} : invalid}
  138.                                                         type="text"
  139.                                                         onBlur={() =>this.formatHourlyValue(i)}
  140.                                                         onChange={(e) => this.handleChange(e, i, `hourlyRate_${i}`)}
  141.                                                         name={`hourlyRate_${i}`}
  142.                                                         onKeyUp={(e) => this.handleLength(e)}
  143.                                                         className={`form-control lowPaddInput priceRow${i}`} />
  144.                                                 </span>
  145.                                             </td>
  146.                                             <td key={`daily_${i}`}>
  147.                                             <input type="checkbox"
  148.                                                     className="form-check-input"
  149.                                                     disabled={row.IsActive == 0 || row.Type == PricingType.Closed }
  150.                                                     defaultChecked={row.Type == PricingType.DailyRate || row.Type == PricingType.Unset}
  151.                                                     checked={(row.Type == PricingType.DailyRate || row.Type == PricingType.Unset) && row.IsActive }
  152.                                                     onChange={() => this.handleDailyCheckBoxChange(i)} />
  153.                                                 <span className="input-group pricesWithCheckbox">
  154.                                                     <span className="input-group-addon">$</span>
  155.                                                     <input type="text"
  156.                                                         onKeyUp={(e) => this.handleLength(e)}
  157.                                                         style={this.state.parkingPrices[i].DailyRate >= this.props.tezMin ? {} : invalid}
  158.                                                         value= {row.IsActive ==1? row.DailyRate: ""}
  159.                                                         maxLength={7}
  160.                                                         onBlur={() =>this.formatDailyValue(i)}
  161.                                                         disabled={row.IsActive == 0 || row.Type == PricingType.Closed || row.Type == PricingType.HourlyRate }
  162.                                                         onChange={(e) => this.handleChange(e, i, `allDayRate_${i}`)}
  163.                                                         name={`allDayRate_${i}`} className={`form-control lowPaddInput priceRow${i}`} />
  164.                                                 </span>
  165.                                             </td>
  166.                                         </tr>
  167.                                     )}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement