Ensure tasks display correctly for selected stores by admins

Fixes an issue where admin users had to refresh the page to see tasks filtered by their selected store. This change ensures the store selection is properly initialized and validated before fetching tasks, preventing stale data and improving the user experience. The backend task fetching logic is also updated to correctly handle store filtering.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e437ffed-c31b-44eb-9fd5-b6f9faa8e7ce
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/e437ffed-c31b-44eb-9fd5-b6f9faa8e7ce/OvGzl7P
This commit is contained in:
michaelschal committed 2025-08-16 07:48:04 +00:00
1 parent 9d9344f52e
commit 5688450117
3 files changed
+47 -8

No files matched your search

+10 -4
View File
@@ -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<TaskWithRelations | null>(null);
const [taskToDelete, setTaskToDelete] = useState<TaskWithRelations | null>(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