Skip to content

Commit 533ad95

Browse files
committed
Merge branch 'feature/create_visit' into develop
2 parents cd640c7 + 21168bc commit 533ad95

File tree

9 files changed

+459
-27
lines changed

9 files changed

+459
-27
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/Create/Client.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class Client extends React.Component
1010
{
1111
constructor(props, context) {
1212
super(props, context);
13-
super(props);
1413
this.handleSubmit = this.handleSubmit.bind(this);
14+
this.formatFormData = this.formatFormData.bind(this);
1515
this.state = {
1616
error : '',
1717
areas : null,
@@ -31,13 +31,20 @@ class Client extends React.Component
3131
});
3232
}
3333

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+
3444
handleSubmit(e) {
3545
e.preventDefault();
3646

37-
let client = {}
38-
for (let i in this.refs) {
39-
client[i] = this.refs[i].value;
40-
}
47+
let client = this.formatFormData(this.refs);
4148

4249
this.setState({client: client});
4350

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
});

app/components/Client/Profile/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Client extends React.Component
1717
}
1818

1919
getClient(id) {
20-
ClientService.getClient(id).then((response) => {
20+
ClientService.find(id).then((response) => {
2121
this.setState({client: response.data.client.shift()});
2222
}).catch((error) => {
2323
this.setState({error: 'Error Found: Trying get client'});
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+

app/services/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Client = {
1818
return axios.get(this.getEntryPoint().join('/'), this.getConfig());
1919
},
2020

21-
getClient(id) {
21+
find(id) {
2222

2323
let url = this.getEntryPoint();
2424
url.push(id);

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

tests/Create.Client.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ describe('Test Create Client', () => {
194194
city : 'city',
195195
frequency : 'frequency',
196196
ability : 'ability',
197-
area : 'Center',
197+
area : {
198+
_id : "Center",
199+
parent : "",
200+
__v : 0,
201+
ancestors : []
202+
},
198203
};
199204

200205
mockAdapter
@@ -298,7 +303,7 @@ describe('Test Create Client', () => {
298303
}, 0);
299304
});
300305

301-
it('Client should show default error message', () => {
306+
it('Client should show default error message', (done) => {
302307

303308
let response = {};
304309
let Client;
@@ -316,9 +321,13 @@ describe('Test Create Client', () => {
316321

317322
setTimeout(() => {
318323

319-
component.update();
320-
expect(component.render().text()).toEqual('Error Found: Trying get area');
321-
done();
324+
try {
325+
component.update();
326+
expect(component.render().text()).toEqual('Error Found: Trying get areas');
327+
done();
328+
} catch(e) {
329+
console.log(e);
330+
}
322331
}, 0);
323332
});
324333

0 commit comments

Comments
 (0)