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

98.11% Statements 52/53
95.65% Branches 88/92
92.85% Functions 13/14
97.87% Lines 46/47

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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                      58x 58x       58x     58x 58x 4x 4x               58x 58x 58x 1x       58x               58x               58x               58x             58x                 58x 4x   58x       58x 4x       58x                           58x           58x 58x   2x 2x 4x 2x       58x             58x   58x 58x 58x 58x     58x           58x 2x 2x 1x           58x           4x                                             2x                                       3x                                                         1x                                   1x            
import { Clock, DollarSign, Wrench } from "lucide-react";
import { useState } from "react";
import { useUpdateProProfile } from "../../../hooks/mutations/useProMutations";
import type { CustomerSegment, Pro } from "../../../lib/api";
import type { ServiceCategory } from "../../../lib/api/taxonomy";
import { HowWeWorkModal } from "../modals/HowWeWorkModal";
import { PricingModal } from "../modals/PricingModal";
import { TimelinesExtrasModal } from "../modals/TimelinesExtrasModal";
import { ReadOnlyField } from "../ReadOnlyField";
import { SectionCard } from "../SectionCard";
 
interface DeliveryPricingTabProps {
	pro: Pro;
	proId: string;
	customerSegments: CustomerSegment[];
	serviceCategories: ServiceCategory[];
	/** #362: hide Edit affordances for roles the backend will 403 on save. */
	canEdit?: boolean;
}
 
