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

Commit 2eaada1

Browse files
committed
Replace axios calls with fetch.
1 parent 72df28e commit 2eaada1

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

client/package-lock.json

-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"@testing-library/jest-dom": "^5.14.1",
77
"@testing-library/react": "^11.2.7",
88
"@testing-library/user-event": "^12.8.3",
9-
"axios": "^0.21.1",
109
"react": "^17.0.2",
1110
"react-dom": "^17.0.2",
1211
"react-scripts": "4.0.3",

client/src/App.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ import EmployeeTable from './EmployeeTable';
44
import NewEmployeeForm from "./NewEmployeeForm";
55

66
import {useEffect, useState} from "react";
7-
import axios from "axios";
87

98
function App() {
109
const [employees, setEmployees] = useState([]);
1110

1211
const reloadEmployeeTable = () => {
13-
axios.get('/api/employees').then(
14-
response => {
15-
setEmployees(response.data);
16-
}
17-
);
12+
fetch(`/api/employees`, {
13+
method: 'GET',
14+
accept: "Application/JSON"
15+
})
16+
.then(response => {
17+
if (response.ok) {
18+
return response.json();
19+
}
20+
else {
21+
throw new Error("Unable to fetch employee list.")
22+
}
23+
})
24+
.then(updatedEmployees => setEmployees(updatedEmployees))
25+
.catch(error => console.log(error));
1826
};
1927

2028
// Populate the table

client/src/EmployeeTable.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function EmployeeTable(props) {
1111
}
1212

1313
// PUT all modified employees
14-
const handleSubmitButton = () => {
14+
const handleSubmitButton = event => {
15+
event.preventDefault();
16+
1517
let requests = [];
1618

1719
for(let id in modifiedEmployees) {

client/src/NewEmployeeForm.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import {useState} from "react";
2-
import axios from "axios";
32

43
function NewEmployeeForm(props) {
54

65
const [employee, setEmployee] = useState({fname: '', lname: '', hdate: '', role: ''});
76

87
const handleSubmit = (event) => {
98
event.preventDefault();
10-
console.log(employee);
11-
axios.post('api/employees', employee)
9+
fetch(`/api/employees`, {
10+
method: 'POST',
11+
headers: {'Content-Type': 'application/json'},
12+
body: JSON.stringify(employee)
13+
})
1214
.then(() => {
1315
props.handleNewEmployeeAdded();
1416
setEmployee({fname: '', lname: '', hdate: '', role: ''});

0 commit comments

Comments
 (0)