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 | 9x 59x 59x 59x 59x 59x 59x 45x 1x 59x 22x 59x 59x 4x 1x 3x 59x 59x 47x 47x 59x 8x 8x 8x 8x 59x 59x 1x 102x 60x 1x 236x 25x 4x | import { useState, useMemo, useEffect, type FormEvent } from "react";
import { X } from "lucide-react";
import { Button } from "../../ui/button";
import { Input } from "../../ui/input";
import { ConfirmDialog } from "../../ui/confirm-dialog";
import { useUnsavedChanges, useDialogAccessibility } from "../../../hooks";
import { useLocationCascade } from "../../../hooks/useLocationCascade";
import {
validatePropertySize,
propertySizeErrorMessage,
normalizePropertySize,
} from "@interioring/utils/validation/property-size";
import type { Project } from "../../../lib/api";
interface LocationPropertyModalProps {
project: Project;
onSave: (data: Partial<Project>) => void;
onClose: () => void;
}
const PROPERTY_TYPES = [
{ value: "apartment", label: "Apartment" },
{ value: "villa", label: "Villa" },
{ value: "independent_house", label: "Independent House" },
{ value: "commercial", label: "Commercial" },
];
export function LocationPropertyModal({ project, onSave, onClose }: LocationPropertyModalProps) {
const {
cities,
localities,
selectedCityId,
localityId,
setLocalityId,
handleCityChange,
initializeFromLocality,
} = useLocationCascade();
const [propertyType, setPropertyType] = useState(project.propertyType || "");
const [propertySize, setPropertySize] = useState(project.propertySize || "");
const [projectAreaSqft, setProjectAreaSqft] = useState(
project.projectAreaSqft?.toString() || "",
);
const [showDiscardConfirm, setShowDiscardConfirm] = useState(false);
// Initialize city + locality from project's existing localityId
useEffect(() => {
if (project.localityId && cities.length > 0) {
initializeFromLocality(project.localityId);
}
}, [project.localityId, cities.length, initializeFromLocality]);
const initialValues = useMemo(
() => ({
localityId: project.localityId || "",
propertyType: project.propertyType || "",
propertySize: project.propertySize || "",
projectAreaSqft: project.projectAreaSqft?.toString() || "",
}),
[project],
);
const isDirty = useUnsavedChanges(initialValues, {
localityId,
propertyType,
propertySize,
projectAreaSqft,
});
const handleClose = () => {
if (isDirty) {
setShowDiscardConfirm(true);
} else {
onClose();
}
};
const { dialogRef, handleFocusTrap } = useDialogAccessibility(handleClose);
const propertySizeError = useMemo(() => {
const err = validatePropertySize(propertySize);
return err ? propertySizeErrorMessage(err) : null;
}, [propertySize]);
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
Iif (propertySizeError) return;
const normalizedPropertySize = normalizePropertySize(propertySize);
onSave({
localityId: localityId || null,
propertyType: propertyType
? (propertyType as "apartment" | "villa" | "independent_house" | "commercial")
: null,
propertySize: normalizedPropertySize || null,
projectAreaSqft: projectAreaSqft ? Number.parseInt(projectAreaSqft, 10) : null,
});
};
const selectClass =
"flex h-10 w-full rounded-md border border-default bg-background-elevated px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500 disabled:opacity-50";
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-property-modal-title"
tabIndex={-1}
className="relative bg-background-elevated rounded-lg shadow-xl w-full max-w-lg 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-property-modal-title" className="font-semibold">
Edit Location & Property
</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="grid grid-cols-1 sm:grid-cols-2 gap-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={selectedCityId} onChange={(e) => handleCityChange(e.target.value)} className={selectClass}>
<option value="">Select city</option>
{cities.map((city) => (
<option key={city.id} value={city.id}>
{city.name}, {city.state}
</option>
))}
</select>
</div>
<div className="space-y-2">
<label htmlFor="loc-locality" className="text-sm font-medium text-foreground-default">
Locality
</label>
<select id="loc-locality" value={localityId} onChange={(e) => setLocalityId(e.target.value)} disabled={!selectedCityId} className={selectClass}>
<option value="">Select locality</option>
{localities.map((locality) => (
<option key={locality.id} value={locality.id}>
{locality.name}
</option>
))}
</select>
</div>
<div className="space-y-2">
<label htmlFor="loc-property-type" className="text-sm font-medium text-foreground-default">
Property Type
</label>
<select id="loc-property-type" value={propertyType} onChange={(e) => setPropertyType(e.target.value)} className={selectClass}>
<option value="">Select type</option>
{PROPERTY_TYPES.map((t) => (
<option key={t.value} value={t.value}>{t.label}</option>
))}
</select>
</div>
<div className="space-y-2">
<label htmlFor="loc-property-size" className="text-sm font-medium text-foreground-default">
Property Size
</label>
<Input
id="loc-property-size"
value={propertySize}
onChange={(e) => setPropertySize(e.target.value)}
placeholder="e.g., 3 BHK or 2400 sq ft"
aria-invalid={propertySizeError ? true : undefined}
aria-describedby={propertySizeError ? "loc-property-size-error" : undefined}
/>
{propertySizeError && (
<p
id="loc-property-size-error"
role="alert"
className="text-sm text-error"
>
{propertySizeError}
</p>
)}
</div>
<div className="space-y-2">
<label htmlFor="loc-area-sqft" className="text-sm font-medium text-foreground-default">
Project Area (sq ft)
</label>
<Input
id="loc-area-sqft"
type="number"
value={projectAreaSqft}
onChange={(e) => setProjectAreaSqft(e.target.value)}
placeholder="e.g., 1200"
/>
</div>
</div>
<div className="flex justify-end gap-2 pt-4">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit" disabled={Boolean(propertySizeError)}>
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>
);
}
|