turash/bugulma/frontend/hooks/api/useProductsServicesAPI.ts

46 lines
1.4 KiB
TypeScript

import type { DiscoveryMatch } from '@/services/discovery-api';
import { useQuery } from '@tanstack/react-query';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api/v1';
/**
* Fetch products for an organization
*/
export function useOrganizationProducts(organizationId: string | undefined) {
return useQuery<DiscoveryMatch[]>({
queryKey: ['organization-products', organizationId],
queryFn: async () => {
if (!organizationId) return [];
const response = await fetch(`${API_BASE_URL}/organizations/${organizationId}/products`);
if (!response.ok) {
throw new Error('Failed to fetch organization products');
}
return response.json();
},
enabled: !!organizationId,
staleTime: 5 * 60 * 1000, // 5 minutes
});
}
/**
* Fetch services for an organization
*/
export function useOrganizationServices(organizationId: string | undefined) {
return useQuery<DiscoveryMatch[]>({
queryKey: ['organization-services', organizationId],
queryFn: async () => {
if (!organizationId) return [];
const response = await fetch(`${API_BASE_URL}/organizations/${organizationId}/services`);
if (!response.ok) {
throw new Error('Failed to fetch organization services');
}
return response.json();
},
enabled: !!organizationId,
staleTime: 5 * 60 * 1000, // 5 minutes
});
}