Skip to content

Commit 792fe98

Browse files
Merge pull request #10 from marcoaraujojunior/master
Add tests
2 parents 39872cd + a84ae49 commit 792fe98

29 files changed

+1318
-100
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
node_modules/**
33
public/dist/**
44
public/css/**
5+
jest/**
6+
coverage/**

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: node_js
2+
node_js:
3+
- "6.7"
4+
5+
before_script:
6+
- npm install
7+
8+
script:
9+
- npm run coverage
10+
- codecov
11+
12+
after_success:
13+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# IClient - React
2+
3+
[![Build Status](https://travis-ci.org/marcoaraujojunior/iClient-react.svg?branch=master)](https://travis-ci.org/marcoaraujojunior/iClient-react) [![codecov](https://codecov.io/gh/marcoaraujojunior/iClient-react/branch/master/graph/badge.svg)](https://codecov.io/gh/marcoaraujojunior/iClient-react)
4+
5+
Simple client to IClient Server made in React JS
6+
7+
8+
# To Try
9+
10+
docker-compose up -d
11+
12+
docker-compose exec iClientReact bash
13+
14+
cd client/
15+
16+
npm run dev
17+
18+
License
19+
----
20+
21+
MIT
22+

app/App.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { Router, Route, IndexRoute, hashHistory } from 'react-router'
4+
import 'bulma/css/bulma.css'
5+
import 'font-awesome-webpack'
46

5-
import iClientComponent from 'components/iClient/iClientComponent';
6-
import HomeComponent from 'components/Home/HomeComponent';
7-
import ClientComponent from 'components/Client/ClientComponent';
7+
import iClientComponent from 'components/IClient/IClient';
8+
import HomeComponent from 'components/Home/Home';
9+
import ClientComponent from 'components/Client/Client';
810
import AreaComponent from 'components/Area/Area';
911

1012
ReactDOM.render(

app/components/Area/Area.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
11
import React from 'react';
2-
import { Router, Link } from 'react-router'
2+
import { Link } from 'react-router'
33

4-
import Visit from 'services/Visit';
4+
import VisitService from 'services/Visit'
5+
6+
import Visit from 'components/Visit/Visit'
7+
import ErrorComponent from 'components/Error/Error'
58

69
class Area extends React.Component
710
{
811
constructor(props) {
912
super(props);
1013
this.state = {
11-
areas : []
14+
areas : [],
15+
error : ''
1216
};
13-
this.renderVisit = this.renderVisit.bind(this);
14-
this.renderArea = this.renderArea.bind(this);
17+
this.generate = this.generate.bind(this);
18+
this.generateError = this.generateError.bind(this);
19+
this.getVisitGroupByArea = this.getVisitGroupByArea.bind(this);
1520
this.getVisitGroupByArea();
1621
}
1722

1823
getVisitGroupByArea() {
19-
Visit.getGroupByArea().then((response) => {
20-
this.setState({areas: response.data.visits});
24+
VisitService.getGroupByArea().then((response) => {
25+
this.setState({error: ''});
26+
this.setState({
27+
areas: this.generate(response.data.visits)
28+
});
29+
}).catch((error) => {
30+
if (error.response) {
31+
this.setState({error: error.response.data.error});
32+
}
2133
});
2234
}
2335

24-
renderVisit(visits) {
25-
return visits.map((areaVisit, key) => {
26-
return (
27-
<tr key={ key } >
28-
<td>{ areaVisit.visit.client.name }</td>
29-
<td>{ areaVisit.visit.visit_date }</td>
30-
<td className="is-icon">
31-
<Link to={ `/visit/${areaVisit.visit._id}` } >
32-
<i className="fa fa-info-circle" />
33-
</Link>
34-
</td>
35-
</tr>
36-
);
37-
});
36+
generateError(error) {
37+
return (
38+
<ErrorComponent error={this.state.error} />
39+
);
3840
}
3941

40-
renderArea(areas) {
42+
generate(areas) {
4143
return areas.map((area, key) => {
4244
return (
4345
<div className="area" key={ key }>
4446
<h3 className="title is-3">{area._id}</h3>
45-
<table className="table">
46-
<thead>
47-
<tr>
48-
<th>Name</th>
49-
<th>Last Visit</th>
50-
<th />
51-
</tr>
52-
</thead>
53-
<tbody>
54-
{this.renderVisit(area.visits)}
55-
</tbody>
56-
</table>
47+
<Visit visits={area.visits} />
5748
</div>
5849
);
5950
});
6051
}
6152

6253
render() {
54+
if (this.state.error) {
55+
let error = this.generateError(this.state.error);
56+
return error;
57+
}
6358
return (
6459
<div className="container hello">
65-
{ this.renderArea(this.state.areas) }
60+
{ this.state.areas }
6661
</div>
6762
);
6863
}

app/components/Client/ClientComponent.js renamed to app/components/Client/Client.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
import React from 'react';
22

3-
import Client from 'services/Client';
3+
import ClientService from 'services/Client';
4+
import Error from 'components/Error/Error'
45

5-
class ClientComponent extends React.Component
6+
class Client extends React.Component
67
{
78
constructor(props) {
89
super(props);
910
this.state = {
10-
clients : []
11+
clients : [],
12+
error : ''
1113
};
1214
this.getClients();
1315
}
1416

1517
getClients() {
16-
Client.getClients().then((response) => {
18+
ClientService.getClients().then((response) => {
1719
this.setState({clients: response.data.clients});
20+
}).catch((error) => {
21+
this.setState({error: 'Error Found: Trying get client'});
22+
if (error.response) {
23+
this.setState({error: error.response.data.error});
24+
}
1825
});
1926
}
2027

2128
render() {
29+
if (this.state.error) {
30+
return (<Error error={this.state.error} />);
31+
}
2232
const clientList = this.state.clients.map((client, key) => {
2333
return (
2434
<tr key={key} >
@@ -52,5 +62,5 @@ class ClientComponent extends React.Component
5262
}
5363
}
5464

55-
export default ClientComponent;
65+
export default Client;
5666

app/components/Error/Error.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ const Error = ({error}) => (
66
{error}
77
</div> : null
88
);
9+
910
export default Error;
11+
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import React from 'react';
2-
import { Router } from 'react-router'
32

4-
class HomeComponent extends React.Component{
3+
class Home extends React.Component{
54

65
render() {
7-
if (!localStorage.token) {
8-
this.context.router.push("/");
9-
}
106
return (
117
<div className="hero-body">
128
<div className="container">
@@ -19,10 +15,5 @@ class HomeComponent extends React.Component{
1915
}
2016
}
2117

22-
HomeComponent.contextTypes = {
23-
router: React.PropTypes.object.isRequired
24-
};
25-
26-
export default HomeComponent;
27-
18+
export default Home;
2819

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
import { Router, Route, IndexRoute, IndexLink, Link } from 'react-router'
32

4-
import LoginComponent from 'components/Login/LoginComponent';
3+
import LoginComponent from 'components/Login/Login';
54
import MenuComponent from 'components/Menu/Menu';
65

7-
class iClientComponent extends React.Component{
8-
constructor(props) {
9-
super(props);
6+
class IClient extends React.Component
7+
{
8+
constructor(props, context) {
9+
super(props, context);
1010
this.handleView = this.handleView.bind(this);
1111
}
1212

@@ -20,10 +20,11 @@ class iClientComponent extends React.Component{
2020
</div>
2121
);
2222
}
23+
2324
render() {
2425
let view = this.handleView();
2526

26-
if (!localStorage.token) {
27+
if (!window.localStorage.getItem('token')) {
2728
view = <LoginComponent />;
2829
}
2930
return (
@@ -32,5 +33,5 @@ class iClientComponent extends React.Component{
3233
}
3334
}
3435

35-
export default iClientComponent;
36+
export default IClient;
3637

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import { Link } from 'react-router'
3+
4+
class LinksApp extends React.Component
5+
{
6+
constructor(props, context) {
7+
super(props, context);
8+
this.generate = this.generate.bind(this);
9+
this.handleClick = this.handleClick.bind(this);
10+
this.state = {
11+
links: this.props.links
12+
};
13+
}
14+
15+
handleClick(e) {
16+
if (this.context.onClick) {
17+
this.context.onClick();
18+
}
19+
}
20+
21+
generate() {
22+
return this.state.links.map((link, index) => {
23+
return (
24+
<Link to={link[0]}
25+
key={index}
26+
className="nav-item is-tab"
27+
onlyActiveOnIndex={true}
28+
activeClassName="is-active"
29+
onClick={this.handleClick}
30+
>
31+
{link[1]}
32+
</Link>
33+
);
34+
});
35+
}
36+
37+
render() {
38+
return (
39+
<span className="nav-right nav-menu is-active">
40+
{this.generate()}
41+
</span>
42+
);
43+
}
44+
}
45+
46+
LinksApp.propTypes = {
47+
links: React.PropTypes.array.isRequired
48+
};
49+
50+
LinksApp.contextTypes = {
51+
onClick: React.PropTypes.func
52+
};
53+
54+
export default LinksApp;
55+

0 commit comments

Comments
 (0)