mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
b657f79a2a
Add a dashboard for managing Google Calendar events and tasks, including create, edit, delete, status updates, search, and integration error notices.
240 lines
7.0 KiB
TypeScript
240 lines
7.0 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState } from 'react';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
} from '@/components/ui/dialog';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import { Label } from '@/components/ui/label';
|
|
import { CalendarTaskItem, TaskStatus } from '@/lib/google-calendar/types';
|
|
import { createCalendarTask, updateCalendarTask } from '@/lib/google-calendar/tasks.actions';
|
|
import { toast } from 'sonner';
|
|
import { Loader2 } from 'lucide-react';
|
|
import dayjs from 'dayjs';
|
|
|
|
interface TaskFormModalProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
task?: CalendarTaskItem | null;
|
|
onSuccess: () => void;
|
|
}
|
|
|
|
interface FormInnerProps {
|
|
task?: CalendarTaskItem | null;
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
}
|
|
|
|
function TaskFormInner({ task, onClose, onSuccess }: FormInnerProps) {
|
|
const isEdit = !!task;
|
|
|
|
const defaultDue = task
|
|
? task.due.includes('T')
|
|
? dayjs(task.due).format('YYYY-MM-DDTHH:mm')
|
|
: dayjs(task.due).format('YYYY-MM-DD')
|
|
: dayjs().add(1, 'day').hour(17).minute(0).second(0).format('YYYY-MM-DDTHH:mm');
|
|
|
|
const [title, setTitle] = useState(task?.title || '');
|
|
const [description, setDescription] = useState(task?.description || '');
|
|
const [due, setDue] = useState(defaultDue);
|
|
const [status, setStatus] = useState<TaskStatus>(task?.status || 'needsAction');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
if (!title.trim()) {
|
|
toast.error('Judul task wajib diisi!');
|
|
return;
|
|
}
|
|
if (!due) {
|
|
toast.error('Tenggat waktu task wajib diisi!');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setLoading(true);
|
|
|
|
if (isEdit && task) {
|
|
const res = await updateCalendarTask(task.id, {
|
|
title,
|
|
description,
|
|
due,
|
|
status,
|
|
});
|
|
|
|
if (res.success) {
|
|
toast.success('Task berhasil diperbarui di Google Calendar!');
|
|
onClose();
|
|
onSuccess();
|
|
} else {
|
|
toast.error(res.error || 'Gagal memperbarui task.');
|
|
}
|
|
} else {
|
|
const res = await createCalendarTask({
|
|
title,
|
|
description,
|
|
due,
|
|
status,
|
|
});
|
|
|
|
if (res.success) {
|
|
toast.success('Task berhasil ditambahkan ke Google Calendar!');
|
|
onClose();
|
|
onSuccess();
|
|
} else {
|
|
toast.error(res.error || 'Gagal membuat task.');
|
|
}
|
|
}
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Terjadi kesalahan sistem.';
|
|
toast.error(msg);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit} className="flex flex-col">
|
|
<DialogHeader className="p-6 pb-4 border-b border-zinc-100 dark:border-zinc-800">
|
|
<DialogTitle className="text-lg font-semibold text-zinc-900 dark:text-zinc-100">
|
|
{isEdit ? 'Edit Task Kalender' : 'Tambah Task Baru'}
|
|
</DialogTitle>
|
|
<DialogDescription className="text-xs text-zinc-500 dark:text-zinc-400">
|
|
Task akan disimpan sebagai agenda berlabel [TASK] pada kalender Google resmi.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="p-6 space-y-4 max-h-[70vh] overflow-y-auto">
|
|
{/* Title */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="task-title" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Judul Task <span className="text-rose-500">*</span>
|
|
</Label>
|
|
<Input
|
|
id="task-title"
|
|
placeholder="misal: Finalisasi Laporan Bulanan"
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
disabled={loading}
|
|
required
|
|
className="h-9 text-sm"
|
|
/>
|
|
</div>
|
|
|
|
{/* Due Date & Time */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="task-due" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Tenggat Waktu (Due Date) <span className="text-rose-500">*</span>
|
|
</Label>
|
|
<Input
|
|
id="task-due"
|
|
type="datetime-local"
|
|
value={due}
|
|
onChange={(e) => setDue(e.target.value)}
|
|
disabled={loading}
|
|
required
|
|
className="h-9 text-xs sm:text-sm"
|
|
/>
|
|
</div>
|
|
|
|
{/* Status Option */}
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Status Pengerjaan
|
|
</Label>
|
|
<div className="grid grid-cols-2 gap-2.5">
|
|
<button
|
|
type="button"
|
|
onClick={() => setStatus('needsAction')}
|
|
className={`flex items-center justify-center py-2 px-3 rounded-lg border text-xs font-medium transition-all ${status === 'needsAction'
|
|
? 'border-amber-500 bg-amber-50/70 text-amber-900 dark:border-amber-600 dark:bg-amber-950/40 dark:text-amber-200'
|
|
: 'border-zinc-200 dark:border-zinc-800 text-zinc-600 dark:text-zinc-400 hover:bg-zinc-50 dark:hover:bg-zinc-900'
|
|
}`}
|
|
>
|
|
Menunggu (needsAction)
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
onClick={() => setStatus('completed')}
|
|
className={`flex items-center justify-center py-2 px-3 rounded-lg border text-xs font-medium transition-all ${status === 'completed'
|
|
? 'border-emerald-500 bg-emerald-50/70 text-emerald-900 dark:border-emerald-600 dark:bg-emerald-950/40 dark:text-emerald-200'
|
|
: 'border-zinc-200 dark:border-zinc-800 text-zinc-600 dark:text-zinc-400 hover:bg-zinc-50 dark:hover:bg-zinc-900'
|
|
}`}
|
|
>
|
|
Selesai (completed)
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Description / Notes */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="task-description" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Catatan / Detail Task (Opsional)
|
|
</Label>
|
|
<Textarea
|
|
id="task-description"
|
|
placeholder="Rincian checklist atau catatan tugas..."
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
disabled={loading}
|
|
rows={3}
|
|
className="text-xs sm:text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<DialogFooter className="p-4 bg-zinc-50 dark:bg-zinc-900/50 border-t border-zinc-100 dark:border-zinc-800 flex items-center justify-end gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={onClose}
|
|
disabled={loading}
|
|
>
|
|
Batal
|
|
</Button>
|
|
<Button
|
|
type="submit"
|
|
size="sm"
|
|
disabled={loading}
|
|
className="bg-emerald-600 hover:bg-emerald-700 text-white font-medium"
|
|
>
|
|
{loading && <Loader2 className="size-3.5 mr-1.5 animate-spin" />}
|
|
{isEdit ? 'Simpan Perubahan' : 'Buat Task'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
export default function TaskFormModal({
|
|
open,
|
|
onOpenChange,
|
|
task,
|
|
onSuccess,
|
|
}: TaskFormModalProps) {
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="p-0 overflow-hidden bg-white dark:bg-zinc-950 border-zinc-200 dark:border-zinc-800">
|
|
{open && (
|
|
<TaskFormInner
|
|
key={task?.id || 'new'}
|
|
task={task}
|
|
onClose={() => onOpenChange(false)}
|
|
onSuccess={onSuccess}
|
|
/>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|