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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | 123x 123x 123x 123x 123x 123x 123x 123x 123x 123x 123x 123x 43x 123x 123x 5x 2x 3x 123x 123x 7x 7x 7x 2x 123x 4x 3x 1x 1x 1x 123x 11x 11x 1x 1x 1x 1x 10x 10x 5x 5x 5x 5x 123x 2x 2x 1x 123x 3x 3x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 123x 4x 4x 1x 1x 1x 1x 3x 3x 2x 1x 1x 1x 123x 123x 13x 13x 13x 10x 10x 8x 3x 13x 4x 4x 3x 9x 13x 11x 11x 9x 2x 1x 1x 13x 5x 5x 5x 8x 13x 2x 13x 3x 13x 1x 13x 4x 13x 13x 6x 13x 123x 7x 245x 2x 94x 9x 3x 2x 10x 4x 11x 11x 11x 1x | import { useState, useMemo, type FormEvent } from "react";
import { X } from "lucide-react";
import { Button } from "../../ui/button";
import { Input } from "../../ui/input";
import { MultiSelect } from "../../ui/multi-select";
import { ConfirmDialog } from "../../ui/confirm-dialog";
import { useUnsavedChanges, useDialogAccessibility } from "../../../hooks";
import type { Pro, City, Locality } from "../../../lib/api";
import {
addressLineErrorMessage,
cityNameErrorMessage,
indianStateErrorMessage,
isValidIndianPincode,
normalizeIndianState,
pincodeErrorMessage,
validateAddressLine,
validateCityName,
validateIndianState,
} from "@interioring/utils/validation/address";
interface LocationModalProps {
pro: Pro;
cities: City[];
localities: Locality[];
onCityChange: (cityId: string) => void;
onSave: (data: {
cityId: string | null;
serviceAreaIds: string[] | null;
addressLine1: string | null;
addressLine2: string | null;
addressCity: string | null;
addressState: string | null;
addressPincode: string | null;
}) => void;
onClose: () => void;
}
export function LocationModal({
pro,
cities,
localities,
onCityChange,
onSave,
onClose,
}: LocationModalProps) {
const derivedCityId = pro.cityId
? pro.cityId
: pro.addressCity
? "other"
: "";
const [cityId, setCityId] = useState(derivedCityId);
const [serviceAreaIds, setServiceAreaIds] = useState<string[]>(
pro.serviceAreaIds || [],
);
const [addressLine1, setAddressLine1] = useState(pro.addressLine1 || "");
const [addressLine2, setAddressLine2] = useState(pro.addressLine2 || "");
const [addressCity, setAddressCity] = useState(pro.addressCity || "");
const [addressState, setAddressState] = useState(pro.addressState || "");
const [addressPincode, setAddressPincode] = useState(
pro.addressPincode || "",
);
const [errors, setErrors] = useState<Record<string, string>>({});
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
const isOtherCity = cityId === "other";
const initialValues = useMemo(
() => ({
cityId: derivedCityId,
serviceAreaIds: pro.serviceAreaIds || [],
addressLine1: pro.addressLine1 || "",
addressLine2: pro.addressLine2 || "",
addressCity: pro.addressCity || "",
addressState: pro.addressState || "",
addressPincode: pro.addressPincode || "",
}),
[pro, derivedCityId],
);
const isDirty = useUnsavedChanges(initialValues, {
cityId,
serviceAreaIds,
addressLine1,
addressLine2,
addressCity,
addressState,
addressPincode,
});
const handleClose = () => {
if (isDirty) {
setShowDiscardConfirm(true);
} else {
onClose();
}
};
const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
const handleCityChange = (newCityId: string) => {
setCityId(newCityId);
setServiceAreaIds([]);
if (newCityId !== "other" && newCityId !== "") {
onCityChange(newCityId);
}
};
// Issue #563: pincode must be 6 digits and not start with 0 (real Indian
// PIN codes have a 1-9 first digit). Shared validator keeps the rule in
// sync with the API's updateProSchema.
const validatePincode = () => {
if (addressPincode && !isValidIndianPincode(addressPincode)) {
setErrors((prev) => ({
...prev,
addressPincode: pincodeErrorMessage("invalid_format"),
}));
} else {
setErrors((prev) => {
const { addressPincode: _, ...rest } = prev;
return rest;
});
}
};
// #426: re-validate State on every keystroke so a sticky error from a
// prior failed submit clears as soon as the user corrects the input.
// Without this, `hasErrors` stayed true after correction and the Save
// button remained disabled — forcing users to close the modal and lose
// all changes. Pass the new value explicitly because `setAddressState`
// is async and the closure-captured `addressState` is still the old one.
//
// Issue #563: state must match the canonical Indian-state list (the old
// regex accepted lowercase "telangana" because it only checked letters).
const validateAddressStateInput = (value: string) => {
const trimmed = value.trim();
if (!trimmed) {
setErrors((prev) => {
const { addressState: _, ...rest } = prev;
return rest;
});
return;
}
const error = validateIndianState(trimmed);
if (error) {
setErrors((prev) => ({
...prev,
addressState: indianStateErrorMessage(error),
}));
} else {
setErrors((prev) => {
const { addressState: _, ...rest } = prev;
return rest;
});
}
};
// On blur, normalize to canonical casing ("telangana" → "Telangana") so the
// stored value matches the marketplace JSON-LD. Skipped while typing to
// avoid fighting the user's caret.
const handleAddressStateBlur = () => {
const canonical = normalizeIndianState(addressState);
if (canonical && canonical !== addressState) {
setAddressState(canonical);
}
};
const validateAddressLineInput = (
field: "addressLine1" | "addressLine2",
value: string,
) => {
const trimmed = value.trim();
if (!trimmed) {
setErrors((prev) => {
const { [field]: _, ...rest } = prev;
return rest;
});
return;
}
const error = validateAddressLine(trimmed);
if (error) {
setErrors((prev) => ({
...prev,
[field]: addressLineErrorMessage(error),
}));
} else {
setErrors((prev) => {
const { [field]: _, ...rest } = prev;
return rest;
});
}
};
const validateAddressCityInput = (value: string) => {
const trimmed = value.trim();
if (!trimmed) {
setErrors((prev) => {
const { addressCity: _, ...rest } = prev;
return rest;
});
return;
}
const error = validateCityName(trimmed);
if (error) {
setErrors((prev) => ({
...prev,
addressCity: cityNameErrorMessage(error),
}));
} else {
setErrors((prev) => {
const { addressCity: _, ...rest } = prev;
return rest;
});
}
};
const hasErrors = Object.keys(errors).length > 0;
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
const newErrors: Record<string, string> = { ...errors };
// Run shared validators on every field; the shared module is the
// single source of truth — same code runs server-side in
// updateProSchema so client + API stay in sync (issue #563).
if (addressLine1.trim()) {
const err = validateAddressLine(addressLine1);
if (err) newErrors.addressLine1 = addressLineErrorMessage(err);
else delete newErrors.addressLine1;
} else {
delete newErrors.addressLine1;
}
if (addressLine2.trim()) {
const err = validateAddressLine(addressLine2);
if (err) newErrors.addressLine2 = addressLineErrorMessage(err);
else delete newErrors.addressLine2;
} else {
delete newErrors.addressLine2;
}
// Always validate addressCity (not just for "Other" — the field is
// rendered for every flow and persisted into JSON-LD).
if (addressCity.trim()) {
const err = validateCityName(addressCity);
if (err) newErrors.addressCity = cityNameErrorMessage(err);
else delete newErrors.addressCity;
} else if (isOtherCity) {
newErrors.addressCity = "City name is required";
} else {
delete newErrors.addressCity;
}
if (addressState.trim()) {
const err = validateIndianState(addressState);
Iif (err) newErrors.addressState = indianStateErrorMessage(err);
else delete newErrors.addressState;
} else {
delete newErrors.addressState;
}
if (addressPincode && !isValidIndianPincode(addressPincode)) {
newErrors.addressPincode = pincodeErrorMessage("invalid_format");
}
// #616: profile-completeness gates the "Add business address" tile on
// addressLine1 + addressCity + addressPincode all being non-empty. The
// modal used to accept empty values silently, leaving users with an
// incomplete dashboard and no signal as to why. Surface required-field
// errors here. Format errors above already populated newErrors[field]
// for malformed values; guard against clobbering them so the more
// specific message wins.
if (!addressLine1.trim() && !newErrors.addressLine1) {
newErrors.addressLine1 = "Business address is required to complete your profile";
}
if (!addressCity.trim() && !newErrors.addressCity) {
newErrors.addressCity = "City is required to complete your profile";
}
if (!addressPincode.trim() && !newErrors.addressPincode) {
newErrors.addressPincode = "Pincode is required to complete your profile";
}
setErrors(newErrors);
if (Object.keys(newErrors).length > 0) return;
// Normalize state to canonical casing on save so "telangana" persists
// as "Telangana" — same rule the API applies via addressStateSchema.
const canonicalState = addressState.trim()
? (normalizeIndianState(addressState) ?? addressState)
: "";
onSave({
cityId: isOtherCity ? null : cityId || null,
serviceAreaIds:
isOtherCity || serviceAreaIds.length === 0 ? null : serviceAreaIds,
addressLine1: addressLine1 || null,
addressLine2: addressLine2 || null,
addressCity: addressCity || null,
addressState: canonicalState || null,
addressPincode: addressPincode || null,
});
};
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="location-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="location-modal-title" className="font-semibold">
Edit Location
</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="loc-city"
className="text-sm font-medium text-foreground-default"
>
City
</label>
<select
id="loc-city"
value={cityId}
onChange={(e) => handleCityChange(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 a city</option>
{cities.map((c) => (
<option key={c.id} value={c.id}>
{c.name}
</option>
))}
<option value="other">Other</option>
</select>
</div>
{isOtherCity && (
<div className="space-y-2">
<div className="flex items-center gap-1">
<label
htmlFor="loc-other-city"
className="text-sm font-medium text-foreground-default"
>
City Name
</label>
<span aria-hidden="true" className="text-error text-base font-bold">*</span>
</div>
<Input
id="loc-other-city"
value={addressCity}
onChange={(e) => setAddressCity(e.target.value)}
placeholder="Enter your city name"
/>
{errors.addressCity && (
<p className="mt-1 text-xs text-error">{errors.addressCity}</p>
)}
</div>
)}
{!isOtherCity && cityId && (
<div className="space-y-2">
<label
htmlFor="loc-service-areas"
id="loc-service-areas-label"
className="text-sm font-medium text-foreground-default"
>
Service Areas / Localities
</label>
<MultiSelect
id="loc-service-areas"
aria-labelledby="loc-service-areas-label"
options={localities.map((l) => ({
value: l.id,
label: l.name,
}))}
selected={serviceAreaIds}
onChange={setServiceAreaIds}
placeholder="Select service areas"
/>
</div>
)}
<div className="border-t border-border-default pt-4 space-y-4">
<div>
<p className="text-sm font-medium text-foreground-default">
Business Address
</p>
<p className="text-xs text-foreground-muted mt-1">
Required to complete your business profile.
</p>
</div>
<div className="space-y-2">
<div className="flex items-center gap-1">
<label
htmlFor="loc-address1"
className="text-sm font-medium text-foreground-default"
>
Address Line 1
</label>
<span aria-hidden="true" className="text-error text-base font-bold">*</span>
</div>
<Input
id="loc-address1"
value={addressLine1}
onChange={(e) => setAddressLine1(e.target.value)}
onBlur={(e) =>
validateAddressLineInput("addressLine1", e.target.value)
}
placeholder="Street address, shop/office number"
/>
{errors.addressLine1 && (
<p className="mt-1 text-xs text-error">{errors.addressLine1}</p>
)}
</div>
<div className="space-y-2">
<label
htmlFor="loc-address2"
className="text-sm font-medium text-foreground-default"
>
Address Line 2
</label>
<Input
id="loc-address2"
value={addressLine2}
onChange={(e) => setAddressLine2(e.target.value)}
onBlur={(e) =>
validateAddressLineInput("addressLine2", e.target.value)
}
placeholder="Area, landmark"
/>
{errors.addressLine2 && (
<p className="mt-1 text-xs text-error">{errors.addressLine2}</p>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className="space-y-2">
<div className="flex items-center gap-1">
<label
htmlFor="loc-address-city"
className="text-sm font-medium text-foreground-default"
>
City
</label>
<span aria-hidden="true" className="text-error text-base font-bold">*</span>
</div>
<Input
id="loc-address-city"
value={addressCity}
onChange={(e) => setAddressCity(e.target.value)}
onBlur={(e) => validateAddressCityInput(e.target.value)}
placeholder="City"
/>
{/* When isOtherCity, the dedicated "City Name" input
above already shows the same error — avoid the
duplicate by gating this render on !isOtherCity. */}
{!isOtherCity && errors.addressCity && (
<p className="mt-1 text-xs text-error">{errors.addressCity}</p>
)}
</div>
<div className="space-y-2">
<label
htmlFor="loc-address-state"
className="text-sm font-medium text-foreground-default"
>
State
</label>
<Input
id="loc-address-state"
value={addressState}
onChange={(e) => {
setAddressState(e.target.value);
validateAddressStateInput(e.target.value);
}}
onBlur={handleAddressStateBlur}
placeholder="State"
/>
{errors.addressState && (
<p className="mt-1 text-xs text-error">{errors.addressState}</p>
)}
</div>
<div className="space-y-2">
<div className="flex items-center gap-1">
<label
htmlFor="loc-address-pincode"
className="text-sm font-medium text-foreground-default"
>
Pincode
</label>
<span aria-hidden="true" className="text-error text-base font-bold">*</span>
</div>
<Input
id="loc-address-pincode"
value={addressPincode}
onChange={(e) =>
setAddressPincode(e.target.value.replace(/\D/g, ""))
}
onBlur={validatePincode}
placeholder="560001"
maxLength={6}
/>
{errors.addressPincode && (
<p className="mt-1 text-xs text-error">
{errors.addressPincode}
</p>
)}
</div>
</div>
</div>
<div className="flex justify-end gap-2 pt-4">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit" disabled={hasErrors}>
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>
);
}
|