Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/components/map/user-home-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export function UserHomeDialog({
const [error, setError] = React.useState<string | null>(null);
const [wasOpen, setWasOpen] = React.useState(false);

// Re-fill the form during render (not an effect) each time the dialog
// opens, so there's no stale-data flash.
if (open !== wasOpen) {
setWasOpen(open);
if (open) {
Expand Down Expand Up @@ -73,7 +71,16 @@ export function UserHomeDialog({

const latNum = Number(lat);
const lngNum = Number(lng);
const isValid = label.trim() && lat !== "" && lng !== "" && !Number.isNaN(latNum) && !Number.isNaN(lngNum);
const latError = lat !== "" && (Number.isNaN(latNum) || latNum < -90 || latNum > 90);
const lngError = lng !== "" && (Number.isNaN(lngNum) || lngNum < -180 || lngNum > 180);
const isValid =
label.trim() &&
lat !== "" &&
lng !== "" &&
!Number.isNaN(latNum) &&
!Number.isNaN(lngNum) &&
!latError &&
!lngError;

async function handleSave() {
if (!isValid) return;
Expand Down Expand Up @@ -115,9 +122,6 @@ export function UserHomeDialog({
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
// Only the X button or Escape should close this form. Ignoring
// outside pointer/interaction events keeps a stray click (or a
// click-injecting browser extension) from dismissing it mid-edit.
onPointerDownOutside={(e) => e.preventDefault()}
onInteractOutside={(e) => e.preventDefault()}
>
Expand Down Expand Up @@ -146,7 +150,11 @@ export function UserHomeDialog({
step="any"
value={lat}
onChange={(e) => setLat(e.target.value)}
aria-invalid={latError}
/>
{latError && (
<p className="text-sm text-destructive">Latitude must be between -90 and 90.</p>
)}
</div>
<div className="grid gap-1.5">
<Label htmlFor="user-home-lng">Longitude</Label>
Expand All @@ -156,7 +164,11 @@ export function UserHomeDialog({
step="any"
value={lng}
onChange={(e) => setLng(e.target.value)}
aria-invalid={lngError}
/>
{lngError && (
<p className="text-sm text-destructive">Longitude must be between -180 and 180.</p>
)}
</div>
</div>

Expand Down
24 changes: 18 additions & 6 deletions src/components/map/user-place-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ export function UserPlaceDialog({
const [error, setError] = React.useState<string | null>(null);
const [lastResetKey, setLastResetKey] = React.useState<string | null>(null);

// Re-fill the form during render (not an effect) each time the dialog
// opens, or opens for a different place, so there's no stale-data flash.
const resetKey = open ? place?.id ?? "new" : null;
if (resetKey !== lastResetKey) {
setLastResetKey(resetKey);
Expand Down Expand Up @@ -99,8 +97,17 @@ export function UserPlaceDialog({

const latNum = Number(lat);
const lngNum = Number(lng);
const latError = lat !== "" && (Number.isNaN(latNum) || latNum < -90 || latNum > 90);
const lngError = lng !== "" && (Number.isNaN(lngNum) || lngNum < -180 || lngNum > 180);
const isValid =
name.trim() && city.trim() && lat !== "" && lng !== "" && !Number.isNaN(latNum) && !Number.isNaN(lngNum);
name.trim() &&
city.trim() &&
lat !== "" &&
lng !== "" &&
!Number.isNaN(latNum) &&
!Number.isNaN(lngNum) &&
!latError &&
!lngError;

async function handleSave() {
if (!isValid) return;
Expand Down Expand Up @@ -154,9 +161,6 @@ export function UserPlaceDialog({
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent
// Only the X button or Escape should close this form. Ignoring
// outside pointer/interaction events keeps a stray click (or a
// click-injecting browser extension) from dismissing it mid-edit.
onPointerDownOutside={(e) => e.preventDefault()}
onInteractOutside={(e) => e.preventDefault()}
>
Expand Down Expand Up @@ -212,7 +216,11 @@ export function UserPlaceDialog({
step="any"
value={lat}
onChange={(e) => setLat(e.target.value)}
aria-invalid={latError}
/>
{latError && (
<p className="text-sm text-destructive">Latitude must be between -90 and 90.</p>
)}
</div>
<div className="grid gap-1.5">
<Label htmlFor="user-place-lng">Longitude</Label>
Expand All @@ -222,7 +230,11 @@ export function UserPlaceDialog({
step="any"
value={lng}
onChange={(e) => setLng(e.target.value)}
aria-invalid={lngError}
/>
{lngError && (
<p className="text-sm text-destructive">Longitude must be between -180 and 180.</p>
)}
</div>
</div>

Expand Down
Loading