All files / src/hooks useTaxonomyData.ts

100% Statements 43/43
72.22% Branches 13/18
100% Functions 8/8
100% Lines 43/43

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                          7x 7x 7x     7x     7x     7x 7x   7x 4x 4x 4x               4x                 3x 4x 4x 4x 4x 4x   1x   4x       4x     7x                         14x 14x   14x 5x 5x 1x 1x     4x 4x 4x 3x     5x 3x   5x   3x 3x   3x   1x   4x       5x     14x    
import { useState, useEffect } from "react";
import {
	taxonomyApi,
	type BusinessType,
	type CustomerSegment,
	type ServiceCategory,
	type MaterialTag,
	type Brand,
	type City,
	type Locality,
} from "../lib/api";
 
export function useTaxonomyData() {
	const [isLoading, setIsLoading] = useState(true);
	const [businessTypes, setBusinessTypes] = useState<BusinessType[]>([]);
	const [customerSegments, setCustomerSegments] = useState<CustomerSegment[]>(
		[],
	);
	const [serviceCategories, setServiceCategories] = useState<ServiceCategory[]>(
		[],
	);
	const [materialTags, setMaterialTags] = useState<
		Record<string, MaterialTag[]>
	>({});
	const [brands, setBrands] = useState<Record<string, Brand[]>>({});
	const [cities, setCities] = useState<City[]>([]);
 
	useEffect(() => {
		const loadTaxonomyData = async () => {
			try {
				setIsLoading(true);
				const [
					businessTypesRes,
					customerSegmentsRes,
					serviceCategoriesRes,
					materialTagsRes,
					brandsRes,
					citiesRes,
				] = await Promise.all([
					taxonomyApi.getBusinessTypes(),
					taxonomyApi.getCustomerSegments(),
					taxonomyApi.getServiceCategories(),
					taxonomyApi.getMaterialTags(),
					taxonomyApi.getBrands(),
					taxonomyApi.getCities(),
				]);
 
				setBusinessTypes(businessTypesRes.data || []);
				setCustomerSegments(customerSegmentsRes.data || []);
				setServiceCategories(serviceCategoriesRes.data || []);
				setMaterialTags(materialTagsRes.data || {});
				setBrands(brandsRes.data || {});
				setCities(citiesRes.data || []);
			} catch (err) {
				console.error("Failed to load taxonomy data:", err);
			} finally {
				setIsLoading(false);
			}
		};
 
		loadTaxonomyData();
	}, []);
 
	return {
		isLoading,
		businessTypes,
		customerSegments,
		serviceCategories,
		materialTags,
		brands,
		cities,
	};
}
 
// Hook for loading localities based on city
export function useLocalities(cityId: string) {
	const [localities, setLocalities] = useState<Locality[]>([]);
	const [isLoading, setIsLoading] = useState(false);
 
	useEffect(() => {
		const loadLocalities = async () => {
			if (!cityId) {
				setLocalities([]);
				return;
			}
 
			try {
				setIsLoading(true);
				const zonesRes = await taxonomyApi.getZones(cityId);
				const zones = zonesRes.data || [];
 
				// Fetch localities for all zones
				const localitiesPromises = zones.map((zone) =>
					taxonomyApi.getLocalities(zone.id),
				);
				const localitiesResults = await Promise.all(localitiesPromises);
 
				const allLocalities = localitiesResults.flatMap(
					(res) => res.data || [],
				);
				setLocalities(allLocalities);
			} catch (err) {
				console.error("Failed to load localities:", err);
			} finally {
				setIsLoading(false);
			}
		};
 
		loadLocalities();
	}, [cityId]);
 
	return { localities, isLoading };
}