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 { useEffect } from "react";
import Navbar from "@/components/layout/navbar"; import Navbar from "@/components/layout/navbar";
import Breadcrumbs from "@/components/layout/breadcrumb"; import Breadcrumbs from "@/components/layout/breadcrumb";
import { useEvents } from "@/context/event-context";
export default function MainLayout({ export default function MainLayout({
children, children,
@@ -12,12 +13,18 @@ export default function MainLayout({
children: React.ReactNode; children: React.ReactNode;
}) { }) {
const { isAuthenticated, isLoading } = useAuth(); const { isAuthenticated, isLoading } = useAuth();
const { refetchUpcomingEvents, refetchPublicEvents, refetchUserEvents } =
useEvents();
const router = useRouter(); const router = useRouter();
const pathname = usePathname(); const pathname = usePathname();
useEffect(() => { useEffect(() => {
if (!isLoading && !isAuthenticated) { if (!isLoading && !isAuthenticated) {
router.push("/login"); router.push("/login");
} else {
refetchUpcomingEvents();
refetchPublicEvents();
refetchUserEvents();
} }
}, [isAuthenticated, isLoading, router]); }, [isAuthenticated, isLoading, router]);

View File

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