diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx index 69d1371..da3bc3c 100644 --- a/src/hooks/useAuth.tsx +++ b/src/hooks/useAuth.tsx @@ -21,31 +21,46 @@ export function AuthProvider({ children }: { children: ReactNode }) { const [isAdmin, setIsAdmin] = useState(false); useEffect(() => { - const { data: { subscription } } = supabase.auth.onAuthStateChange( - (event, session) => { - setSession(session); - setUser(session?.user ?? null); - - if (session?.user) { - setTimeout(() => { - checkAdminRole(session.user.id); - }, 0); - } else { - setIsAdmin(false); - } - } - ); + let mounted = true; + // First, get the initial session supabase.auth.getSession().then(({ data: { session } }) => { + if (!mounted) return; + setSession(session); setUser(session?.user ?? null); + if (session?.user) { checkAdminRole(session.user.id); } + + // Only set loading to false after initial session is loaded setLoading(false); }); - return () => subscription.unsubscribe(); + // Then listen for auth state changes + const { data: { subscription } } = supabase.auth.onAuthStateChange( + (event, session) => { + if (!mounted) return; + + setSession(session); + setUser(session?.user ?? null); + + if (session?.user) { + checkAdminRole(session.user.id); + } else { + setIsAdmin(false); + } + + // Ensure loading is false after auth state changes + setLoading(false); + } + ); + + return () => { + mounted = false; + subscription.unsubscribe(); + }; }, []); const checkAdminRole = async (userId: string) => { diff --git a/src/pages/admin/AdminOrders.tsx b/src/pages/admin/AdminOrders.tsx index cd5210b..b09a9d2 100644 --- a/src/pages/admin/AdminOrders.tsx +++ b/src/pages/admin/AdminOrders.tsx @@ -508,87 +508,110 @@ export default function AdminOrders() { )} - {/* Consulting Slots */} - {consultingSlots.length > 0 && ( -
- - Jadwal Konsultasi -
-- {new Date(slot.date).toLocaleDateString("id-ID", { - weekday: "short", - day: "numeric", - month: "short", - year: "numeric" - })} -
-- {slot.start_time.substring(0, 5)} - {slot.end_time.substring(0, 5)} WIB -
- {slot.notes && ( -- Catatan: {slot.notes} -
- )} -