All files / src/hooks/queries useTaxonomyQueries.ts

100% Statements 38/38
100% Branches 32/32
100% Functions 21/21
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158          14x     178x     84x 3x               178x     84x 3x               484x     146x 3x               178x     84x 3x               178x     84x 3x               4x     2x 2x               178x     84x 3x               5x     2x 2x                 5x     2x 2x                 177x     67x     2x                   174x 174x 174x 174x 174x 174x   174x                                
import { useQuery } from "@tanstack/react-query";
import { taxonomyApi } from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
 
// Taxonomy data changes rarely, so use longer cache times
const TAXONOMY_STALE_TIME = 1000 * 60 * 60; // 1 hour
 
export function useBusinessTypes() {
	return useQuery({
		queryKey: queryKeys.taxonomy.businessTypes(),
		queryFn: async () => {
			const response = await taxonomyApi.getBusinessTypes();
			return response.data || [];
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false, // Taxonomy rarely changes
	});
}
 
export function useCustomerSegments() {
	return useQuery({
		queryKey: queryKeys.taxonomy.customerSegments(),
		queryFn: async () => {
			const response = await taxonomyApi.getCustomerSegments();
			return response.data || [];
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useServiceCategories() {
	return useQuery({
		queryKey: queryKeys.taxonomy.serviceCategories(),
		queryFn: async () => {
			const response = await taxonomyApi.getServiceCategories();
			return response.data || [];
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useMaterialTags() {
	return useQuery({
		queryKey: queryKeys.taxonomy.materialTags(),
		queryFn: async () => {
			const response = await taxonomyApi.getMaterialTags();
			return response.data || {};
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useBrands() {
	return useQuery({
		queryKey: queryKeys.taxonomy.brands(),
		queryFn: async () => {
			const response = await taxonomyApi.getBrands();
			return response.data || {};
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useRoomTypes() {
	return useQuery({
		queryKey: queryKeys.taxonomy.roomTypes(),
		queryFn: async () => {
			const response = await taxonomyApi.getRoomTypes();
			return response.data || [];
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useCities() {
	return useQuery({
		queryKey: queryKeys.taxonomy.cities(),
		queryFn: async () => {
			const response = await taxonomyApi.getCities();
			return response.data || [];
		},
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useZones(cityId: string | null) {
	return useQuery({
		queryKey: queryKeys.taxonomy.zones(cityId ?? ""),
		queryFn: async () => {
			const response = await taxonomyApi.getZones(cityId as string);
			return response.data || [];
		},
		enabled: !!cityId,
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useLocalities(zoneId: string | null) {
	return useQuery({
		queryKey: queryKeys.taxonomy.localities(zoneId ?? ""),
		queryFn: async () => {
			const response = await taxonomyApi.getLocalities(zoneId as string);
			return response.data || [];
		},
		enabled: !!zoneId,
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
export function useLocalitiesByCity(cityId: string | null) {
	return useQuery({
		queryKey: queryKeys.taxonomy.localitiesByCity(cityId ?? ""),
		queryFn: async () => {
			const response = await taxonomyApi.getLocalitiesByCity(
				cityId as string,
			);
			return response.data || [];
		},
		enabled: !!cityId,
		staleTime: TAXONOMY_STALE_TIME,
		refetchOnWindowFocus: false,
	});
}
 
// Composite hook maintains same API as existing useTaxonomyData
export function useTaxonomyData() {
	const businessTypes = useBusinessTypes();
	const customerSegments = useCustomerSegments();
	const serviceCategories = useServiceCategories();
	const materialTags = useMaterialTags();
	const brands = useBrands();
	const cities = useCities();
 
	return {
		isLoading:
			businessTypes.isLoading ||
			customerSegments.isLoading ||
			serviceCategories.isLoading ||
			materialTags.isLoading ||
			brands.isLoading ||
			cities.isLoading,
		businessTypes: businessTypes.data,
		customerSegments: customerSegments.data,
		serviceCategories: serviceCategories.data,
		materialTags: materialTags.data,
		brands: brands.data,
		cities: cities.data,
	};
}