export function DeliveryPricingTab({
	pro,
	proId,
	customerSegments,
	serviceCategories,
	canEdit = true,
}: DeliveryPricingTabProps) {
	const updateProfile = useUpdateProProfile(proId);
	const [editingSection, setEditingSection] = useState<
		"howWeWork" | "pricing" | "timelinesExtras" | null
	>(null);
 
	const pf = (pro.profileFields || {}) as Record<string, unknown>;
 
	// --- How We Work display values ---
	const deliveryMethods: string[] = [];
	if (pf.factoryMade) {
		const ft = pf.factoryType as string | undefined;
		deliveryMethods.push(
			ft === "own"
				? "Factory made (Own Factory)"
				: ft === "partner"
					? "Factory made (Partner Factory)"
					: "Factory made",
		);
	}
	if (pf.siteExecution) deliveryMethods.push("Site execution");
	if (pf.labourOnly) deliveryMethods.push("Labour only projects");
	if (pf.fullSiteExecution)
		deliveryMethods.push("Full site execution (design + material + execution)");
 
	const materialApproachLabel =
		(
			{
				we_provide: "We provide all materials",
				customer_provides: "Customer provides all materials",
				combination: "Combination of both",
			} as Record<string, string>
		)[pf.materialApproach as string] || null;
 
	const deliveryTimelineDisplay =
		pf.deliveryDaysMin != null
			? pf.deliveryDaysMax != null && pf.deliveryDaysMax !== pf.deliveryDaysMin
				? `${pf.deliveryDaysMin} – ${pf.deliveryDaysMax} days`
				: `${pf.deliveryDaysMin} days`
			: null;
 
	const consultationFeeLabel =
		(
			{
				free: "Free",
				paid: "Paid",
				adjustable: "Adjustable against order",
			} as Record<string, string>
		)[pf.consultationFee as string] || null;
 
	const isHowWeWorkEmpty =
		deliveryMethods.length === 0 &&
		!materialApproachLabel &&
		!consultationFeeLabel;
 
	// --- Pricing display values ---
	const pricingModelLabel =
		(
			{
				per_sqft: "Per Square Foot",
				fixed_quote: "Fixed Quote per Project",
				cost_plus: "Cost + Margin",
				daily_rate: "Daily/Hourly Rate",
				package_based: "Package Based",
			} as Record<string, string>
		)[pf.pricingModel as string] || null;
 
	const primarySegment = pro.customerSegmentId
		? customerSegments.find((s) => s.id === pro.customerSegmentId)
		: null;
	const primarySegmentLabel = primarySegment
		? `${primarySegment.name}${primarySegment.priceIndicator ? ` (${primarySegment.priceIndicator})` : ""}`
		: null;
 
	const secondarySegmentLabels = (pro.customerSegmentsSecondary || [])
		.map((id) => customerSegments.find((s) => s.id === id)?.name)
		.filter(Boolean) as string[];
 
	const priceRangeDisplay =
		pro.priceRangeMin != null || pro.priceRangeMax != null
			? [
					pro.priceRangeMin != null
						? `₹${pro.priceRangeMin.toLocaleString("en-IN")}`
						: null,
					pro.priceRangeMax != null
						? `₹${pro.priceRangeMax.toLocaleString("en-IN")}`
						: null,
				]
					.filter(Boolean)
					.join(" – ")
			: null;
 
	const isPricingEmpty =
		!pricingModelLabel &&
		!primarySegmentLabel &&
		!priceRangeDisplay;
 
	// --- Timelines & Extras display values ---
	const typicalTimelines =
		(pf.typicalTimelines as Record<string, string>) || {};
	const timelineEntries = Object.entries(typicalTimelines)
		.map(([catId, value]) => {
			const cat = serviceCategories
				.flatMap((c) => [c, ...(c.children || [])])
				.find((c) => c.id === catId);
			return cat ? `${cat.name}: ${value}` : null;
		})
		.filter(Boolean) as string[];
 
	const rushOrderDisplay = pro.acceptsRushOrders
		? pro.rushOrderPremium
			? `Yes (${pro.rushOrderPremium.replace("_percent", "%").replace("case_by_case", "Case by case")})`
			: "Yes"
		: "No";
 
	const warrantyDisplay =
		pf.warrantyYears != null ? `${pf.warrantyYears} years` : null;
 
	const extrasBadges: string[] = [];
	if (pf.hasAfterSalesService) extrasBadges.push("After-sales service");
	if (pf.hasShowroom) extrasBadges.push("Showroom / experience center");
	if (pf.provides3DDesign) extrasBadges.push("3D designs / renders");
 
	const isTimelinesExtrasEmpty =
		!deliveryTimelineDisplay &&
		timelineEntries.length === 0 &&
		!pro.acceptsRushOrders &&
		!warrantyDisplay &&
		extrasBadges.length === 0;
 
	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">
			{/* How We Work */}
			<SectionCard
				title="How We Work"
				icon={<Wrench className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("howWeWork") : undefined}
				isEmpty={isHowWeWorkEmpty}
			>
				<dl className="grid grid-cols-1 gap-4">
					<ReadOnlyField
						label="Delivery Methods"
						value={deliveryMethods.length > 0 ? deliveryMethods : null}
					/>
					<ReadOnlyField
						label="Material Approach"
						value={materialApproachLabel}
					/>
					<ReadOnlyField
						label="Consultation Fee"
						value={consultationFeeLabel}
					/>
				</dl>
			</SectionCard>
 
			{/* Pricing */}
			<SectionCard
				title="Pricing"
				icon={<DollarSign className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("pricing") : undefined}
				isEmpty={isPricingEmpty}
			>
				<dl className="grid grid-cols-1 gap-4">
					<ReadOnlyField label="Pricing Model" value={pricingModelLabel} />
					<ReadOnlyField label="Pricing Tier" value={primarySegmentLabel} />
					<ReadOnlyField
						label="Also Serves"
						value={
							secondarySegmentLabels.length > 0 ? secondarySegmentLabels : null
						}
					/>
					<ReadOnlyField label="Typical Project Value Range" value={priceRangeDisplay} />
				</dl>
			</SectionCard>
 
			{/* Timelines & Extras */}
			<SectionCard
				title="Timelines & Extras"
				icon={<Clock className="h-5 w-5" />}
				onEdit={canEdit ? () => setEditingSection("timelinesExtras") : undefined}
				isEmpty={isTimelinesExtrasEmpty}
				className="xl:col-span-2"
			>
				<dl className="grid grid-cols-1 sm:grid-cols-2 gap-4">
					<ReadOnlyField
						label="Delivery Timeline"
						value={deliveryTimelineDisplay}
					/>
					<ReadOnlyField
						label="Typical Timelines"
						value={timelineEntries.length > 0 ? timelineEntries : null}
						className="sm:col-span-2"
					/>
					<ReadOnlyField label="Rush Orders" value={rushOrderDisplay} />
					<ReadOnlyField label="Warranty Period" value={warrantyDisplay} />
					<ReadOnlyField
						label="Extras"
						value={extrasBadges.length > 0 ? extrasBadges : null}
						className="sm:col-span-2"
					/>
				</dl>
			</SectionCard>
 
			{/* Modals */}
			{editingSection === "howWeWork" && (
				<HowWeWorkModal
					pro={pro}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
 
			{editingSection === "pricing" && (
				<PricingModal
					pro={pro}
					customerSegments={customerSegments}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
 
			{editingSection === "timelinesExtras" && (
				<TimelinesExtrasModal
					pro={pro}
					serviceCategories={serviceCategories}
					onSave={handleSave}
					onClose={() => setEditingSection(null)}
				/>
			)}
		</div>
	);
}