github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/src/insurance/components/contract-management/NewContractTemplatePage.js (about) 1 'use strict'; 2 3 import React, { Props } from 'react'; 4 import PropTypes from 'prop-types'; 5 import { FormattedMessage, injectIntl, intlShape } from 'react-intl'; 6 import { connect } from 'react-redux'; 7 import { bindActionCreators } from 'redux'; 8 import { withRouter } from 'react-router-dom'; 9 10 import * as contractTemplateActions from '../../actions/contractTemplateActions'; 11 12 class NewContractTemplatePage extends React.Component { 13 14 static get propTypes() { 15 return { 16 intl: intlShape.isRequired, 17 contractTemplateActions: PropTypes.object.isRequired, 18 history: PropTypes.object.isRequired 19 }; 20 } 21 22 constructor(props) { 23 super(props); 24 25 this.state = { 26 description: 'New Contract', 27 bikeShop: true, 28 phoneShop: false, 29 skiShop: false, 30 formulaPerDay: 'price * 0.001', 31 maxSumInsured: 2000, 32 theftInsured: true, 33 conditions: 'New Contract Template Terms', 34 minDurationDays: 1, 35 maxDurationDays: 5 36 }; 37 38 this.setField = this.setField.bind(this); 39 this.createContractType = this.createContractType.bind(this); 40 } 41 42 setField(event) { 43 const { name, value, checked } = event.target; 44 if (!name) { 45 return; 46 } 47 let validated; 48 49 if (value) { 50 switch (event.target) { 51 case this.refs.maxSumInsuredField: 52 validated = Number(value) ? value : 0; 53 break; 54 case this.refs.minDurationDaysField: 55 case this.refs.maxDurationDaysField: 56 validated = parseInt(value, 10); 57 break; 58 case this.refs.theftInsuredField: 59 case this.refs.bikeShopField: 60 case this.refs.phoneShopField: 61 case this.refs.skiShopField: 62 validated = checked; 63 break; 64 default: 65 validated = value; 66 break; 67 } 68 } 69 this.setState({ [name]: validated }); 70 } 71 72 createContractType() { 73 const { 74 description, bikeShop, phoneShop, skiShop, formulaPerDay, 75 maxSumInsured, theftInsured, conditions, minDurationDays, maxDurationDays 76 } = this.state; 77 this.props.contractTemplateActions.createContractType({ 78 description, 79 shopType: 80 `${bikeShop ? 'B' : ''}${phoneShop ? 'P' : ''}${skiShop ? 'S' : ''}`, 81 formulaPerDay, 82 maxSumInsured, 83 theftInsured, 84 conditions, 85 minDurationDays, 86 maxDurationDays, 87 active: true 88 }); 89 this.props.history.push('/contract-management'); 90 } 91 92 render() { 93 const { 94 description, bikeShop, phoneShop, skiShop, formulaPerDay, 95 maxSumInsured, theftInsured, conditions, minDurationDays, maxDurationDays 96 } = this.state; 97 98 return ( 99 <div> 100 <div className='ibm-columns'> 101 <div className='ibm-col-1-1'> 102 <h3 className='ibm-h3'> 103 <FormattedMessage id='New Contract Template' /> 104 </h3> 105 </div> 106 </div> 107 <div className='ibm-columns'> 108 <div className='ibm-col-2-1 ibm-col-medium-5-3 ibm-col-small-1-1'> 109 <div className='ibm-column-form'> 110 <p> 111 <label><FormattedMessage id='Description' />:</label> 112 <span> 113 <input type='text' 114 ref='descriptionField' name='description' 115 value={description} onChange={this.setField} /> 116 </span> 117 </p> 118 <p className='ibm-form-elem-grp'> 119 <label> 120 <FormattedMessage id='Availability to Merchants' />: 121 </label> 122 <span className='ibm-input-group'> 123 <span> 124 <input type='checkbox' className='ibm-styled-checkbox' 125 id='bikeShopField' ref='bikeShopField' name='bikeShop' 126 checked={bikeShop} onChange={this.setField} /> 127 <label className='ibm-field-label' htmlFor='bikeShopField'> 128 <FormattedMessage id='Bike Shops' /> 129 </label> 130 </span> 131 <br /> 132 <span> 133 <input type='checkbox' className='ibm-styled-checkbox' 134 id='phoneShopField' ref='phoneShopField' name='phoneShop' 135 checked={phoneShop} onChange={this.setField} /> 136 <label className='ibm-field-label' htmlFor='phoneShopField'> 137 <FormattedMessage id='Phone Shops' /> 138 </label> 139 </span> 140 <br /> 141 <span> 142 <input type='checkbox' className='ibm-styled-checkbox' 143 id='skiShopField' ref='skiShopField' name='skiShop' 144 checked={skiShop} onChange={this.setField} /> 145 <label className='ibm-field-label' htmlFor='skiShopField'> 146 <FormattedMessage id='Ski Shops' /> 147 </label> 148 </span> 149 </span> 150 </p> 151 <p> 152 <label><FormattedMessage id='Formula Per Day' />:</label> 153 <span> 154 <input type='text' 155 ref='formulaPerDayField' name='formulaPerDay' 156 value={formulaPerDay} onChange={this.setField} /> 157 </span> 158 </p> 159 <p> 160 <label><FormattedMessage id='Max. Sum Insured' />:</label> 161 <span> 162 <input type='text' 163 ref='maxSumInsuredField' name='maxSumInsured' 164 value={maxSumInsured} onChange={this.setField} /> 165 </span> 166 </p> 167 <p> 168 <label><FormattedMessage id='Terms and Conditions' />:</label> 169 <span> 170 <textarea type='text' 171 ref='conditionsField' name='conditions' 172 value={conditions} onChange={this.setField} /> 173 </span> 174 </p> 175 <p className='ibm-form-elem-grp'> 176 <label><FormattedMessage id='Theft Insured' />:</label> 177 <span className='ibm-input-group'> 178 <input type='checkbox' className='ibm-styled-checkbox' 179 id='theftInsuredField' ref='theftInsuredField' 180 name='theftInsured' 181 checked={theftInsured} onChange={this.setField} /> 182 <label className='ibm-field-label' 183 htmlFor='theftInsuredField' /> 184 </span> 185 </p> 186 <p> 187 <label><FormattedMessage id='Min. Duration (days)' />:</label> 188 <span> 189 <input type='text' 190 ref='minDurationDaysField' name='minDurationDays' 191 value={minDurationDays} onChange={this.setField} /> 192 </span> 193 </p> 194 <p> 195 <label><FormattedMessage id='Max. Duration (days)' />:</label> 196 <span> 197 <input type='text' 198 ref='maxDurationDaysField' name='maxDurationDays' 199 value={maxDurationDays} onChange={this.setField} /> 200 </span> 201 </p> 202 </div> 203 </div> 204 </div> 205 <div className='ibm-columns'> 206 <div className='ibm-col-2-1 ibm-col-medium-5-3 ibm-col-small-1-1 ibm-right'> 207 <button type='button' className='ibm-btn-pri ibm-btn-blue-50' 208 onClick={this.createContractType}> 209 <FormattedMessage id='Create' /> 210 </button> 211 </div> 212 </div> 213 </div> 214 ); 215 } 216 } 217 218 function mapStateToProps(state, ownProps) { 219 return {}; 220 } 221 222 function mapDispatchToProps(dispatch) { 223 return { 224 contractTemplateActions: bindActionCreators( 225 contractTemplateActions, dispatch) 226 }; 227 } 228 229 export default withRouter(connect(mapStateToProps, mapDispatchToProps)( 230 injectIntl(NewContractTemplatePage)));