220 lines
9.0 KiB
TypeScript
Executable File
220 lines
9.0 KiB
TypeScript
Executable File
import { Pencil, Trash2 } from 'lucide-react';
|
||
import { useState } from 'react';
|
||
import type { DayInfo } from '@/components/planner/month-calendar';
|
||
import { Button } from '@/components/ui/button';
|
||
import {
|
||
Dialog,
|
||
DialogContent,
|
||
DialogDescription,
|
||
DialogHeader,
|
||
DialogTitle,
|
||
} from '@/components/ui/dialog';
|
||
import { Input } from '@/components/ui/input';
|
||
import { Label } from '@/components/ui/label';
|
||
import {
|
||
Select,
|
||
SelectContent,
|
||
SelectItem,
|
||
SelectTrigger,
|
||
SelectValue,
|
||
} from '@/components/ui/select';
|
||
import { daysInclusive, formatRangeRu, formatRu } from '@/lib/calendar';
|
||
import type { Marker, VacationPeriod } from '@/types/planner';
|
||
|
||
type Props = {
|
||
date: string | null;
|
||
entries: DayInfo[];
|
||
markers: Marker[];
|
||
onClose: () => void;
|
||
onDelete: (period: VacationPeriod) => void;
|
||
onReschedule: (
|
||
period: VacationPeriod,
|
||
data: { start_date: string; end_date: string; marker_id: number },
|
||
) => void;
|
||
};
|
||
|
||
export function DayDialog({
|
||
date,
|
||
entries,
|
||
markers,
|
||
onClose,
|
||
onDelete,
|
||
onReschedule,
|
||
}: Props) {
|
||
const [editing, setEditing] = useState<VacationPeriod | null>(null);
|
||
const [start, setStart] = useState('');
|
||
const [end, setEnd] = useState('');
|
||
const [markerId, setMarkerId] = useState<number | null>(null);
|
||
|
||
const startEdit = (period: VacationPeriod) => {
|
||
setEditing(period);
|
||
setStart(period.start_date);
|
||
setEnd(period.end_date);
|
||
setMarkerId(period.marker_id);
|
||
};
|
||
|
||
const save = () => {
|
||
if (!editing || !markerId || end < start) {
|
||
return;
|
||
}
|
||
|
||
onReschedule(editing, {
|
||
start_date: start,
|
||
end_date: end,
|
||
marker_id: markerId,
|
||
});
|
||
setEditing(null);
|
||
};
|
||
|
||
const close = () => {
|
||
setEditing(null);
|
||
onClose();
|
||
};
|
||
|
||
return (
|
||
<Dialog open={date !== null} onOpenChange={(open) => !open && close()}>
|
||
<DialogContent className="sm:max-w-md">
|
||
<DialogHeader>
|
||
<DialogTitle>
|
||
{date ? formatRu(date, true) : ''}
|
||
</DialogTitle>
|
||
<DialogDescription>
|
||
{entries.length > 0
|
||
? 'Отметки на этот день'
|
||
: 'На этот день нет отметок'}
|
||
</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
{editing ? (
|
||
<div className="space-y-4">
|
||
<div className="grid grid-cols-2 gap-3">
|
||
<div className="grid gap-2">
|
||
<Label htmlFor="period-start">Начало</Label>
|
||
<Input
|
||
id="period-start"
|
||
type="date"
|
||
value={start}
|
||
onChange={(e) => setStart(e.target.value)}
|
||
/>
|
||
</div>
|
||
<div className="grid gap-2">
|
||
<Label htmlFor="period-end">Конец</Label>
|
||
<Input
|
||
id="period-end"
|
||
type="date"
|
||
value={end}
|
||
min={start}
|
||
onChange={(e) => setEnd(e.target.value)}
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="grid gap-2">
|
||
<Label>Маркер</Label>
|
||
<Select
|
||
value={markerId ? String(markerId) : undefined}
|
||
onValueChange={(v) => setMarkerId(Number(v))}
|
||
>
|
||
<SelectTrigger className="w-full">
|
||
<SelectValue />
|
||
</SelectTrigger>
|
||
<SelectContent>
|
||
{markers.map((m) => (
|
||
<SelectItem
|
||
key={m.id}
|
||
value={String(m.id)}
|
||
>
|
||
<span className="flex items-center gap-2">
|
||
<span
|
||
className="size-3 rounded-full"
|
||
style={{
|
||
backgroundColor:
|
||
m.color,
|
||
}}
|
||
/>
|
||
{m.name}
|
||
</span>
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
</div>
|
||
|
||
{end < start && (
|
||
<p className="text-sm text-destructive">
|
||
Дата окончания не может быть раньше начала.
|
||
</p>
|
||
)}
|
||
|
||
<div className="flex justify-end gap-2">
|
||
<Button
|
||
variant="secondary"
|
||
onClick={() => setEditing(null)}
|
||
>
|
||
Отмена
|
||
</Button>
|
||
<Button
|
||
onClick={save}
|
||
disabled={!markerId || end < start}
|
||
data-test="save-period-button"
|
||
>
|
||
Сохранить
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
) : (
|
||
<div className="space-y-2">
|
||
{entries.map(({ marker, period }) => (
|
||
<div
|
||
key={period.id}
|
||
className="flex items-center gap-3 rounded-md border p-3"
|
||
>
|
||
<span
|
||
className="size-3 shrink-0 rounded-full"
|
||
style={{ backgroundColor: marker.color }}
|
||
/>
|
||
<div className="min-w-0 flex-1">
|
||
<div className="truncate text-sm font-medium">
|
||
{marker.name}
|
||
</div>
|
||
<div className="text-xs text-muted-foreground">
|
||
{formatRangeRu(
|
||
period.start_date,
|
||
period.end_date,
|
||
)}{' '}
|
||
·{' '}
|
||
{daysInclusive(
|
||
period.start_date,
|
||
period.end_date,
|
||
)}{' '}
|
||
дн.
|
||
</div>
|
||
</div>
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="size-8"
|
||
onClick={() => startEdit(period)}
|
||
aria-label={`Изменить период: ${marker.name}`}
|
||
>
|
||
<Pencil className="size-4" />
|
||
</Button>
|
||
<Button
|
||
variant="ghost"
|
||
size="icon"
|
||
className="size-8 text-muted-foreground hover:text-destructive"
|
||
onClick={() => onDelete(period)}
|
||
aria-label={`Удалить период: ${marker.name}`}
|
||
data-test={`delete-period-${period.id}`}
|
||
>
|
||
<Trash2 className="size-4" />
|
||
</Button>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</DialogContent>
|
||
</Dialog>
|
||
);
|
||
}
|