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.
This commit is contained in:
2025-03-19 09:00:26 +01:00
parent 678c55a1e2
commit 31c6ae3f5c
2 changed files with 25 additions and 0 deletions

View File

@@ -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]);

View File

@@ -32,6 +32,11 @@ interface EventsContextState {
userEvents: PaginatedResponseEventResponse | undefined;
upcomingEvents: PaginatedResponseEventResponse | undefined;
publicEvents: PaginatedResponseEventResponse | undefined;
refetchUpcomingEvents: () => Promise<any>;
refetchPublicEvents: () => Promise<any>;
refetchUserEvents: () => Promise<any>;
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<EventsProviderProps> = ({ children }) => {
data: userEvents,
isLoading: isLoadingUserEvents,
error: userEventsError,
refetch: refetchUserEvents,
} = useQuery({
...getUserEventsOptions(paginationParams),
});
@@ -157,6 +168,7 @@ export const EventsProvider: React.FC<EventsProviderProps> = ({ children }) => {
data: upcomingEvents,
isLoading: isLoadingUpcomingEvents,
error: upcomingEventsError,
refetch: refetchUpcomingEvents,
} = useQuery({
...getUpcomingEventsOptions(paginationParams),
});
@@ -166,6 +178,7 @@ export const EventsProvider: React.FC<EventsProviderProps> = ({ children }) => {
data: publicEvents,
isLoading: isLoadingPublicEvents,
error: publicEventsError,
refetch: refetchPublicEvents,
} = useQuery({
...getPublicEventsOptions(paginationParams),
});
@@ -255,6 +268,11 @@ export const EventsProvider: React.FC<EventsProviderProps> = ({ children }) => {
userEvents,
upcomingEvents,
publicEvents,
refetchPublicEvents,
refetchUpcomingEvents,
refetchUserEvents,
isLoadingUserEvents,
isLoadingUpcomingEvents,
isLoadingPublicEvents,