diff --git a/frontend/src/hooks/useGuests/index.tsx b/frontend/src/hooks/useGuests/index.tsx index 04e62653ac34b8b3b2a8882899d508db454fac05..34de36ff949a3f4d04525595008fb4c98a6c8f99 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()