From 4b2ce2c3d1b12d8af3f00a18966f8f2f2d076118 Mon Sep 17 00:00:00 2001 From: Andreas Ellewsen <ae@uio.no> Date: Thu, 9 Dec 2021 15:57:33 +0100 Subject: [PATCH] Simplify guest fetching --- frontend/src/hooks/useGuests/index.tsx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/frontend/src/hooks/useGuests/index.tsx b/frontend/src/hooks/useGuests/index.tsx index 04e62653..34de36ff 100644 --- a/frontend/src/hooks/useGuests/index.tsx +++ b/frontend/src/hooks/useGuests/index.tsx @@ -1,16 +1,14 @@ import { useEffect, useState } from 'react' import { FetchedGuest, Guest } from 'interfaces' -import { parseIdentity, parseRole } from 'utils' +import { parseIdentity, parseRole, fetchJsonOpts } from 'utils' const useGuests = () => { const [guests, setGuests] = useState<Guest[]>([]) - const getGuestsInfo = async () => { - try { - const response = await fetch('/api/ui/v1/guests/?format=json') - const jsonResponse = await response.json() - if (response.ok) { - const persons = await jsonResponse + const getGuestsInfo = () => + fetch('/api/ui/v1/guests/', fetchJsonOpts()) + .then((response) => (response.ok ? response.json() : [])) + .then((persons) => { setGuests( persons.map( (person: FetchedGuest): Guest => ({ @@ -28,11 +26,8 @@ const useGuests = () => { }) ) ) - } - } catch (error) { - setGuests([]) - } - } + }) + .catch(() => setGuests([])) const reloadGuests = () => { getGuestsInfo() -- GitLab