mirror of
https://github.com/R0m1k3/Regisflow.git
synced 2026-10-20 05:38:18 +02:00
Enable store selection for admins and associate sales with specific stores
Implements store selection and association functionality using useStoreContext hook and updates sale creation and retrieval APIs. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/d304bad8-e4eb-4558-b9d6-38704ba052e8.jpg
This commit is contained in:
1 parent
a5336a5919
commit
4384818f63
5 files changed
+216
-7
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { apiRequest } from '@/lib/queryClient';
|
||||
import type { Store } from '@shared/schema';
|
||||
|
||||
interface StoreContextType {
|
||||
selectedStoreId: number | null;
|
||||
setSelectedStoreId: (storeId: number | null) => void;
|
||||
stores: Store[];
|
||||
selectedStore: Store | null;
|
||||
isLoadingStores: boolean;
|
||||
}
|
||||
|
||||
const StoreContext = createContext<StoreContextType | undefined>(undefined);
|
||||
|
||||
interface StoreProviderProps {
|
||||
children: ReactNode;
|
||||
userStoreId?: number | null;
|
||||
isAdmin?: boolean;
|
||||
}
|
||||
|
||||
export function StoreProvider({ children, userStoreId, isAdmin }: StoreProviderProps) {
|
||||
const [selectedStoreId, setSelectedStoreId] = useState<number | null>(null);
|
||||
|
||||
// Query all stores for admin, or just get user's store for non-admin
|
||||
const { data: stores = [], isLoading: isLoadingStores } = useQuery<Store[]>({
|
||||
queryKey: isAdmin ? ['/api/admin/stores'] : ['/api/stores'],
|
||||
queryFn: async () => {
|
||||
const endpoint = isAdmin ? '/api/admin/stores' : '/api/stores';
|
||||
const response = await apiRequest(endpoint);
|
||||
return response.json();
|
||||
},
|
||||
enabled: !!isAdmin || !!userStoreId,
|
||||
});
|
||||
|
||||
// Set initial selected store
|
||||
useEffect(() => {
|
||||
if (stores.length > 0 && selectedStoreId === null) {
|
||||
if (isAdmin) {
|
||||
// Admin: select first store by default
|
||||
setSelectedStoreId(stores[0].id);
|
||||
} else if (userStoreId) {
|
||||
// Non-admin: select user's store
|
||||
setSelectedStoreId(userStoreId);
|
||||
}
|
||||
}
|
||||
}, [stores, selectedStoreId, isAdmin, userStoreId]);
|
||||
|
||||
const selectedStore = stores.find(store => store.id === selectedStoreId) || null;
|
||||
|
||||
return (
|
||||
<StoreContext.Provider value={{
|
||||
selectedStoreId,
|
||||
setSelectedStoreId,
|
||||
stores,
|
||||
selectedStore,
|
||||
isLoadingStores,
|
||||
}}>
|
||||
{children}
|
||||
</StoreContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useStoreContext() {
|
||||
const context = useContext(StoreContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useStoreContext must be used within a StoreProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in new issue
Block a user