neuer demo tenant switcher + demo tenants mit eigenem artisan command. Event Admin überarbeitet, aber das ist nur ein Zwischenstand.
This commit is contained in:
@@ -9,8 +9,10 @@ const STORAGE_KEY = 'tenant-admin.active-event';
|
||||
export interface EventContextValue {
|
||||
events: TenantEvent[];
|
||||
isLoading: boolean;
|
||||
isError: boolean;
|
||||
activeEvent: TenantEvent | null;
|
||||
selectEvent: (slug: string | null) => void;
|
||||
refetch: () => void;
|
||||
}
|
||||
|
||||
const EventContext = React.createContext<EventContextValue | undefined>(undefined);
|
||||
@@ -29,11 +31,13 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
|
||||
const {
|
||||
data: fetchedEvents = [],
|
||||
isLoading: queryLoading,
|
||||
isError,
|
||||
refetch,
|
||||
} = useQuery<TenantEvent[]>({
|
||||
queryKey: ['tenant-events'],
|
||||
queryFn: async () => {
|
||||
try {
|
||||
return await getEvents();
|
||||
return await getEvents({ force: true });
|
||||
} catch (error) {
|
||||
console.warn('[EventContext] Failed to fetch events', error);
|
||||
throw error;
|
||||
@@ -48,9 +52,17 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
|
||||
const isLoading = authReady ? queryLoading : status === 'loading';
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!storedSlug && events.length === 1 && events[0]?.slug && typeof window !== 'undefined') {
|
||||
setStoredSlug(events[0].slug);
|
||||
window.localStorage.setItem(STORAGE_KEY, events[0].slug);
|
||||
if (!events.length || typeof window === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasStored = Boolean(storedSlug);
|
||||
const slugExists = hasStored && events.some((event) => event.slug === storedSlug);
|
||||
const fallbackSlug = events[0]?.slug;
|
||||
|
||||
if (!slugExists && fallbackSlug) {
|
||||
setStoredSlug(fallbackSlug);
|
||||
window.localStorage.setItem(STORAGE_KEY, fallbackSlug);
|
||||
}
|
||||
}, [events, storedSlug]);
|
||||
|
||||
@@ -64,11 +76,8 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
|
||||
return matched;
|
||||
}
|
||||
|
||||
if (!storedSlug && events.length === 1) {
|
||||
return events[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
// Fallback to the first event if the stored slug is missing or stale.
|
||||
return events[0];
|
||||
}, [events, storedSlug]);
|
||||
|
||||
const selectEvent = React.useCallback((slug: string | null) => {
|
||||
@@ -86,10 +95,12 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
|
||||
() => ({
|
||||
events,
|
||||
isLoading,
|
||||
isError,
|
||||
activeEvent,
|
||||
selectEvent,
|
||||
refetch,
|
||||
}),
|
||||
[events, isLoading, activeEvent, selectEvent]
|
||||
[events, isLoading, isError, activeEvent, selectEvent, refetch]
|
||||
);
|
||||
|
||||
return <EventContext.Provider value={value}>{children}</EventContext.Provider>;
|
||||
|
||||
Reference in New Issue
Block a user