All files / src/components/profile/tabs ServicesTab.tsx

95.34% Statements 41/43
83.67% Branches 41/49
90% Functions 18/20
95% Lines 38/40

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                                        30x 30x         30x 39x     30x 22x 22x 38x 38x 16x     22x   30x 38x     30x 38x         30x       30x 2x     30x 5x     30x 2x     30x 11x     30x 2x       30x   30x 30x   30x 2x 2x 1x           30x           2x                               2x                                             2x                                                               1x                                
import { useState, useMemo } from "react";
import { Briefcase, FileText, Award } from "lucide-react";
import type {
	Pro,
	BusinessType,
	ServiceCategory,
	MaterialTag,
	Brand,
} from "../../../lib/api";
import { useUpdateProProfile } from "../../../hooks/mutations/useProMutations";
import { SectionCard } from "../SectionCard";
import { ReadOnlyField } from "../ReadOnlyField";
import { BusinessTypeModal } from "../modals/BusinessTypeModal";
import { ServicesModal } from "../modals/ServicesModal";
import { BrandsModal } from "../modals/BrandsModal";
 
interface ServicesTabProps {
	pro: Pro;
	proId: string;
	businessTypes: BusinessType[];
	serviceCategories: ServiceCategory[];
	materialTags: Record<string, MaterialTag[]>;
	brands: Record<string, Brand[]>;
	/** #362: hide Edit affordances for roles the backend will 403 on save. */
	canEdit?: boolean;
}
 
export function ServicesTab({
	pro,
	proId,
	businessTypes,
	serviceCategories,
	materialTags,
	brands,
	canEdit = true,
}: ServicesTabProps) {
	const updateProfile = useUpdateProProfile(proId);
	const [editingSection, setEditingSection] = useState<
		"businessType" | "services" | "brands" | null
	>(null);
 
	// Build O(1) lookup maps
	const businessTypeMap = useMemo(
		() => new Map(businessTypes.map((t) => [t.id, t.name])),
		[businessTypes],
	);
	const categoryMap = useMemo(() => {
		const map = new Map<string, string>();
		for (const cat of serviceCategories) {
			map.set(cat.id, cat.name);
			for (const child of cat.children || []) {
				map.set(child.id, child.name);
			}
		}
		return map;
	}, [serviceCategories]);
	const materialTagMap = useMemo(
		() => new Map(Object.values(materialTags).flat().map((t) => [t.id, t.name])),
		[materialTags],
	);
	const brandMap = useMemo(
		() => new Map(Object.values(brands).flat().map((b) => [b.id, b.name])),
		[brands],
	);
 
	// Look up display values using maps (O(1) per lookup)
	const primaryTypeName = pro.businessTypeId
		? businessTypeMap.get(pro.businessTypeId) ?? null
		: null;
 
	const secondaryTypeNames = (pro.businessTypesSecondary || [])
		.map((id) => businessTypeMap.get(id))
		.filter(Boolean) as string[];
 
	const serviceCategoryNames = (pro.serviceCategoryIds || [])
		.map((id) => categoryMap.get(id) ?? null)
		.filter(Boolean) as string[];
 
	const materialTagNames = (pro.materialTagIds || [])
		.map((id) => materialTagMap.get(id) ?? null)
		.filter(Boolean) as string[];
 
	const brandsWorkWithNames = (pro.brandsWorkWith || [])
		.map((id) => brandMap.get(id) ?? null)
		.filter(Boolean) as string[];
 
	const brandsOfficialPartnerNames = (pro.brandsOfficialPartner || [])
		.map((id) => brandMap.get(id) ?? null)
		.filter(Boolean) as string[];
 
	// isEmpty checks
	const isBusinessTypeEmpty = !pro.businessTypeId;
	const isServicesEmpty =
		!pro.serviceCategoryIds?.length && !pro.materialTagIds?.length;
	const isBrandsEmpty = !pro.brandsWorkWith?.length;
 
	const handleSave = async (data: Partial<Pro>) => {
		try {
			await updateProfile.mutateAsync(data);
			setEditingSection(null);
		} catch {
			// Error toast shown by mutation onError callback
		}
	};
 
	return (
		<div className="grid grid-cols-1 xl:grid-cols-2 gap-6">
			{/* Business Type */}
			<SectionCard
				title="What You Do"
				icon={<Briefcase className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("businessType") : undefined}
				isEmpty={isBusinessTypeEmpty}
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField label="Primary Profession" value={primaryTypeName} />
					<ReadOnlyField
						label="Also Do"
						value={secondaryTypeNames.length > 0 ? secondaryTypeNames : null}
					/>
				</dl>
			</SectionCard>
 
			{/* Brands */}
			<SectionCard
				title="Brands"
				icon={<Award className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("brands") : undefined}
				isEmpty={isBrandsEmpty}
			>
				<dl className="grid grid-cols-1 gap-4">
					<ReadOnlyField
						label="Brands We Work With"
						value={brandsWorkWithNames.length > 0 ? brandsWorkWithNames : null}
					/>
					<ReadOnlyField
						label="Official Brand Partners"
						value={
							brandsOfficialPartnerNames.length > 0
								? brandsOfficialPartnerNames
								: null
						}
					/>
				</dl>
			</SectionCard>
 
			{/* Services & Expertise */}
			<SectionCard
				title="Services & Expertise"
				icon={<FileText className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("services") : undefined}
				isEmpty={isServicesEmpty}
				className="xl:col-span-2"
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField
						label="Services Offered"
						value={serviceCategoryNames.length > 0 ? serviceCategoryNames : null}
					/>
					<ReadOnlyField
						label="Materials Used"
						value={materialTagNames.length > 0 ? materialTagNames : null}
					/>
				</dl>
			</SectionCard>
 
			{/* Modals */}
			{editingSection === "businessType" && (
				<BusinessTypeModal
					pro={pro}
					businessTypes={businessTypes}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
 
			{editingSection === "services" && (
				<ServicesModal
					pro={pro}
					serviceCategories={serviceCategories}
					materialTags={materialTags}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
 
			{editingSection === "brands" && (
				<BrandsModal
					pro={pro}
					brands={brands}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
 
		</div>
	);
}