Skip to content

Commit 6c9953b

Browse files
committed
Create visit
1 parent c9d57b3 commit 6c9953b

File tree

4 files changed

+148
-15
lines changed

4 files changed

+148
-15
lines changed

app/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import AreaComponent from 'components/Area/Area'
1010
import ListClientComponent from 'components/Client/List/Client'
1111
import ProfileClientComponent from 'components/Client/Profile/Client'
1212
import CreateClientComponent from 'components/Client/Create/Client'
13+
import CreateVisitComponent from 'components/Visit/Create/Visit'
1314

1415
ReactDOM.render(
1516
<Router history={hashHistory} >
@@ -19,6 +20,7 @@ ReactDOM.render(
1920
<Route path="client" component={ CreateClientComponent } />
2021
<Route path="client/:id" component={ProfileClientComponent} />
2122
<Route path="area" component={AreaComponent} />
23+
<Route path="visit/:clientId/" component={CreateVisitComponent} />
2224
<Route path="visit/:id" component={AreaComponent} />
2325
</Route>
2426
</Router>,

app/components/Client/List/Client.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,11 @@ class Client extends React.Component
5050
</td>
5151

5252
<td className="is-icon">
53-
<Link to={ `#` } >
53+
<Link to={ `/visit/${client._id}/` } >
5454
<i className="fa fa-calendar-check-o"></i>
5555
</Link>
5656
</td>
5757

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

app/services/Visit.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import axios from 'axios';
22

33
const Visit = {
4-
getGroupByArea() {
5-
let config = {
4+
5+
getEntryPoint() {
6+
return [ HOST, 'api', 'v1', 'visit' ];
7+
},
8+
9+
getConfig() {
10+
return {
611
headers: {
712
Authorization : window.localStorage.getItem('token')
813
}
914
};
10-
return axios.get(`${HOST}/api/v1/visit/group/area`, config);
15+
},
16+
17+
getGroupByArea() {
18+
let url = this.getEntryPoint();
19+
url.push('group', 'area');
20+
return axios.get(url.join('/'), this.getConfig());
21+
},
22+
23+
save(data) {
24+
return axios.post(this.getEntryPoint().join('/'), data, this.getConfig());
1125
}
1226
};
1327

0 commit comments

Comments
 (0)