File tree Expand file tree Collapse file tree 1 file changed +17
-9
lines changed
Expand file tree Collapse file tree 1 file changed +17
-9
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,21 @@ import "./App.css";
33import { ProductRow } from "./ProductRow" ;
44
55function App ( ) {
6- const [ lastRequest , setLastRequest ] = useState ( null ) ;
76 const [ catalog , setCatalog ] = useState ( null ) ;
7+ const [ errorOccurred , setErrorOccurred ] = useState ( false ) ;
88
99 const fetchCatalog = useCallback ( ( ) => {
10+ setErrorOccurred ( false ) ;
11+
1012 fetch ( "/api/products" )
1113 . then ( ( response ) => response . json ( ) )
1214 . then ( ( data ) => {
1315 setCatalog ( data ) ;
14- setLastRequest ( {
15- method : "GET" ,
16- url : "/api/products" ,
17- status : 200 ,
18- response : data ,
19- } ) ;
16+ } )
17+ . catch ( ( e ) => {
18+ setErrorOccurred ( e ) ;
2019 } ) ;
21- } , [ ] ) ;
20+ } , [ setErrorOccurred , setCatalog ] ) ;
2221
2322 const createProduct = useCallback ( ( ) => {
2423 const body = {
@@ -77,7 +76,16 @@ function App() {
7776 ) }
7877 </ >
7978 ) : (
80- < p > Loading catalog...</ p >
79+ < >
80+ { errorOccurred ? (
81+ < p >
82+ An error occurred while fetching the catalog. Is the backend
83+ running?
84+ </ p >
85+ ) : (
86+ < p > Loading catalog...</ p >
87+ ) }
88+ </ >
8189 ) }
8290 </ >
8391 ) ;
You can’t perform that action at this time.
0 commit comments