Skip to content

Commit 167e04e

Browse files
committed
Added store, reducers and actions.
Refactored employees-components.
1 parent 69ef1a5 commit 167e04e

25 files changed

+6439
-15075
lines changed

img/Employees_Add.jpg

448 Bytes
Loading

img/Employees_Edit.jpg

-9.06 KB
Loading

img/Employees_List.jpg

-9.35 KB
Loading

react-frontend/package-lock.json

Lines changed: 5866 additions & 3775 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-frontend/package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@testing-library/jest-dom": "^4.2.4",
7-
"@testing-library/react": "^9.3.2",
8-
"@testing-library/user-event": "^7.1.2",
9-
"react": "^16.14.0",
10-
"react-dom": "^16.14.0",
11-
"react-scripts": "3.4.3",
6+
"@testing-library/jest-dom": "^5.11.6",
7+
"@testing-library/react": "^11.2.2",
8+
"@testing-library/user-event": "^12.5.0",
9+
"react": "^17.0.1",
10+
"react-dom": "^17.0.1",
11+
"react-scripts": "^4.0.1",
1212
"react-router-dom": "^5.2.0",
1313
"bootstrap": "^4.5.3",
14-
"axios": "0.20.0"
14+
"axios": "^0.21.0",
15+
"redux": "^4.0.5",
16+
"react-redux": "^7.2.2",
17+
"redux-thunk": "^2.3.0",
18+
"prop-types": "^15.7.2"
1519
},
1620
"scripts": {
1721
"start": "react-scripts start",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import axios from "axios";
2+
import {GET_ERRORS, GET_EMPLOYEES, DELETE_EMPLOYEE, GET_EMPLOYEE_BY_ID} from "./types";
3+
4+
const EMPLOYEE_API_BASE_URL = "http://localhost:8080/api/v1/employees";
5+
6+
export const createEmployee = (employee, history) => async dispatch => {
7+
try {
8+
await axios.post(EMPLOYEE_API_BASE_URL, employee);
9+
history.push("/");
10+
dispatch({
11+
type: GET_ERRORS,
12+
payload: {}
13+
})
14+
} catch (error) {
15+
dispatch({
16+
type: GET_ERRORS,
17+
payload: error.response.data
18+
})
19+
}
20+
};
21+
22+
export const updateEmployee = (employeeId, employee, history) => async dispatch => {
23+
try {
24+
await axios.put(EMPLOYEE_API_BASE_URL + "/" + employeeId, employee);
25+
history.push("/");
26+
dispatch({
27+
type: GET_ERRORS,
28+
payload: {}
29+
})
30+
} catch (error) {
31+
dispatch({
32+
type: GET_ERRORS,
33+
payload: error.response.data
34+
})
35+
}
36+
};
37+
38+
export const getEmployees = () => async dispatch => {
39+
const response = await axios.get(EMPLOYEE_API_BASE_URL);
40+
dispatch({
41+
type: GET_EMPLOYEES,
42+
payload: response.data
43+
})
44+
};
45+
46+
export const deleteEmployee = (employeeId) => async dispatch => {
47+
await axios.delete(EMPLOYEE_API_BASE_URL + "/" + employeeId);
48+
dispatch({
49+
type: DELETE_EMPLOYEE,
50+
payload: employeeId
51+
})
52+
};
53+
54+
export const getEmployeeById = (employeeId) => async dispatch => {
55+
const response = await axios.get(EMPLOYEE_API_BASE_URL + "/" + employeeId);
56+
dispatch({
57+
type: GET_EMPLOYEE_BY_ID,
58+
payload: response.data
59+
})
60+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const GET_ERRORS = "GET_ERRORS";
2+
export const GET_EMPLOYEES = "GET_EMPLOYEES";
3+
export const DELETE_EMPLOYEE = "DELETE_EMPLOYEE";
4+
export const GET_EMPLOYEE_BY_ID = "GET_EMPLOYEE_BY_ID";

react-frontend/src/components/app/app.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
html {
2+
position: relative;
3+
min-height: 100%;
4+
}
5+
6+
body {
7+
margin-bottom: 50px;
8+
}
9+
110
.footer {
211
position: absolute;
312
bottom: 0;
Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
import React, {Component} from 'react';
2-
import ListEmployeesComponent from "../employees-components/list-employees-component.jsx";
3-
import "./app.css"
1+
import React from 'react';
2+
import {BrowserRouter as Router, Route, Switch} from "react-router-dom";
3+
import {Provider} from "react-redux";
4+
import store from "../../store";
5+
6+
import EmployeesListComponent from "../employees-components/employees-list-component";
7+
import EmployeeAddComponent from "../employees-components/employee-add-component";
8+
import EmployeeUpdateComponent from "../employees-components/employee-update-component";
9+
import EmployeeViewComponent from "../employees-components/employee-view-component";
410
import Header from "../parts/header";
511
import Footer from "../parts/footer";
6-
import {BrowserRouter as Router, Route, Switch} from "react-router-dom";
7-
import EmployeeComponent from "../employees-components/employee-component";
8-
import ViewEmployeeComponent from "../employees-components/view-employee-component";
912

10-
class App extends Component {
11-
render() {
12-
return (
13-
<div>
14-
<Router>
15-
<Header/>
16-
<div className="container">
17-
<Switch>
18-
<Route path="/" component={ListEmployeesComponent} exact />
19-
<Route path="/employees" component={ListEmployeesComponent} exact/>
20-
<Route path="/employees/:id" component={EmployeeComponent} exact/>
21-
<Route path="/employees/view/:id" component={ViewEmployeeComponent} exact/>
22-
</Switch>
23-
</div>
24-
<Footer/>
25-
</Router>
26-
</div>
27-
);
28-
}
29-
}
13+
import "./app.css"
14+
15+
const App = () => {
16+
return (
17+
<Provider store={store}>
18+
<Router>
19+
<Header/>
20+
<div className="container">
21+
<Switch>
22+
<Route exact path="/" component={EmployeesListComponent}/>
23+
<Route exact path="/employees/add" component={EmployeeAddComponent}/>
24+
<Route exact path="/employees/:id" component={EmployeeUpdateComponent}/>
25+
<Route exact path="/employees/view/:id" component={EmployeeViewComponent}/>
26+
</Switch>
27+
</div>
28+
<Footer/>
29+
</Router>
30+
</Provider>
31+
);
32+
};
3033

3134
export default App;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, {Component} from 'react';
2+
import {connect} from "react-redux";
3+
import PropTypes from "prop-types";
4+
5+
import {createEmployee} from "../../actions/employee-actions";
6+
import InputForm from "../parts/input-form";
7+
8+
class EmployeeAddComponent extends Component {
9+
constructor(props) {
10+
super(props);
11+
12+
this.state = {
13+
firstName: "",
14+
lastName: "",
15+
city: "",
16+
address: "",
17+
telephone: "",
18+
errors: {}
19+
};
20+
21+
this.createEmployee = this.createEmployee.bind(this);
22+
this.handleInputChange = this.handleInputChange.bind(this);
23+
}
24+
25+
// static getDerivedStateFromProps(nextProps)
26+
componentWillReceiveProps(nextProps) {
27+
if (nextProps.errors) {
28+
this.setState({errors: nextProps.errors});
29+
}
30+
}
31+
32+
handleInputChange(event) {
33+
const {name, value} = event.target;
34+
35+
this.setState({
36+
[name]: value
37+
});
38+
};
39+
40+
createEmployee(event) {
41+
event.preventDefault();
42+
43+
const {firstName, lastName, city, address, telephone} = this.state;
44+
45+
const employee = {
46+
firstName,
47+
lastName,
48+
city,
49+
address,
50+
telephone
51+
};
52+
53+
this.props.createEmployee(employee, this.props.history);
54+
};
55+
56+
render() {
57+
return (
58+
<div>
59+
<InputForm
60+
title={"Add Employee"}
61+
employee={this.state}
62+
errors={this.state.errors}
63+
onSubmitForm={this.createEmployee}
64+
handleInputChange={this.handleInputChange}
65+
/>
66+
</div>
67+
);
68+
}
69+
}
70+
71+
EmployeeAddComponent.propTypes = {
72+
createEmployee: PropTypes.func.isRequired,
73+
errors: PropTypes.object.isRequired,
74+
};
75+
76+
const mapStateToProps = (state) => ({
77+
errors: state.errors,
78+
});
79+
80+
export default connect(mapStateToProps, {createEmployee})(EmployeeAddComponent);

0 commit comments

Comments
 (0)