|
| 1 | +import React from 'react'; |
| 2 | +import { Link, Router } from 'react-router' |
| 3 | + |
| 4 | +import Error from 'components/Error/Error' |
| 5 | +import VisitService from 'services/Visit' |
| 6 | +import ClientService from 'services/Client' |
| 7 | +import styles from 'components/Client/Create/styles.css' |
| 8 | + |
| 9 | +class Visit extends React.Component |
| 10 | +{ |
| 11 | + constructor(props, context) { |
| 12 | + super(props, context); |
| 13 | + this.handleSubmit = this.handleSubmit.bind(this); |
| 14 | + this.state = { |
| 15 | + error : '', |
| 16 | + client : null, |
| 17 | + visit_date: new Date(), |
| 18 | + visit : {} |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + componentWillMount() { |
| 23 | + ClientService.find(this.props.params.clientId).then((response) => { |
| 24 | + this.setState({client: response.data.client.shift()}); |
| 25 | + }).catch((error) => { |
| 26 | + this.setState({error: 'Error Found: Trying get client'}); |
| 27 | + let validResponse = typeof error.response.data !== 'undefined'; |
| 28 | + if (validResponse && typeof error.response.data.error !== 'undefined') { |
| 29 | + this.setState({error: error.response.data.error}); |
| 30 | + } |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + handleSubmit(e) { |
| 35 | + e.preventDefault(); |
| 36 | + |
| 37 | + let visit = { |
| 38 | + client : this.state.client, |
| 39 | + visit_date: this.state.visit_date, |
| 40 | + }; |
| 41 | + |
| 42 | + for (let i in this.refs) { |
| 43 | + visit[i] = this.refs[i].value; |
| 44 | + } |
| 45 | + |
| 46 | + this.setState({visit: visit}); |
| 47 | + |
| 48 | + VisitService.save(visit).then((response) => { |
| 49 | + this.context.router.push("/area"); |
| 50 | + }).catch((error) => { |
| 51 | + |
| 52 | + this.setState({error: 'Error trying create visit'}); |
| 53 | + |
| 54 | + let responseValid = typeof error.response.data !== 'undefined'; |
| 55 | + |
| 56 | + if (responseValid && typeof error.response.data.error !== 'undefined') { |
| 57 | + this.setState({error: error.response.data.error}); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + render() { |
| 63 | + if (this.state.error) { |
| 64 | + return (<Error error={this.state.error} />); |
| 65 | + } |
| 66 | + |
| 67 | + if (!this.state.client) { |
| 68 | + return <div>Loading...</div>; |
| 69 | + } |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="container"> |
| 73 | + <div className="columns is-vcentered"> |
| 74 | + <div className="column is-4 is-offset-4"> |
| 75 | + <h1 className="title"> |
| 76 | + Create a Visit |
| 77 | + </h1> |
| 78 | + <form onSubmit={this.handleSubmit}> |
| 79 | + <center> |
| 80 | + <div className="card has-text-left"> |
| 81 | + <div className="card-content"> |
| 82 | + <div className="content"> |
| 83 | + <p className="is-5">Name: { this.state.client.name }< /p> |
| 84 | + <p className="is-6">Address: { this.state.client.address }</p> |
| 85 | + <p className="is-6">City: { this.state.client.city }</p> |
| 86 | + <hr /> |
| 87 | + <label className="label">Value Received</label> |
| 88 | + <p className="control"> |
| 89 | + <input |
| 90 | + ref="value_received" |
| 91 | + className="input" |
| 92 | + type="text" |
| 93 | + placeholder="" |
| 94 | + required="required" |
| 95 | + /> |
| 96 | + </p> |
| 97 | + <label className="label">Quantity</label> |
| 98 | + <p className="control"> |
| 99 | + <input |
| 100 | + ref="sales_quantity" |
| 101 | + className="input" |
| 102 | + type="text" |
| 103 | + placeholder="" |
| 104 | + required="required" |
| 105 | + /> |
| 106 | + </p> |
| 107 | + <button className="button is-primary" >Save</button> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + <div className="card-image"> |
| 111 | + <figure className="image is-4by3"> |
| 112 | + <img src="http://placehold.it/300x225" alt="" /> |
| 113 | + </figure> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </center> |
| 117 | + </form> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +Visit.contextTypes = { |
| 126 | + router: React.PropTypes.object.isRequired |
| 127 | +}; |
| 128 | + |
| 129 | +export default Visit; |
| 130 | + |
0 commit comments