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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 40x 40x 40x 40x 40x 40x 40x 40x 22x 40x 40x 3x 2x 1x 40x 40x 40x 7x 7x 7x 7x 7x 2x 2x 5x 1x 1x 4x 7x 40x 77x 18x 9x 9x 3x 1x 3x 3x 1x | import { X } from "lucide-react";
import { type FormEvent, useMemo, useState } from "react";
import { useDialogAccessibility, useUnsavedChanges } from "../../../hooks";
import type { CustomerSegment, Pro } from "../../../lib/api";
import { formatIndianNumber, parseIndianNumber } from "../../../lib/utils";
import { Button } from "../../ui/button";
import { ConfirmDialog } from "../../ui/confirm-dialog";
interface PricingModalProps {
pro: Pro;
customerSegments: CustomerSegment[];
onSave: (data: Partial<Pro>) => void;
onClose: () => void;
}
export function PricingModal({
pro,
customerSegments,
onSave,
onClose,
}: PricingModalProps) {
const pf = (pro.profileFields || {}) as Record<string, unknown>;
const [pricingModel, setPricingModel] = useState<string>(
(pf.pricingModel as string) || "",
);
const [customerSegmentId, setCustomerSegmentId] = useState(
pro.customerSegmentId || "",
);
const [customerSegmentsSecondary, setCustomerSegmentsSecondary] = useState<
string[]
>(pro.customerSegmentsSecondary || []);
const [priceRangeMin, setPriceRangeMin] = useState(
pro.priceRangeMin?.toString() || "",
);
const [priceRangeMax, setPriceRangeMax] = useState(
pro.priceRangeMax?.toString() || "",
);
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
// biome-ignore lint/correctness/useExhaustiveDependencies: pf is derived from pro
const initialValues = useMemo(
() => ({
pricingModel: (pf.pricingModel as string) || "",
customerSegmentId: pro.customerSegmentId || "",
customerSegmentsSecondary: pro.customerSegmentsSecondary || [],
priceRangeMin: pro.priceRangeMin?.toString() || "",
priceRangeMax: pro.priceRangeMax?.toString() || "",
}),
[pro],
);
const isDirty = useUnsavedChanges(initialValues, {
pricingModel,
customerSegmentId,
customerSegmentsSecondary,
priceRangeMin,
priceRangeMax,
});
const handleClose = () => {
if (isDirty) {
setShowDiscardConfirm(true);
} else {
onClose();
}
};
const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
const [validationError, setValidationError] = useState<string | null>(null);
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
setValidationError(null);
const minVal = priceRangeMin ? Number.parseInt(priceRangeMin, 10) : null;
const maxVal = priceRangeMax ? Number.parseInt(priceRangeMax, 10) : null;
if (
(minVal !== null && minVal > 1_000_000_000) ||
(maxVal !== null && maxVal > 1_000_000_000)
) {
setValidationError("Price cannot exceed \u20B9100 Cr");
return;
}
if (minVal !== null && maxVal !== null && minVal > maxVal) {
setValidationError("Minimum price cannot be greater than maximum price");
return;
}
const updatedProfileFields: Record<string, unknown> = {
...(pro.profileFields || {}),
pricingModel: pricingModel || null,
};
onSave({
customerSegmentId: customerSegmentId || null,
customerSegmentsSecondary:
customerSegmentsSecondary.length > 0 ? customerSegmentsSecondary : null,
priceRangeMin: priceRangeMin ? Number.parseInt(priceRangeMin, 10) : null,
priceRangeMax: priceRangeMax ? Number.parseInt(priceRangeMax, 10) : null,
profileFields: updatedProfileFields,
});
};
return (
<div
role="dialog"
className="fixed inset-0 flex items-center justify-center z-50"
onKeyDown={handleFocusTrap}
>
<button
type="button"
aria-label="Close modal"
className="fixed inset-0 bg-black/50 cursor-default"
onClick={handleClose}
tabIndex={-1}
/>
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby="pricing-modal-title"
tabIndex={-1}
className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 max-h-[90vh] overflow-y-auto focus:outline-none"
>
<div className="flex items-center justify-between p-4 border-b">
<h3 id="pricing-modal-title" className="font-semibold">
Edit Pricing
</h3>
<button
type="button"
onClick={handleClose}
aria-label="Close"
className="text-foreground-subtle hover:text-foreground-muted"
>
<X className="h-5 w-5" />
</button>
</div>
<form onSubmit={handleSubmit} className="p-4 space-y-4">
<div className="space-y-2">
<label
htmlFor="pm-pricing-model"
className="text-sm font-medium text-foreground-default"
>
How do you charge?
</label>
<select
id="pm-pricing-model"
value={pricingModel}
onChange={(e) => setPricingModel(e.target.value)}
className="flex h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="">Select pricing model</option>
<option value="per_sqft">Per Square Foot</option>
<option value="fixed_quote">Fixed Quote per Project</option>
<option value="cost_plus">Cost + Margin</option>
<option value="daily_rate">Daily/Hourly Rate</option>
<option value="package_based">Package Based</option>
</select>
</div>
<div className="space-y-2">
<label
htmlFor="pm-customer-segment"
className="text-sm font-medium text-foreground-default"
>
How expensive are you?
</label>
<p className="text-xs text-foreground-muted">
Be honest about your pricing tier. This helps match you with the
right customers.
</p>
<select
id="pm-customer-segment"
value={customerSegmentId}
onChange={(e) => setCustomerSegmentId(e.target.value)}
className="flex h-10 w-full rounded-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
>
<option value="">Select your pricing tier</option>
{customerSegments.map((seg) => (
<option key={seg.id} value={seg.id}>
{seg.name}
{seg.priceIndicator && ` (${seg.priceIndicator})`}
</option>
))}
</select>
</div>
{customerSegmentId && customerSegments.length > 1 && (
<div className="space-y-2">
<span className="text-sm font-medium text-foreground-default">
Also serve these customer types
</span>
<p className="text-xs text-foreground-muted">
Select any additional customer segments you serve.
</p>
<div className="flex flex-wrap gap-2">
{customerSegments
.filter((s) => s.id !== customerSegmentId)
.map((segment) => {
const isSelected = customerSegmentsSecondary.includes(
segment.id,
);
return (
<button
key={segment.id}
type="button"
onClick={() =>
setCustomerSegmentsSecondary(
isSelected
? customerSegmentsSecondary.filter(
(id) => id !== segment.id,
)
: [...customerSegmentsSecondary, segment.id],
)
}
className={`px-3 py-1.5 rounded-full border text-sm transition-all ${
isSelected
? "border-primary-500 bg-primary-50 text-primary-700"
: "border-border-default hover:border-primary-300"
}`}
>
{segment.name}
</button>
);
})}
</div>
</div>
)}
<div className="space-y-2">
<span className="text-sm font-medium text-foreground-default">
Typical Project Value Range
</span>
<div className="flex items-center gap-3">
<div className="flex flex-1">
<span className="inline-flex items-center px-3 rounded-l-md border border-r-0 border-border-default bg-background-muted text-foreground-subtle text-sm">
₹
</span>
<input
type="text"
value={priceRangeMin ? formatIndianNumber(priceRangeMin) : ""}
onChange={(e) =>
setPriceRangeMin(
parseIndianNumber(e.target.value).replace(/\D/g, ""),
)
}
placeholder="Min"
className="flex-1 h-10 rounded-r-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default placeholder:text-foreground-subtle focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
/>
</div>
<span className="text-sm text-foreground-subtle">to</span>
<div className="flex flex-1">
<span className="inline-flex items-center px-3 rounded-l-md border border-r-0 border-border-default bg-background-muted text-foreground-subtle text-sm">
₹
</span>
<input
type="text"
value={priceRangeMax ? formatIndianNumber(priceRangeMax) : ""}
onChange={(e) =>
setPriceRangeMax(
parseIndianNumber(e.target.value).replace(/\D/g, ""),
)
}
placeholder="Max"
className="flex-1 h-10 rounded-r-md border border-border-default bg-background-elevated px-3 py-2 text-sm text-foreground-default placeholder:text-foreground-subtle focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
/>
</div>
</div>
<p className="text-xs text-foreground-subtle">
e.g. ₹2,00,000 to ₹15,00,000
</p>
</div>
{validationError && (
<p className="text-sm text-error">{validationError}</p>
)}
<div className="flex justify-end gap-2 pt-4">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit">Save</Button>
</div>
</form>
</div>
<ConfirmDialog
open={showDiscardConfirm}
title="Discard unsaved changes?"
description="You have unsaved changes that will be lost if you close this form."
confirmLabel="Discard"
variant="destructive"
onConfirm={onClose}
onCancel={() => setShowDiscardConfirm(false)}
/>
</div>
);
}
|