improvement: sidebar refactored. COMMING SOON page added for all non-developed tabs.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { authApi } from '@/lib/api/auth';
|
||||
import { User, Organization } from '@/types';
|
||||
@@ -37,11 +37,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
const normalizeProfilePayload = (payload: any): { user: User | null; organizations: Organization[] } => {
|
||||
const normalizeProfilePayload = useCallback((payload: any): { user: User | null; organizations: Organization[] } => {
|
||||
const organizations = payload?.organizations || [];
|
||||
|
||||
if (payload?.user) {
|
||||
@@ -60,9 +56,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}
|
||||
|
||||
return { user: null, organizations };
|
||||
};
|
||||
}, []);
|
||||
|
||||
const checkAuth = async () => {
|
||||
const checkAuth = useCallback(async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
@@ -103,10 +99,14 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
setIsLoading(false);
|
||||
setIsAuthReady(true);
|
||||
}
|
||||
};
|
||||
}, [normalizeProfilePayload]);
|
||||
|
||||
useEffect(() => {
|
||||
void checkAuth();
|
||||
}, [checkAuth]);
|
||||
|
||||
// ✅ REGISTER
|
||||
const registerTrial = async (
|
||||
const registerTrial = useCallback(async (
|
||||
email: string,
|
||||
password: string,
|
||||
name: string,
|
||||
@@ -147,10 +147,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
}, [router]);
|
||||
|
||||
// ✅ LOGIN
|
||||
const login = async (email: string, password: string) => {
|
||||
const login = useCallback(async (email: string, password: string) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -178,17 +178,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
}, [router]);
|
||||
|
||||
const logout = async () => {
|
||||
const logout = useCallback(async () => {
|
||||
localStorage.clear();
|
||||
setUser(null);
|
||||
setOrganizations([]);
|
||||
setCurrentOrganization(null);
|
||||
router.push('/');
|
||||
};
|
||||
}, [router]);
|
||||
|
||||
const selectOrganization = async (orgId: string) => {
|
||||
const selectOrganization = useCallback(async (orgId: string) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
|
||||
@@ -208,26 +208,41 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
}, [router]);
|
||||
|
||||
const clearError = () => setError(null);
|
||||
const clearError = useCallback(() => setError(null), []);
|
||||
|
||||
const contextValue = useMemo(
|
||||
() => ({
|
||||
user,
|
||||
organizations,
|
||||
currentOrganization,
|
||||
isLoading,
|
||||
isAuthReady,
|
||||
error,
|
||||
registerTrial,
|
||||
login,
|
||||
logout,
|
||||
selectOrganization,
|
||||
clearError,
|
||||
}),
|
||||
[
|
||||
user,
|
||||
organizations,
|
||||
currentOrganization,
|
||||
isLoading,
|
||||
isAuthReady,
|
||||
error,
|
||||
registerTrial,
|
||||
login,
|
||||
logout,
|
||||
selectOrganization,
|
||||
clearError,
|
||||
],
|
||||
);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user,
|
||||
organizations,
|
||||
currentOrganization,
|
||||
isLoading,
|
||||
isAuthReady, // ✅ expose it
|
||||
error,
|
||||
registerTrial,
|
||||
login,
|
||||
logout,
|
||||
selectOrganization,
|
||||
clearError,
|
||||
}}
|
||||
>
|
||||
<AuthContext.Provider value={contextValue}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user