From 31c6ae3f5c93f00b587c6e7a427d2dd5d2932db5 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Wed, 19 Mar 2025 09:00:26 +0100 Subject: [PATCH] Add refetch methods to event context and integrate in layout Introduce `refetchUpcomingEvents`, `refetchPublicEvents`, and `refetchUserEvents` to the event context for improved data management. These methods are now invoked in the dashboard layout to ensure events data is refreshed upon user authentication. --- frontend/src/app/(main)/dashboard/layout.tsx | 7 +++++++ frontend/src/context/event-context.tsx | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/frontend/src/app/(main)/dashboard/layout.tsx b/frontend/src/app/(main)/dashboard/layout.tsx index c5ff5e1..ee1e1dc 100644 --- a/frontend/src/app/(main)/dashboard/layout.tsx +++ b/frontend/src/app/(main)/dashboard/layout.tsx @@ -5,6 +5,7 @@ import { useRouter, usePathname } from "next/navigation"; import { useEffect } from "react"; import Navbar from "@/components/layout/navbar"; import Breadcrumbs from "@/components/layout/breadcrumb"; +import { useEvents } from "@/context/event-context"; export default function MainLayout({ children, @@ -12,12 +13,18 @@ export default function MainLayout({ children: React.ReactNode; }) { const { isAuthenticated, isLoading } = useAuth(); + const { refetchUpcomingEvents, refetchPublicEvents, refetchUserEvents } = + useEvents(); const router = useRouter(); const pathname = usePathname(); useEffect(() => { if (!isLoading && !isAuthenticated) { router.push("/login"); + } else { + refetchUpcomingEvents(); + refetchPublicEvents(); + refetchUserEvents(); } }, [isAuthenticated, isLoading, router]); diff --git a/frontend/src/context/event-context.tsx b/frontend/src/context/event-context.tsx index c70a3ee..4a7f4fe 100644 --- a/frontend/src/context/event-context.tsx +++ b/frontend/src/context/event-context.tsx @@ -32,6 +32,11 @@ interface EventsContextState { userEvents: PaginatedResponseEventResponse | undefined; upcomingEvents: PaginatedResponseEventResponse | undefined; publicEvents: PaginatedResponseEventResponse | undefined; + + refetchUpcomingEvents: () => Promise; + refetchPublicEvents: () => Promise; + refetchUserEvents: () => Promise; + isLoadingUserEvents: boolean; isLoadingUpcomingEvents: boolean; isLoadingPublicEvents: boolean; @@ -67,6 +72,11 @@ const defaultEventsState: EventsContextState = { userEvents: undefined, upcomingEvents: undefined, publicEvents: undefined, + + refetchUpcomingEvents: async () => undefined, + refetchPublicEvents: async () => undefined, + refetchUserEvents: async () => undefined, + isLoadingUserEvents: false, isLoadingUpcomingEvents: false, isLoadingPublicEvents: false, @@ -148,6 +158,7 @@ export const EventsProvider: React.FC = ({ children }) => { data: userEvents, isLoading: isLoadingUserEvents, error: userEventsError, + refetch: refetchUserEvents, } = useQuery({ ...getUserEventsOptions(paginationParams), }); @@ -157,6 +168,7 @@ export const EventsProvider: React.FC = ({ children }) => { data: upcomingEvents, isLoading: isLoadingUpcomingEvents, error: upcomingEventsError, + refetch: refetchUpcomingEvents, } = useQuery({ ...getUpcomingEventsOptions(paginationParams), }); @@ -166,6 +178,7 @@ export const EventsProvider: React.FC = ({ children }) => { data: publicEvents, isLoading: isLoadingPublicEvents, error: publicEventsError, + refetch: refetchPublicEvents, } = useQuery({ ...getPublicEventsOptions(paginationParams), }); @@ -255,6 +268,11 @@ export const EventsProvider: React.FC = ({ children }) => { userEvents, upcomingEvents, publicEvents, + + refetchPublicEvents, + refetchUpcomingEvents, + refetchUserEvents, + isLoadingUserEvents, isLoadingUpcomingEvents, isLoadingPublicEvents,