180 lines
6.9 KiB
TypeScript
Executable File
180 lines
6.9 KiB
TypeScript
Executable File
import { router } from '@inertiajs/react';
|
||
import { Pencil, Plus, Trash2 } from 'lucide-react';
|
||
import { useState } from 'react';
|
||
import { MarkerDialog } from '@/components/planner/marker-dialog';
|
||
import { Badge } from '@/components/ui/badge';
|
||
import { Button } from '@/components/ui/button';
|
||
import {
|
||
Dialog,
|
||
DialogContent,
|
||
DialogDescription,
|
||
DialogFooter,
|
||
DialogHeader,
|
||
DialogTitle,
|
||
} from '@/components/ui/dialog';
|
||
import { daysInYear } from '@/lib/calendar';
|
||
import { cn } from '@/lib/utils';
|
||
import { destroy } from '@/routes/markers';
|
||
import type { Marker, VacationPeriod } from '@/types/planner';
|
||
|
||
type Props = {
|
||
markers: Marker[];
|
||
periods: VacationPeriod[];
|
||
year: number;
|
||
activeMarkerId: number | null;
|
||
onSelect: (id: number | null) => void;
|
||
};
|
||
|
||
export function MarkerPanel({
|
||
markers,
|
||
periods,
|
||
year,
|
||
activeMarkerId,
|
||
onSelect,
|
||
}: Props) {
|
||
const [dialogOpen, setDialogOpen] = useState(false);
|
||
const [editing, setEditing] = useState<Marker | null>(null);
|
||
const [deleting, setDeleting] = useState<Marker | null>(null);
|
||
|
||
const dayCount = (markerId: number) =>
|
||
periods
|
||
.filter((p) => p.marker_id === markerId)
|
||
.reduce(
|
||
(sum, p) => sum + daysInYear(p.start_date, p.end_date, year),
|
||
0,
|
||
);
|
||
|
||
const confirmDelete = () => {
|
||
if (!deleting) {
|
||
return;
|
||
}
|
||
|
||
router.delete(destroy.url(deleting.id), {
|
||
preserveScroll: true,
|
||
only: ['markers', 'periods'],
|
||
onSuccess: () => setDeleting(null),
|
||
});
|
||
};
|
||
|
||
return (
|
||
<div className="space-y-3">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
{markers.map((marker) => {
|
||
const active = marker.id === activeMarkerId;
|
||
|
||
return (
|
||
<div
|
||
key={marker.id}
|
||
className={cn(
|
||
'group flex items-center gap-1 rounded-full border py-1 pr-1 pl-3 transition-colors',
|
||
active
|
||
? 'border-foreground bg-accent'
|
||
: 'hover:bg-accent/50',
|
||
)}
|
||
>
|
||
<button
|
||
type="button"
|
||
onClick={() =>
|
||
onSelect(active ? null : marker.id)
|
||
}
|
||
className="flex items-center gap-2 text-sm font-medium outline-none"
|
||
aria-pressed={active}
|
||
data-test={`marker-chip-${marker.id}`}
|
||
title={
|
||
active
|
||
? 'Нажмите, чтобы убрать кисть'
|
||
: 'Выбрать как кисть для отметки дат'
|
||
}
|
||
>
|
||
<span
|
||
className="size-3.5 shrink-0 rounded-full"
|
||
style={{ backgroundColor: marker.color }}
|
||
/>
|
||
<span className="max-w-32 truncate">
|
||
{marker.name}
|
||
</span>
|
||
<Badge
|
||
variant="secondary"
|
||
className="rounded-full px-1.5 text-xs tabular-nums"
|
||
>
|
||
{dayCount(marker.id)} дн.
|
||
</Badge>
|
||
</button>
|
||
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
setEditing(marker);
|
||
setDialogOpen(true);
|
||
}}
|
||
className="rounded-full p-1.5 text-muted-foreground hover:bg-background hover:text-foreground"
|
||
aria-label={`Изменить маркер ${marker.name}`}
|
||
>
|
||
<Pencil className="size-3.5" />
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setDeleting(marker)}
|
||
className="rounded-full p-1.5 text-muted-foreground hover:bg-background hover:text-destructive"
|
||
aria-label={`Удалить маркер ${marker.name}`}
|
||
>
|
||
<Trash2 className="size-3.5" />
|
||
</button>
|
||
</div>
|
||
);
|
||
})}
|
||
|
||
<Button
|
||
variant="outline"
|
||
size="sm"
|
||
className="rounded-full"
|
||
onClick={() => {
|
||
setEditing(null);
|
||
setDialogOpen(true);
|
||
}}
|
||
data-test="add-marker-button"
|
||
>
|
||
<Plus className="size-4" />
|
||
Добавить
|
||
</Button>
|
||
</div>
|
||
|
||
<MarkerDialog
|
||
open={dialogOpen}
|
||
onOpenChange={setDialogOpen}
|
||
marker={editing}
|
||
/>
|
||
|
||
<Dialog
|
||
open={deleting !== null}
|
||
onOpenChange={(open) => !open && setDeleting(null)}
|
||
>
|
||
<DialogContent className="sm:max-w-md">
|
||
<DialogHeader>
|
||
<DialogTitle>Удалить маркер?</DialogTitle>
|
||
<DialogDescription>
|
||
Маркер «{deleting?.name}» и все его отметки на
|
||
календарях будут удалены. Это действие необратимо.
|
||
</DialogDescription>
|
||
</DialogHeader>
|
||
<DialogFooter>
|
||
<Button
|
||
variant="secondary"
|
||
onClick={() => setDeleting(null)}
|
||
>
|
||
Отмена
|
||
</Button>
|
||
<Button
|
||
variant="destructive"
|
||
onClick={confirmDelete}
|
||
data-test="confirm-delete-marker"
|
||
>
|
||
Удалить
|
||
</Button>
|
||
</DialogFooter>
|
||
</DialogContent>
|
||
</Dialog>
|
||
</div>
|
||
);
|
||
}
|