|
| 1 | +import React from 'react'; |
| 2 | +import { Link, Router } from 'react-router' |
| 3 | + |
| 4 | +import Error from 'components/Error/Error' |
| 5 | +import AreaService from 'services/Area' |
| 6 | +import ClientService from 'services/Client' |
| 7 | +import styles from 'components/Client/Create/styles.css' |
| 8 | + |
| 9 | +class Client extends React.Component |
| 10 | +{ |
| 11 | + constructor(props, context) { |
| 12 | + super(props, context); |
| 13 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 14 | + this.formatFormData = this.formatFormData.bind(this); |
| 15 | + this.state = { |
| 16 | + error : '', |
| 17 | + areas : null, |
| 18 | + client: {} |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + componentWillMount() { |
| 23 | + |
| 24 | + AreaService.getAll().then((response) => { |
| 25 | + this.setState({areas: response.data.areas}); |
| 26 | + }).catch((error) => { |
| 27 | + this.setState({error: 'Error Found: Trying get areas'}); |
| 28 | + if (typeof error.response.data.error !== 'undefined') { |
| 29 | + this.setState({error: error.response.data.error}); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + formatFormData(data) { |
| 35 | + let client = {} |
| 36 | + for (let i in data) { |
| 37 | + client[i] = data[i].value; |
| 38 | + } |
| 39 | + |
| 40 | + client.area = this.state.areas.filter( (area) => client['area'] == area._id ).shift(); |
| 41 | + return client; |
| 42 | + } |
| 43 | + |
| 44 | + handleSubmit(e) { |
| 45 | + e.preventDefault(); |
| 46 | + |
| 47 | + let client = this.formatFormData(this.refs); |
| 48 | + |
| 49 | + this.setState({client: client}); |
| 50 | + |
| 51 | + ClientService.save(client).then((response) => { |
| 52 | + this.context.router.push("/clients"); |
| 53 | + }).catch((error) => { |
| 54 | + |
| 55 | + this.setState({error: 'Error trying create client'}); |
| 56 | + |
| 57 | + let responseValid = typeof error.response.data !== 'undefined'; |
| 58 | + |
| 59 | + if (responseValid && typeof error.response.data.error !== 'undefined') { |
| 60 | + this.setState({error: error.response.data.error}); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + render() { |
| 66 | + if (this.state.error) { |
| 67 | + return (<Error error={this.state.error} />); |
| 68 | + } |
| 69 | + if (!this.state.areas) { |
| 70 | + return <div>Loading...</div>; |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="container"> |
| 75 | + <div className="columns is-vcentered"> |
| 76 | + <div className="column is-4 is-offset-4"> |
| 77 | + <h1 className="title"> |
| 78 | + Register a Client |
| 79 | + </h1> |
| 80 | + <form onSubmit={this.handleSubmit}> |
| 81 | + <div className="box"> |
| 82 | + <label className="label">Name</label> |
| 83 | + <p className="control has-icon"> |
| 84 | + <input |
| 85 | + required='required' |
| 86 | + ref='name' |
| 87 | + className="input marco" |
| 88 | + type="text" |
| 89 | + placeholder="John Smith" |
| 90 | + /> |
| 91 | + <i className="fa fa-id-card-o" aria-hidden="true" /> |
| 92 | + </p> |
| 93 | + <label className="label">Phone</label> |
| 94 | + <p className="control has-icon"> |
| 95 | + <input |
| 96 | + required='required' |
| 97 | + pattern="[\(]\d{2}[\)]\d{4,5}[\-]\d{4}" |
| 98 | + ref='phone' |
| 99 | + className="input" |
| 100 | + type="text" |
| 101 | + placeholder="(21)99999-00000" |
| 102 | + /> |
| 103 | + <i className="fa fa-phone" aria-hidden="true" /> |
| 104 | + </p> |
| 105 | + <label className="label">Address</label> |
| 106 | + <p className="control has-icon"> |
| 107 | + <input |
| 108 | + required='required' |
| 109 | + ref='address' |
| 110 | + className="input" |
| 111 | + type="text" |
| 112 | + placeholder="Street 32 - Nº 23" |
| 113 | + /> |
| 114 | + <i className="fa fa-address-card" aria-hidden="true" /> |
| 115 | + </p> |
| 116 | + <label className="label">City</label> |
| 117 | + <p className="control has-icon"> |
| 118 | + <input |
| 119 | + required='required' |
| 120 | + ref='city' |
| 121 | + className="input" |
| 122 | + type="text" |
| 123 | + placeholder="London" |
| 124 | + /> |
| 125 | + <i className="fa fa-address-book" aria-hidden="true" /> |
| 126 | + </p> |
| 127 | + <label className="label">Area</label> |
| 128 | + <p className="control has-icon"> |
| 129 | + <span className="select"> |
| 130 | + <select ref='area' > |
| 131 | + { |
| 132 | + this.state.areas.map((area, key) => ( |
| 133 | + <option value={ area._id } key={ key }>{ area._id}</option> |
| 134 | + )) |
| 135 | + } |
| 136 | + </select> |
| 137 | + </span> |
| 138 | + <i className="fa fa-map-marker" aria-hidden="true" /> |
| 139 | + </p> |
| 140 | + <label className="label">Frequency</label> |
| 141 | + <p className="control has-icon"> |
| 142 | + <input |
| 143 | + required='required' |
| 144 | + ref='frequency' |
| 145 | + className="input" |
| 146 | + type="number" |
| 147 | + placeholder="10" |
| 148 | + /> |
| 149 | + <i className="fa fa-refresh" aria-hidden="true" /> |
| 150 | + </p> |
| 151 | + <label className="label">Ability</label> |
| 152 | + <p className="control has-icon"> |
| 153 | + <input |
| 154 | + required='required' |
| 155 | + ref='ability' |
| 156 | + className="input" |
| 157 | + type="number" |
| 158 | + placeholder="200" |
| 159 | + /> |
| 160 | + <i className="fa fa-shopping-basket" aria-hidden="true" /> |
| 161 | + </p> |
| 162 | + <hr /> |
| 163 | + <p className="control"> |
| 164 | + <button className="button is-primary">Save</button> |
| 165 | + <Link to='/clients' className="button"> |
| 166 | + <span>Cancel</span> |
| 167 | + </Link> |
| 168 | + </p> |
| 169 | + </div> |
| 170 | + </form> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +Client.contextTypes = { |
| 179 | + router: React.PropTypes.object.isRequired |
| 180 | +}; |
| 181 | + |
| 182 | +export default Client; |
| 183 | + |
0 commit comments