From 1dd3ff92929fd84123c3f8ac39a30003fd079e2c Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Sun, 22 Sep 2024 03:59:35 +0300 Subject: [PATCH] Add ids and relations to the API responses, add completion flag to data loading hook --- src/hooks/useSchedule.js | 49 ++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/hooks/useSchedule.js b/src/hooks/useSchedule.js index a5f0862..0ec43ad 100644 --- a/src/hooks/useSchedule.js +++ b/src/hooks/useSchedule.js @@ -4,45 +4,65 @@ import useTracks from './useTracks.js'; import useEventTypes from './useEventTypes.js'; import useHalls from './useHalls.js'; import useSlots from './useSlots.js'; -import { calculateProgress } from '../utils.js'; +import { addIdAndRelations, calculateProgress } from '../utils.js'; +import { useMemo } from 'react'; export default function useSchedule(conferenceId) { const { - data: events, - isLoading: eventsLoading, - isValidating: eventsValidating, - } = useEvents(conferenceId); - - const { - data: speakers, + data: speakersResponse, isLoading: speakersLoading, isValidating: speakersValidating, } = useSpeakers(conferenceId); + const speakers = useMemo(() => addIdAndRelations(speakersResponse || []), [speakersResponse]); + const { - data: tracks, + data: tracksResponse, isLoading: tracksLoading, isValidating: tracksValidating, } = useTracks(conferenceId); + const tracks = useMemo(() => addIdAndRelations(tracksResponse || []), [tracksResponse]); + const { - data: eventTypes, + data: eventTypesResponse, isLoading: eventTypesLoading, isValidating: eventTypesValidating, } = useEventTypes(conferenceId); + const eventTypes = useMemo(() => addIdAndRelations(eventTypesResponse || []), [eventTypesResponse]); + const { - data: halls, + data: hallsResponse, isLoading: hallsLoading, isValidating: hallsValidating, } = useHalls(conferenceId); + const halls = useMemo(() => addIdAndRelations(hallsResponse || []), [hallsResponse]); + const { - data: slots, + data: eventsResponse, + isLoading: eventsLoading, + isValidating: eventsValidating, + } = useEvents(conferenceId); + + const events = useMemo(() => addIdAndRelations(eventsResponse || [], [ + ['event_type', eventTypes, 'event_type_id'], + ['track', tracks, 'track_id'], + ['participant_users', speakers, 'participant_user_ids'], + ]), [eventsResponse, eventTypes, tracks, speakers]); + + const { + data: slotsResponse, isLoading: slotsLoading, isValidating: slotsValidating, } = useSlots(conferenceId); + const slots = useMemo(() => addIdAndRelations(slotsResponse || [], [ + ['hall', halls, 'hall_id'], + ['event', events, 'event_id'], + ]), [slotsResponse, halls, events]); + const { isStarted: isLoading, remainingProgress: loadingProgress, @@ -53,6 +73,10 @@ export default function useSchedule(conferenceId) { remainingProgress: validatingProgress, } = calculateProgress(eventsValidating, speakersValidating, tracksValidating, eventTypesValidating, hallsValidating, slotsValidating); + const { + isComplete, + } = calculateProgress(events, speakers, tracks, eventTypes, halls, slots); + return { events, speakers, @@ -64,5 +88,6 @@ export default function useSchedule(conferenceId) { loadingProgress, isValidating, validatingProgress, + isComplete, }; }