Skip to content
This repository was archived by the owner on Nov 21, 2021. It is now read-only.

Commit e2b000d

Browse files
committed
Add a new employee form in react app.
1 parent d55d78f commit e2b000d

File tree

5 files changed

+87
-32
lines changed

5 files changed

+87
-32
lines changed

client/public/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"short_name": "React App",
3-
"name": "Create React App Sample",
2+
"short_name": "Employees",
3+
"name": "Employee Coding Challenge App",
44
"icons": [
55
{
66
"src": "favicon.ico",

client/src/App.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import './App.css';
22

33
import EmployeeTable from './EmployeeTable';
4+
import NewEmployeeForm from "./NewEmployeeForm";
5+
46
import {useEffect, useState} from "react";
57
import axios from "axios";
68

79
function App() {
8-
const [employees, setEmployees] = useState([]);
10+
const [employees, setEmployees] = useState([]);
911

10-
useEffect(() => {
11-
axios.get('/api/employees').then(
12-
response => {
13-
setEmployees(response.data);
14-
}
15-
);
16-
}, []);
12+
// Populate the table
13+
useEffect(() => {
14+
axios.get('/api/employees').then(
15+
response => {
16+
setEmployees(response.data);
17+
}
18+
);
19+
}, []);
1720

18-
return (
19-
<EmployeeTable employees={employees} />
20-
);
21+
return (
22+
<>
23+
<EmployeeTable employees={employees}/>
24+
<NewEmployeeForm/>
25+
</>
26+
);
2127
}
2228

2329
export default App;

client/src/Employee.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ function Employee (props) {
44

55
return (
66
<tr>
7-
<td><input type="text" value={employee.fname} /></td>
8-
<td><input type="text" value={employee.lname} /></td>
9-
<td><input type="text" value={employee.hdate} /></td>
10-
<td><input type="text" value={employee.role} /></td>
7+
<td><input type="text" value={employee.fname} onChange={props.onChange} /></td>
8+
<td><input type="text" value={employee.lname} onChange={props.onChange} /></td>
9+
<td><input type="text" value={employee.hdate} onChange={props.onChange} /></td>
10+
<td><input type="text" value={employee.role} onChange={props.onChange} /></td>
1111
<td>{employee.id}</td>
1212
<td><button>Delete</button></td>
1313
</tr>

client/src/EmployeeTable.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ import Employee from "./Employee";
22

33
function EmployeeTable (props) {
44
return (
5-
<table>
6-
<thead>
7-
<tr>
8-
<th>First Name</th>
9-
<th>Last Name</th>
10-
<th>Hire Date</th>
11-
<th>Role</th>
12-
<th>ID</th>
13-
</tr>
14-
</thead>
15-
<tbody>
16-
{props.employees.map(e => <Employee employee={e}/>)}
17-
</tbody>
5+
<form>
6+
<table>
7+
<thead>
8+
<tr>
9+
<th>First Name</th>
10+
<th>Last Name</th>
11+
<th>Hire Date</th>
12+
<th>Role</th>
13+
<th>ID</th>
14+
</tr>
15+
</thead>
16+
<tbody>
17+
{props.employees.map(e => <Employee key={e.id} employee={e} onChange={() => console.log("Something changed...")}/>)}
18+
</tbody>
19+
</table>
1820
<button>Submit Changes</button>
19-
<button>New Record</button>
20-
</table>
21+
</form>
2122
)
2223
}
2324

client/src/NewEmployeeForm.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {useState} from "react";
2+
import axios from "axios";
3+
4+
function NewEmployeeForm() {
5+
6+
const [employee, setEmployee] = useState({fname: '', lname: '', hdate: '', role: ''});
7+
8+
const handleSubmit = (event) => {
9+
event.preventDefault();
10+
console.log(employee);
11+
axios.post('api/employees', employee)
12+
.then(response => console.log(response))
13+
.catch(error => console.log(error));
14+
}
15+
16+
return (
17+
<form onSubmit={handleSubmit}>
18+
<h2>Add New Employee</h2>
19+
<table>
20+
<thead>
21+
<tr>
22+
<th>First Name</th>
23+
<th>Last Name</th>
24+
<th>Hire Date</th>
25+
<th>Role</th>
26+
</tr>
27+
<tr>
28+
<td><input id="newEmployeeFirstName" type="text" value={employee.fname}
29+
onChange={e => (setEmployee({...employee, fname: e.target.value}))}
30+
/></td>
31+
<td><input id="newEmployeeLastName" type="text" value={employee.lname}
32+
onChange={e => (setEmployee({...employee, lname: e.target.value}))}
33+
/></td>
34+
<td><input id="newEmployeeHireDate" type="text" value={employee.hdate}
35+
onChange={e => (setEmployee({...employee, hdate: e.target.value}))}
36+
/></td>
37+
<td><input id="newEmployeeRole" type="text" value={employee.role}
38+
onChange={e => (setEmployee({...employee, role: e.target.value}))}
39+
/></td>
40+
</tr>
41+
</thead>
42+
</table>
43+
<button>New Record</button>
44+
</form>
45+
);
46+
}
47+
48+
export default NewEmployeeForm

0 commit comments

Comments
 (0)