Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Commit f5408c8

Browse files
committed
23. Show an error message (attempted)
Finally, display an error message in <Bookings /> if there is an HTTP error when fetching data from the server. To test this, try loading data from https://cyf-react.glitch.me/error, which will return a 500 HTTP error. Hint: Try looking at your Pokemon app that you worked on in class for an example. Test: When loading bookings data from the /error endpoint, an error message should be displayed on the screen.
1 parent bc22ee1 commit f5408c8

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/Bookings.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
11
import React, {useEffect, useState} from 'react';
22
import Search from "./Search.js";
33
import SearchResults from "./components/SearchResults.jsx";
4-
5-
64
// import SearchResultsOther from './components/SearchResultsOther.js';
75

86
const Bookings = () => {
97
const [bookings, setBookings] = useState([]);
8+
109
const [searchVal, setSearchVal] = useState("");
1110

1211
const [isLoading, setIsLoading] = useState(true);
13-
12+
13+
const [isError, setIsError] = useState(false);
14+
1415
useEffect(() => {
15-
fetch("https://cyf-react.glitch.me/delayed")
16+
fetch("https://cyf-react.glitch.me/error")
1617
.then((res) => res.json())
1718
.then((data) => {
18-
setIsLoading(false);
19-
setBookings(data);
20-
});
19+
if (data.error !== undefined) {
20+
console.log("error");
21+
setIsError(true);
22+
} else {
23+
setIsLoading(false);
24+
setBookings(data);
25+
}
26+
})
2127
}, []);
2228

29+
/*
30+
isLoading: true, isError: false,
31+
isLoading: false, isError: false,
32+
isLoading: false, isError: true,
33+
*/
2334

24-
const search = searchVal => {
25-
console.log("TO DO!", searchVal);
26-
setSearchVal(searchVal);
27-
}
35+
const search = (searchVal) => {
36+
console.log("TO DO!", searchVal);
37+
setSearchVal(searchVal);
38+
};
2839

29-
const displayedBookings = bookings.filter(
40+
let displayedBookings = bookings.filter(
3041
(booking) =>
3142
booking.firstName.toLowerCase().includes(searchVal.toLowerCase()) ||
3243
booking.surname.toLowerCase().includes(searchVal.toLowerCase())
3344
);
3445

35-
46+
3647
return (
3748
<div className="App-content">
3849
<div className="container">
39-
{isLoading ? <p>Page loading...</p> : ( <>
50+
{isError ? (
51+
<p>Error loading...</p>
52+
) : isLoading ? (
53+
<p>Page loading...</p>
54+
) : (
55+
<>
4056
<Search search={search} />
41-
<SearchResults results={displayedBookings} />
57+
<SearchResults results={displayedBookings} />
4258
</>
4359
)}
4460
{/* <SearchResultsOther results={bookings} /> */}
@@ -48,3 +64,16 @@ const search = searchVal => {
4864
};
4965

5066
export default Bookings;
67+
68+
69+
70+
{/* {isError && !isLoading ? (
71+
<p>Error loading...</p>
72+
) : isLoading ? (
73+
<p>Page loading...</p>
74+
) : (
75+
<>
76+
<Search search={search} />
77+
<SearchResults results={displayedBookings} />
78+
</>
79+
)} */}

0 commit comments

Comments
 (0)