All files / src/components/projects/tabs SpecificsTab.tsx

91.37% Statements 53/58
83.01% Branches 44/53
78.94% Functions 15/19
92.45% Lines 49/53

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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217                                      7x               7x               44x     44x 44x   44x 19x 19x     19x 19x 19x           19x 19x         44x 19x 16x 16x   3x   3x     2x 2x 2x 2x 2x   2x 1x 1x     1x     1x   3x 3x       44x 3x 3x 2x           44x 30x 28x 2x 2x 2x   4x       44x         44x       44x   44x         3x                                                         2x                                               2x                                                                        
import { IndianRupee, Info, MapPin } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import {
	type Locality,
	type MaterialTag,
	type Project,
	taxonomyApi,
} from "../../../lib/api";
import { ReadOnlyField } from "../../profile/ReadOnlyField";
import { SectionCard } from "../../profile/SectionCard";
import { AdditionalInfoModal } from "../modals/AdditionalInfoModal";
import { BudgetTimelineModal } from "../modals/BudgetTimelineModal";
import { LocationPropertyModal } from "../modals/LocationPropertyModal";
 
interface SpecificsTabProps {
	project: Project;
	onSave: (data: Partial<Project>) => void | Promise<void>;
}
 
const BUDGET_LABELS: Record<string, string> = {
	under_1l: "Under ₹1L",
	"1_3l": "₹1-3L",
	"3_5l": "₹3-5L",
	"5_10l": "₹5-10L",
	above_10l: "Above ₹10L",
};
 
const PROPERTY_TYPE_LABELS: Record<string, string> = {
	apartment: "Apartment",
	villa: "Villa",
	independent_house: "Independent House",
	commercial: "Commercial",
};
 
export function SpecificsTab({ project, onSave }: SpecificsTabProps) {
	const [editingSection, setEditingSection] = useState<
		"location" | "budget" | "additional" | null
	>(null);
	const [materialTags, setMaterialTags] = useState<MaterialTag[]>([]);
	const [localityName, setLocalityName] = useState<string | null>(null);
 
	useEffect(() => {
		let cancelled = false;
		taxonomyApi
			.getMaterialTags()
			.then((res) => {
				Eif (!cancelled) {
					const allTags = Object.values(res.data || {}).flat();
					setMaterialTags(allTags);
				}
			})
			.catch((err) => {
				console.error("Failed to load material tags:", err);
			});
		return () => {
			cancelled = true;
		};
	}, []);
 
	// Resolve locality name from ID
	useEffect(() => {
		if (!project.localityId) {
			setLocalityName(null);
			return;
		}
		let cancelled = false;
		// Fetch all cities and find the locality
		taxonomyApi
			.getCities()
			.then(async (citiesRes) => {
				for (const city of citiesRes.data || []) {
					Iif (cancelled) return;
					const localitiesRes = await taxonomyApi.getLocalitiesByCity(city.id);
					const match = (localitiesRes.data || []).find(
						(l: Locality) => l.id === project.localityId,
					);
					if (match) {
						Eif (!cancelled) setLocalityName(`${match.name}, ${city.name}`);
						return;
					}
				}
				Eif (!cancelled) setLocalityName(project.localityId); // fallback to ID
			})
			.catch(() => {
				Eif (!cancelled) setLocalityName(project.localityId);
			});
		return () => {
			cancelled = true;
		};
	}, [project.localityId]);
 
	const handleSave = async (data: Partial<Project>) => {
		try {
			await onSave(data);
			setEditingSection(null);
		} catch {
			// Error is handled by the parent component
		}
	};
 
	const materialTagNames = useMemo(() => {
		if (!project.materialTagIds || project.materialTagIds.length === 0)
			return null;
		const idToName: Record<string, string> = {};
		for (const tag of materialTags) {
			idToName[tag.id] = tag.name;
		}
		return project.materialTagIds.map((id) => idToName[id] || id);
	}, [project.materialTagIds, materialTags]);
 
	const isLocationEmpty =
		!project.localityId &&
		!project.propertyType &&
		!project.propertySize &&
		!project.projectAreaSqft;
	const isBudgetEmpty =
		!project.budgetRange &&
		!project.duration &&
		!project.yearCompleted &&
		(!project.materialTagIds || project.materialTagIds.length === 0);
	const isAdditionalEmpty = !project.clientTestimonial;
 
	return (
		<div className="space-y-6">
			<SectionCard
				title="Location & Property"
				icon={<MapPin className="h-4 w-4" />}
				onEdit={() => setEditingSection("location")}
				isEmpty={isLocationEmpty}
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField label="Location" value={localityName} />
					<ReadOnlyField
						label="Property Type"
						value={
							project.propertyType
								? PROPERTY_TYPE_LABELS[project.propertyType] ||
									project.propertyType
								: null
						}
					/>
					<ReadOnlyField label="Property Size" value={project.propertySize} />
					<ReadOnlyField
						label="Project Area"
						value={
							project.projectAreaSqft
								? `${project.projectAreaSqft} sq ft`
								: null
						}
					/>
				</dl>
			</SectionCard>
 
			<SectionCard
				title="Budget & Timeline"
				icon={<IndianRupee className="h-4 w-4" />}
				onEdit={() => setEditingSection("budget")}
				isEmpty={isBudgetEmpty}
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField
						label="Budget Range"
						value={
							project.budgetRange
								? BUDGET_LABELS[project.budgetRange] || project.budgetRange
								: null
						}
					/>
					<ReadOnlyField label="Duration" value={project.duration} />
					<ReadOnlyField
						label="Year Completed"
						value={project.yearCompleted ? String(project.yearCompleted) : null}
					/>
					<ReadOnlyField label="Material Tags" value={materialTagNames} />
				</dl>
			</SectionCard>
 
			<SectionCard
				title="Additional Info"
				icon={<Info className="h-4 w-4" />}
				onEdit={() => setEditingSection("additional")}
				isEmpty={isAdditionalEmpty}
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField
						label="Client Testimonial"
						value={project.clientTestimonial}
						className="sm:col-span-2"
					/>
				</dl>
			</SectionCard>
 
			{editingSection === "location" && (
				<LocationPropertyModal
					project={project}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
			{editingSection === "budget" && (
				<BudgetTimelineModal
					project={project}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
			{editingSection === "additional" && (
				<AdditionalInfoModal
					project={project}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
		</div>
	);
}