diff --git a/client/src/components/Layout.tsx b/client/src/components/Layout.tsx index eff853e..e900af4 100644 --- a/client/src/components/Layout.tsx +++ b/client/src/components/Layout.tsx @@ -1,4 +1,4 @@ -import { ReactNode, useState, createContext, useContext } from "react"; +import { ReactNode, useState, createContext, useContext, useEffect } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; @@ -18,6 +18,7 @@ interface StoreContextType { setSidebarCollapsed: (collapsed: boolean) => void; mobileMenuOpen: boolean; setMobileMenuOpen: (open: boolean) => void; + storeInitialized: boolean; } const StoreContext = createContext(undefined); @@ -46,6 +47,8 @@ export default function Layout({ children }: LayoutProps) { console.log('🏪 Layout - Restoring selectedStoreId from localStorage:', { saved, restoredId }); return restoredId; }); + + const [storeInitialized, setStoreInitialized] = useState(false); const [sidebarCollapsed, setSidebarCollapsed] = useState(() => { // Restaurer l'état de la sidebar depuis localStorage, mais forcer collapsed sur mobile @@ -60,12 +63,34 @@ export default function Layout({ children }: LayoutProps) { enabled: !!user, }); + // Effet pour marquer l'initialisation comme terminée + useEffect(() => { + if (user && stores.length > 0) { + // Pour les admins, vérifier que selectedStoreId est cohérent avec les stores disponibles + if (user.role === 'admin' && selectedStoreId) { + const storeExists = stores.find(store => store.id === selectedStoreId); + if (!storeExists) { + console.log('🏪 Selected store not found in available stores, clearing selection'); + setSelectedStoreId(null); + localStorage.removeItem('selectedStoreId'); + } + } + + console.log('🏪 Store initialization complete:', { + user: user.role, + selectedStoreId, + storesCount: stores.length + }); + setStoreInitialized(true); + } + }, [user, stores, selectedStoreId]); + const handleLogout = () => { window.location.href = "/api/logout"; }; return ( - +
{/* Mobile overlay */} {isMobile && mobileMenuOpen && ( diff --git a/client/src/pages/Tasks.tsx b/client/src/pages/Tasks.tsx index ca26dd2..955fdac 100644 --- a/client/src/pages/Tasks.tsx +++ b/client/src/pages/Tasks.tsx @@ -39,7 +39,7 @@ type TaskWithRelations = Task & { export default function Tasks() { const { user } = useAuthUnified(); - const { selectedStoreId } = useStore(); + const { selectedStoreId, storeInitialized } = useStore(); const { toast } = useToast(); const queryClient = useQueryClient(); @@ -57,14 +57,20 @@ export default function Tasks() { const [selectedTask, setSelectedTask] = useState(null); const [taskToDelete, setTaskToDelete] = useState(null); - // Fetch tasks + // Fetch tasks - attendre que l'initialisation des stores soit terminée pour les admins const { data: tasks = [], isLoading } = useQuery({ queryKey: ["/api/tasks", selectedStoreId], queryFn: () => { const params = new URLSearchParams(); - if (selectedStoreId) { + if (selectedStoreId && user?.role === 'admin') { params.append('storeId', selectedStoreId.toString()); } + console.log('📋 TASKS QUERY - Fetching with params:', { + selectedStoreId, + userRole: user?.role, + storeInitialized, + url: `/api/tasks?${params.toString()}` + }); return fetch(`/api/tasks?${params.toString()}`, { credentials: 'include' }).then(res => { @@ -74,7 +80,7 @@ export default function Tasks() { return res.json(); }); }, - enabled: !!user, + enabled: !!user && (user.role !== 'admin' || storeInitialized), }); // Fetch users for task assignment - seulement pour admin/manager/directeur diff --git a/server/routes.ts b/server/routes.ts index 4bf8ca1..ecc8fb0 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -1265,11 +1265,19 @@ export async function registerRoutes(app: Express): Promise { userRole: user.role, userId: user.id, requestedStoreId: storeId, - userGroups: user.role !== 'admin' ? user.userGroups.map(ug => ug.groupId) : 'all' + userGroups: user.role !== 'admin' ? user.userGroups.map(ug => ug.groupId) : 'all', + timestamp: new Date().toISOString() }); const tasks = await storage.getTasks(groupIds); - console.log('📋 Tasks returned:', tasks.length, 'items for user:', user.id); + console.log('📋 Tasks returned:', { + count: tasks.length, + userId: user.id, + userRole: user.role, + requestedStoreId: storeId, + groupIds, + taskGroups: tasks.map(t => ({ id: t.id, title: t.title, groupId: t.groupId })).slice(0, 3) + }); res.json(tasks); } catch (error) { console.error("Error fetching tasks:", error);