All files / src/components/crm LeadFinancials.tsx

100% Statements 28/28
100% Branches 28/28
100% Functions 10/10
100% Lines 26/26

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                                  20x   20x 2x     2x         20x   2x           20x       20x                 2x                                                                                                 35x 35x   35x 7x   7x         35x 3x 3x 2x   3x     35x 2x 2x     35x                 3x   3x 3x                                                                                      
import { useState, useCallback } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { IndianRupee, Pencil, Check, X } from "lucide-react";
import { crmApi } from "../../lib/api";
import type { Lead } from "../../lib/api/pro/crm";
import { queryKeys } from "../../lib/query-keys";
import { formatPaise, parseCurrencyInput } from "../../lib/currency-utils";
import { Trophy } from "lucide-react";
 
// ─── Component ───────────────────────────────────────────────────────────────
 
type LeadFinancialsProps = {
	lead: Lead;
	proId: string;
};
 
export function LeadFinancials({ lead, proId }: LeadFinancialsProps) {
	const queryClient = useQueryClient();
 
	const invalidate = useCallback(() => {
		queryClient.invalidateQueries({
			queryKey: queryKeys.crm.lead(proId, String(lead.id)),
		});
		queryClient.invalidateQueries({
			queryKey: queryKeys.crm.kanban(proId),
		});
	}, [queryClient, proId, lead.id]);
 
	const quoteValueMutation = useMutation({
		mutationFn: async (valuePaise: number) =>
			crmApi.updateQuoteValue(proId, lead.id, valuePaise),
		onSuccess: invalidate,
	});
 
	// Calculate delta if both values exist
	const delta =
		lead.quoteValuePaise != null && lead.orderValuePaise != null
			? lead.orderValuePaise - lead.quoteValuePaise
			: null;
 
	return (
		<div className="rounded-lg border border-border-default bg-background-elevated p-5">
			<h3 className="text-sm font-semibold text-foreground-default mb-4">
				Financials
			</h3>
			<div className="space-y-4">
				<ValueField
					label="Quote Value"
					valuePaise={lead.quoteValuePaise}
					onSave={(paise) => quoteValueMutation.mutate(paise)}
					isSaving={quoteValueMutation.isPending}
				/>
				<div className="flex items-center justify-between">
					<span className="text-sm text-foreground-muted">Order Value</span>
					{lead.orderValuePaise != null ? (
						<span className="text-sm font-semibold text-foreground-default">
							₹{formatPaise(lead.orderValuePaise)}
						</span>
					) : (
						<span className="text-sm text-foreground-subtle italic flex items-center gap-1">
							<Trophy className="h-3 w-3" />
							Set when marking Won
						</span>
					)}
				</div>
 
				{/* Delta display */}
				{delta !== null && (
					<div className="flex items-center justify-between pt-2 border-t border-border-default">
						<span className="text-sm text-foreground-muted">Difference</span>
						<span
							className={`text-sm font-medium ${
								delta > 0
									? "text-success"
									: delta < 0
										? "text-error"
										: "text-foreground-muted"
							}`}
						>
							{delta > 0 ? "+" : ""}₹{formatPaise(Math.abs(delta))}
						</span>
					</div>
				)}
			</div>
		</div>
	);
}
 
// ─── Inline editable value field ─────────────────────────────────────────────
 
type ValueFieldProps = {
	label: string;
	valuePaise: number | null;
	onSave: (paise: number) => void;
	isSaving: boolean;
};
 
function ValueField({ label, valuePaise, onSave, isSaving }: ValueFieldProps) {
	const [isEditing, setIsEditing] = useState(false);
	const [input, setInput] = useState("");
 
	const startEdit = useCallback(() => {
		setIsEditing(true);
		// Pre-fill with current value in rupees
		setInput(
			valuePaise != null ? String(Math.round(valuePaise / 100)) : "",
		);
	}, [valuePaise]);
 
	const handleSave = useCallback(() => {
		const paise = parseCurrencyInput(input);
		if (paise !== null && paise >= 0) {
			onSave(paise);
		}
		setIsEditing(false);
	}, [input, onSave]);
 
	const handleCancel = useCallback(() => {
		setIsEditing(false);
		setInput("");
	}, []);
 
	return (
		<div className="flex items-center justify-between">
			<span className="text-sm text-foreground-muted">{label}</span>
			{isEditing ? (
				<div className="flex items-center gap-1">
					<span className="text-sm text-foreground-muted">₹</span>
					<input
						type="text"
						value={input}
						onChange={(e) => setInput(e.target.value)}
						onKeyDown={(e) => {
							if (e.key === "Enter") handleSave();
							if (e.key === "Escape") handleCancel();
						}}
						placeholder="e.g., 4.2L or 420000"
						className="h-7 w-32 px-2 text-sm rounded border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500"
					/>
					<button
						type="button"
						onClick={handleSave}
						className="p-0.5 text-success hover:text-success/80"
					>
						<Check className="h-3.5 w-3.5" />
					</button>
					<button
						type="button"
						onClick={handleCancel}
						className="p-0.5 text-foreground-subtle hover:text-foreground-muted"
					>
						<X className="h-3.5 w-3.5" />
					</button>
				</div>
			) : (
				<button
					type="button"
					onClick={startEdit}
					disabled={isSaving}
					className="group flex items-center gap-1 text-sm"
				>
					{valuePaise != null ? (
						<span className="font-semibold text-foreground-default">
							₹{formatPaise(valuePaise)}
						</span>
					) : (
						<span className="text-foreground-subtle italic flex items-center gap-1">
							<IndianRupee className="h-3 w-3" />
							Add {label.toLowerCase()}
						</span>
					)}
					<Pencil className="h-3 w-3 text-foreground-subtle opacity-0 group-hover:opacity-100 transition-opacity" />
				</button>
			)}
		</div>
	);
}