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:
@@ -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]);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user