Improve teh securty of teh DB
This commit is contained in:
141
frontend/src/api/hooks/equipment.ts
Normal file
141
frontend/src/api/hooks/equipment.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
// frontend/src/api/hooks/equipment.ts
|
||||
/**
|
||||
* React hooks for Equipment API integration
|
||||
*/
|
||||
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import { equipmentService } from '../services/equipment';
|
||||
import type { Equipment } from '../types/equipment';
|
||||
|
||||
// Query Keys
|
||||
export const equipmentKeys = {
|
||||
all: ['equipment'] as const,
|
||||
lists: () => [...equipmentKeys.all, 'list'] as const,
|
||||
list: (tenantId: string, filters?: Record<string, any>) =>
|
||||
[...equipmentKeys.lists(), tenantId, filters] as const,
|
||||
details: () => [...equipmentKeys.all, 'detail'] as const,
|
||||
detail: (tenantId: string, equipmentId: string) =>
|
||||
[...equipmentKeys.details(), tenantId, equipmentId] as const,
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook to fetch equipment list
|
||||
*/
|
||||
export function useEquipment(
|
||||
tenantId: string,
|
||||
filters?: {
|
||||
status?: string;
|
||||
type?: string;
|
||||
is_active?: boolean;
|
||||
},
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: equipmentKeys.list(tenantId, filters),
|
||||
queryFn: () => equipmentService.getEquipment(tenantId, filters),
|
||||
enabled: !!tenantId && (options?.enabled ?? true),
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to fetch a specific equipment item
|
||||
*/
|
||||
export function useEquipmentById(
|
||||
tenantId: string,
|
||||
equipmentId: string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: equipmentKeys.detail(tenantId, equipmentId),
|
||||
queryFn: () => equipmentService.getEquipmentById(tenantId, equipmentId),
|
||||
enabled: !!tenantId && !!equipmentId && (options?.enabled ?? true),
|
||||
staleTime: 10 * 60 * 1000, // 10 minutes
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to create equipment
|
||||
*/
|
||||
export function useCreateEquipment(tenantId: string) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (equipmentData: Equipment) =>
|
||||
equipmentService.createEquipment(tenantId, equipmentData),
|
||||
onSuccess: (newEquipment) => {
|
||||
// Invalidate and refetch equipment lists
|
||||
queryClient.invalidateQueries({ queryKey: equipmentKeys.lists() });
|
||||
|
||||
// Add to cache
|
||||
queryClient.setQueryData(
|
||||
equipmentKeys.detail(tenantId, newEquipment.id),
|
||||
newEquipment
|
||||
);
|
||||
|
||||
toast.success('Equipment created successfully');
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error('Error creating equipment:', error);
|
||||
toast.error(error.response?.data?.detail || 'Error creating equipment');
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to update equipment
|
||||
*/
|
||||
export function useUpdateEquipment(tenantId: string) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ equipmentId, equipmentData }: {
|
||||
equipmentId: string;
|
||||
equipmentData: Partial<Equipment>;
|
||||
}) => equipmentService.updateEquipment(tenantId, equipmentId, equipmentData),
|
||||
onSuccess: (updatedEquipment, { equipmentId }) => {
|
||||
// Update cached data
|
||||
queryClient.setQueryData(
|
||||
equipmentKeys.detail(tenantId, equipmentId),
|
||||
updatedEquipment
|
||||
);
|
||||
|
||||
// Invalidate lists to refresh
|
||||
queryClient.invalidateQueries({ queryKey: equipmentKeys.lists() });
|
||||
|
||||
toast.success('Equipment updated successfully');
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error('Error updating equipment:', error);
|
||||
toast.error(error.response?.data?.detail || 'Error updating equipment');
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to delete equipment
|
||||
*/
|
||||
export function useDeleteEquipment(tenantId: string) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (equipmentId: string) =>
|
||||
equipmentService.deleteEquipment(tenantId, equipmentId),
|
||||
onSuccess: (_, equipmentId) => {
|
||||
// Remove from cache
|
||||
queryClient.removeQueries({
|
||||
queryKey: equipmentKeys.detail(tenantId, equipmentId)
|
||||
});
|
||||
|
||||
// Invalidate lists to refresh
|
||||
queryClient.invalidateQueries({ queryKey: equipmentKeys.lists() });
|
||||
|
||||
toast.success('Equipment deleted successfully');
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error('Error deleting equipment:', error);
|
||||
toast.error(error.response?.data?.detail || 'Error deleting equipment');
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user