vacation-planner/resources/js/components/planner/marker-dialog.tsx
2026-11-28 12:59:18 +08:00

136 lines
5.1 KiB
TypeScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useForm } from '@inertiajs/react';
import { useEffect } from 'react';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { MARKER_COLORS } from '@/lib/marker-colors';
import { cn } from '@/lib/utils';
import { store, update } from '@/routes/markers';
import type { Marker } from '@/types/planner';
type Props = {
open: boolean;
onOpenChange: (open: boolean) => void;
marker: Marker | null;
};
export function MarkerDialog({ open, onOpenChange, marker }: Props) {
const form = useForm({
name: marker?.name ?? '',
color: marker?.color ?? MARKER_COLORS[0],
});
useEffect(() => {
if (open) {
form.setData({
name: marker?.name ?? '',
color: marker?.color ?? MARKER_COLORS[0],
});
form.clearErrors();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, marker]);
const submit = (e: React.FormEvent) => {
e.preventDefault();
const options = {
preserveScroll: true,
only: ['markers'],
onSuccess: () => onOpenChange(false),
};
if (marker) {
form.patch(update.url(marker.id), options);
} else {
form.post(store().url, options);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>
{marker ? 'Изменить маркер' : 'Новый маркер'}
</DialogTitle>
<DialogDescription>
Маркер — это цвет и имя человека, которым вы отмечаете
отпуска.
</DialogDescription>
</DialogHeader>
<form onSubmit={submit} className="space-y-4">
<div className="grid gap-2">
<Label htmlFor="marker-name">Имя</Label>
<Input
id="marker-name"
value={form.data.name}
onChange={(e) =>
form.setData('name', e.target.value)
}
placeholder="Например: Анна"
maxLength={50}
required
autoFocus
data-test="marker-name-input"
/>
<InputError message={form.errors.name} />
</div>
<div className="grid gap-2">
<Label>Цвет</Label>
<div className="flex flex-wrap gap-2">
{MARKER_COLORS.map((color) => (
<button
key={color}
type="button"
onClick={() => form.setData('color', color)}
className={cn(
'size-8 rounded-full border-2 transition-transform hover:scale-110',
form.data.color === color
? 'border-foreground ring-2 ring-ring ring-offset-2 ring-offset-background'
: 'border-transparent',
)}
style={{ backgroundColor: color }}
aria-label={`Цвет ${color}`}
aria-pressed={form.data.color === color}
/>
))}
</div>
<InputError message={form.errors.color} />
</div>
<DialogFooter>
<Button
type="button"
variant="secondary"
onClick={() => onOpenChange(false)}
>
Отмена
</Button>
<Button
type="submit"
disabled={form.processing}
data-test="marker-save-button"
>
{form.processing && <Spinner />}
{marker ? 'Сохранить' : 'Добавить'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}