diff --git a/05_react/unterricht/tag51/01_react-app-with-tests/src/components/lists/ListUser.jsx b/05_react/unterricht/tag51/01_react-app-with-tests/src/components/lists/ListUser.jsx index 66edae2..442d44f 100644 --- a/05_react/unterricht/tag51/01_react-app-with-tests/src/components/lists/ListUser.jsx +++ b/05_react/unterricht/tag51/01_react-app-with-tests/src/components/lists/ListUser.jsx @@ -5,7 +5,7 @@ const ListUser = (props) => { // const { } = props; const [users, setUsers] = useState([]); const [isLoading, setIsLoading] = useState(false); - // const [error, setError] = useState(null); + const [error, setError] = useState(null); useEffect(() => { const controller = new AbortController(); // 2019 @@ -18,7 +18,8 @@ const ListUser = (props) => { }) .then(({ data }) => { console.log(data); - setUsers(data?.users); + setUsers(data?.users ?? []); + setError(null); }) .catch((err) => { // @@ -27,6 +28,7 @@ const ListUser = (props) => { console.error('Request abgebrochen:', err.message); } else { console.error('Error:', err); + setError(err); } }) .finally(() => { @@ -47,6 +49,15 @@ const ListUser = (props) => { return

is loading users...

; } + if (error) { + const status = error.response?.status; + return ( +

+ {status ? `Failed to load users (HTTP ${status})` : 'Failed to load users'} +

+ ); + } + return (