mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 11:06:29 +00:00
4931d3be5f
Adjust event detail modal spacing, labels, links, and action icon styling. Update the monthly calendar's header and day-cell colors and reformat its JSX.
538 lines
18 KiB
TypeScript
538 lines
18 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 { Label } from '@/components/ui/label';
|
|
import { Switch } from '@/components/ui/switch';
|
|
import { Badge } from '@/components/ui/badge';
|
|
import DescriptionRichEditor from './DescriptionRichEditor';
|
|
import { CalendarEventItem, GoogleCalendarListEntry } from '@/lib/google-calendar/types';
|
|
import { GOOGLE_EVENT_COLORS, getEventColorInfo } from '@/lib/google-calendar/constants';
|
|
import { createCalendarEvent, updateCalendarEvent } from '@/lib/google-calendar/events.actions';
|
|
import { toast } from 'sonner';
|
|
import { Loader2, Plus, X, Video, Users, Palette, Check, Calendar as CalendarIcon } from 'lucide-react';
|
|
import dayjs from 'dayjs';
|
|
|
|
interface EventFormModalProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
event?: CalendarEventItem | null;
|
|
initialDate?: string | null; // YYYY-MM-DD when clicking empty grid cell
|
|
defaultCalendarId?: string;
|
|
calendars?: GoogleCalendarListEntry[];
|
|
onSuccess: () => void;
|
|
}
|
|
|
|
interface FormInnerProps {
|
|
event?: CalendarEventItem | null;
|
|
initialDate?: string | null;
|
|
defaultCalendarId?: string;
|
|
calendars?: GoogleCalendarListEntry[];
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
}
|
|
|
|
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
|
|
function EventFormInner({
|
|
event,
|
|
initialDate,
|
|
defaultCalendarId,
|
|
calendars = [],
|
|
onClose,
|
|
onSuccess,
|
|
}: FormInnerProps) {
|
|
const isEdit = Boolean(event);
|
|
|
|
// Default All-Day Event when creating a new event
|
|
const initialIsAllDay = event ? event.isAllDay : true;
|
|
const baseDateStr = initialDate || dayjs().format('YYYY-MM-DD');
|
|
|
|
const defaultStart = event ? event.isAllDay ? dayjs(event.start).format('YYYY-MM-DD') : dayjs(event.start).format('YYYY-MM-DDTHH:mm') : baseDateStr;
|
|
|
|
const defaultEnd = event ? event.isAllDay ? dayjs(event.end || event.start).format('YYYY-MM-DD') : dayjs(event.end).format('YYYY-MM-DDTHH:mm') : baseDateStr;
|
|
|
|
const writableCalendars = calendars.filter((c) => c.isWritable);
|
|
const resolvedDefaultCalId = event?.calendarId || defaultCalendarId || writableCalendars.find((c) => c.isDefaultEnv)?.id || writableCalendars[0]?.id || '';
|
|
|
|
const [targetCalendarId, setTargetCalendarId] = useState<string>(resolvedDefaultCalId);
|
|
const [summary, setSummary] = useState(event?.summary || '');
|
|
const [description, setDescription] = useState(event?.description || '');
|
|
const [location, setLocation] = useState(event?.location || '');
|
|
const [isAllDay, setIsAllDay] = useState(initialIsAllDay);
|
|
const [startDateTime, setStartDateTime] = useState(defaultStart);
|
|
const [endDateTime, setEndDateTime] = useState(defaultEnd);
|
|
|
|
// Fields: colorId, attendees, Google Meet
|
|
const [colorId, setColorId] = useState<string>(event?.colorId || '');
|
|
const [attendees, setAttendees] = useState<string[]>(event?.attendees || []);
|
|
const [guestInput, setGuestInput] = useState('');
|
|
const [generateMeet, setGenerateMeet] = useState(false);
|
|
const [manualMeetLink, setManualMeetLink] = useState(event?.hangoutLink || '');
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const selectedColorInfo = getEventColorInfo(colorId);
|
|
|
|
const handleAddGuest = () => {
|
|
const raw = guestInput.trim().toLowerCase().replace(/,$/, '');
|
|
if (!raw) return;
|
|
|
|
if (!EMAIL_REGEX.test(raw)) {
|
|
toast.error(`Format email "${raw}" tidak valid.`);
|
|
return;
|
|
}
|
|
|
|
if (attendees.includes(raw)) {
|
|
toast.info(`Email "${raw}" sudah ada di daftar tamu.`);
|
|
setGuestInput('');
|
|
return;
|
|
}
|
|
|
|
setAttendees((prev) => [...prev, raw]);
|
|
setGuestInput('');
|
|
};
|
|
|
|
const handleGuestKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
if (e.key === 'Enter' || e.key === ',') {
|
|
e.preventDefault();
|
|
handleAddGuest();
|
|
}
|
|
};
|
|
|
|
const handleRemoveGuest = (emailToRemove: string) => {
|
|
setAttendees((prev) => prev.filter((email) => email !== emailToRemove));
|
|
};
|
|
|
|
const handleStartChange = (val: string) => {
|
|
setStartDateTime(val);
|
|
if (!val) return;
|
|
|
|
if (isAllDay) {
|
|
if (endDateTime && dayjs(endDateTime).isBefore(dayjs(val), 'day')) {
|
|
setEndDateTime(dayjs(val).format('YYYY-MM-DD'));
|
|
}
|
|
} else {
|
|
if (endDateTime && !dayjs(endDateTime).isAfter(dayjs(val))) {
|
|
setEndDateTime(dayjs(val).add(1, 'hour').format('YYYY-MM-DDTHH:mm'));
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
if (!summary.trim()) {
|
|
toast.error('Judul event wajib diisi!');
|
|
return;
|
|
}
|
|
if (!startDateTime || !endDateTime) {
|
|
toast.error('Waktu mulai dan selesai wajib diisi!');
|
|
return;
|
|
}
|
|
|
|
const finalAttendees = [...attendees];
|
|
const pendingGuest = guestInput.trim().toLowerCase();
|
|
if (pendingGuest) {
|
|
if (!EMAIL_REGEX.test(pendingGuest)) {
|
|
toast.error(`Format email tamu "${pendingGuest}" tidak valid.`);
|
|
return;
|
|
}
|
|
if (!finalAttendees.includes(pendingGuest)) {
|
|
finalAttendees.push(pendingGuest);
|
|
}
|
|
}
|
|
|
|
try {
|
|
setLoading(true);
|
|
|
|
const payload = {
|
|
calendarId: targetCalendarId || defaultCalendarId || undefined,
|
|
summary,
|
|
description,
|
|
location,
|
|
startDateTime,
|
|
endDateTime,
|
|
isAllDay,
|
|
colorId: colorId || undefined,
|
|
attendees: finalAttendees,
|
|
generateMeet,
|
|
manualMeetLink: manualMeetLink.trim() || undefined,
|
|
};
|
|
|
|
const res =
|
|
isEdit && event
|
|
? await updateCalendarEvent(event.id, payload)
|
|
: await createCalendarEvent(payload);
|
|
|
|
if (res.success) {
|
|
toast.success(
|
|
isEdit
|
|
? 'Event berhasil diperbarui di Google Calendar!'
|
|
: 'Event berhasil ditambahkan ke Google Calendar!'
|
|
);
|
|
if (res.warning) {
|
|
toast.warning(res.warning, { duration: 6000 });
|
|
}
|
|
onClose();
|
|
onSuccess();
|
|
} else {
|
|
toast.error(res.error || 'Gagal menyimpan event.', { duration: 5000 });
|
|
}
|
|
} catch (err: unknown) {
|
|
const msg = err instanceof Error ? err.message : 'Terjadi kesalahan tidak terduga.';
|
|
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 Event Google Calendar' : 'Tambah Event Baru'}
|
|
</DialogTitle>
|
|
<DialogDescription className="text-xs text-zinc-500 dark:text-zinc-400">
|
|
Perubahan akan langsung disinkronkan ke kalender Google secara real-time.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="p-6 space-y-4 max-h-[70vh] overflow-y-auto">
|
|
{/* Target Calendar Selector (when multiple writable calendars exist) */}
|
|
{writableCalendars.length > 0 && (
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="target-calendar" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300 flex items-center gap-1.5">
|
|
<CalendarIcon className="size-3.5 text-zinc-500" />
|
|
<span>Simpan ke Kalender</span>
|
|
</Label>
|
|
<select
|
|
id="target-calendar"
|
|
value={targetCalendarId}
|
|
onChange={(e) => setTargetCalendarId(e.target.value)}
|
|
disabled={loading || isEdit}
|
|
className="h-9 w-full rounded-md border border-zinc-200 dark:border-zinc-800 bg-white dark:bg-zinc-950 px-3 text-xs sm:text-sm text-zinc-900 dark:text-zinc-100 outline-none focus:ring-2 focus:ring-zinc-400 disabled:opacity-60"
|
|
>
|
|
{writableCalendars.map((cal) => (
|
|
<option key={cal.id} value={cal.id}>
|
|
{cal.summary} {cal.isDefaultEnv ? '(Default .env)' : cal.primary ? '(Utama)' : ''}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
)}
|
|
|
|
{/* Summary */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="event-summary" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Judul Event <span className="text-rose-500">*</span>
|
|
</Label>
|
|
<Input
|
|
id="event-summary"
|
|
placeholder="misal: Rapat Koordinasi Tim Project"
|
|
value={summary}
|
|
onChange={(e) => setSummary(e.target.value)}
|
|
disabled={loading}
|
|
required
|
|
autoFocus
|
|
className="h-9 text-sm"
|
|
/>
|
|
</div>
|
|
|
|
{/* All Day Toggle */}
|
|
<div className="flex items-center justify-between p-3 rounded-lg bg-zinc-50 dark:bg-zinc-900/60 border border-zinc-200/80 dark:border-zinc-800">
|
|
<div className="space-y-0.5">
|
|
<Label htmlFor="all-day-toggle" className="text-xs font-semibold text-zinc-800 dark:text-zinc-200 cursor-pointer">
|
|
Acara Sepanjang Hari (All Day)
|
|
</Label>
|
|
<p className="text-[11px] text-zinc-500">
|
|
Menggunakan format tanggal harian (YYYY-MM-DD) tanpa jam spesifik
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
id="all-day-toggle"
|
|
checked={isAllDay}
|
|
onCheckedChange={(checked) => {
|
|
setIsAllDay(checked);
|
|
if (checked) {
|
|
const startDay = startDateTime.split('T')[0] || dayjs().format('YYYY-MM-DD');
|
|
const endDay = endDateTime.split('T')[0] || startDay;
|
|
setStartDateTime(startDay);
|
|
setEndDateTime(dayjs(endDay).isBefore(dayjs(startDay), 'day') ? startDay : endDay);
|
|
} else {
|
|
const baseDay = startDateTime.split('T')[0] || dayjs().format('YYYY-MM-DD');
|
|
const startFull = dayjs(`${baseDay}T09:00`).format('YYYY-MM-DDTHH:mm');
|
|
const endFull = dayjs(`${baseDay}T10:00`).format('YYYY-MM-DDTHH:mm');
|
|
setStartDateTime(startFull);
|
|
setEndDateTime(endFull);
|
|
}
|
|
}}
|
|
disabled={loading}
|
|
/>
|
|
</div>
|
|
|
|
{/* Dates: Start & End */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="event-start" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
{isAllDay ? 'Tanggal Mulai' : 'Waktu Mulai'} <span className="text-rose-500">*</span>
|
|
</Label>
|
|
<Input
|
|
id="event-start"
|
|
type={isAllDay ? 'date' : 'datetime-local'}
|
|
value={startDateTime}
|
|
onChange={(e) => handleStartChange(e.target.value)}
|
|
disabled={loading}
|
|
required
|
|
className="h-9 text-xs sm:text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="event-end" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
{isAllDay ? 'Tanggal Selesai (Inklusif)' : 'Waktu Selesai'} <span className="text-rose-500">*</span>
|
|
</Label>
|
|
<Input
|
|
id="event-end"
|
|
type={isAllDay ? 'date' : 'datetime-local'}
|
|
value={endDateTime}
|
|
min={startDateTime}
|
|
onChange={(e) => setEndDateTime(e.target.value)}
|
|
disabled={loading}
|
|
required
|
|
className="h-9 text-xs sm:text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Event Color Picker (colorId 1 - 11) */}
|
|
<div className="space-y-2 p-3 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-white dark:bg-zinc-900/40">
|
|
<div className="flex items-center justify-between">
|
|
<Label className="text-xs font-semibold text-zinc-700 dark:text-zinc-300 flex items-center gap-1.5">
|
|
<Palette className="size-3.5 text-zinc-500" />
|
|
<span>Warna Event (colorId)</span>
|
|
</Label>
|
|
<span className="text-[11px] font-medium text-zinc-500 dark:text-zinc-400">
|
|
{selectedColorInfo
|
|
? `${selectedColorInfo.id} - ${selectedColorInfo.name}`
|
|
: 'Default Kalender'}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2 pt-1">
|
|
<button
|
|
type="button"
|
|
onClick={() => setColorId('')}
|
|
disabled={loading}
|
|
title="Default Kalender"
|
|
className={`h-6 px-2 rounded-full text-[11px] font-medium border transition-all flex items-center gap-1 ${colorId === ''
|
|
? 'border-zinc-900 dark:border-zinc-100 bg-zinc-900 text-white dark:bg-zinc-100 dark:text-zinc-900'
|
|
: 'border-zinc-300 dark:border-zinc-700 text-zinc-600 dark:text-zinc-400 hover:bg-zinc-100 dark:hover:bg-zinc-800'
|
|
}`}
|
|
>
|
|
Default
|
|
</button>
|
|
|
|
{GOOGLE_EVENT_COLORS.map((color) => {
|
|
const isSelected = colorId === color.id;
|
|
return (
|
|
<button
|
|
key={color.id}
|
|
type="button"
|
|
onClick={() => setColorId(color.id)}
|
|
disabled={loading}
|
|
title={`${color.id}: ${color.name}`}
|
|
style={{ backgroundColor: color.hex }}
|
|
className={`size-6 rounded-full flex items-center justify-center transition-transform ${isSelected
|
|
? 'ring-2 ring-offset-2 ring-zinc-900 dark:ring-zinc-100 dark:ring-offset-zinc-950 scale-110'
|
|
: 'opacity-85 hover:opacity-100 hover:scale-105'
|
|
}`}
|
|
>
|
|
{isSelected && <Check className="size-3.5 text-white stroke-[3]" />}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Add Guest (attendees) */}
|
|
<div className="space-y-2 p-3 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-white dark:bg-zinc-900/40">
|
|
<Label htmlFor="event-guest-input" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300 flex items-center gap-1.5">
|
|
<Users className="size-3.5 text-zinc-500" />
|
|
<span>Tambah Tamu / Peserta (Attendees)</span>
|
|
</Label>
|
|
|
|
<div className="flex items-center gap-2">
|
|
<Input
|
|
id="event-guest-input"
|
|
type="email"
|
|
placeholder="Ketik email tamu lalu tekan Enter / Tambah..."
|
|
value={guestInput}
|
|
onChange={(e) => setGuestInput(e.target.value)}
|
|
onKeyDown={handleGuestKeyDown}
|
|
disabled={loading}
|
|
className="h-8 text-xs flex-1"
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={handleAddGuest}
|
|
disabled={loading || !guestInput.trim()}
|
|
className="h-8 px-2.5 text-xs shrink-0"
|
|
>
|
|
<Plus className="size-3.5 mr-1" />
|
|
Tambah
|
|
</Button>
|
|
</div>
|
|
|
|
{attendees.length > 0 && (
|
|
<div className="flex flex-wrap gap-1.5 pt-1">
|
|
{attendees.map((email) => (
|
|
<Badge
|
|
key={email}
|
|
variant="secondary"
|
|
className="text-[11px] py-0.5 pl-2 pr-1 gap-1 font-normal bg-zinc-100 dark:bg-zinc-800 text-zinc-800 dark:text-zinc-200"
|
|
>
|
|
<span className="truncate max-w-[210px]">{email}</span>
|
|
<button
|
|
type="button"
|
|
onClick={() => handleRemoveGuest(email)}
|
|
disabled={loading}
|
|
className="p-0.5 rounded-full hover:bg-zinc-300 dark:hover:bg-zinc-700 text-zinc-500 hover:text-zinc-900 dark:hover:text-zinc-100"
|
|
title={`Hapus ${email}`}
|
|
>
|
|
<X className="size-3" />
|
|
</button>
|
|
</Badge>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Google Meet (conferenceData / hangoutLink) */}
|
|
<div className="space-y-3 p-3 rounded-lg border border-zinc-200 dark:border-zinc-800 bg-white dark:bg-zinc-900/40">
|
|
<div className="flex items-center justify-between gap-2">
|
|
<div className="space-y-0.5">
|
|
<Label htmlFor="meet-toggle" className="text-xs font-semibold text-zinc-800 dark:text-zinc-200 flex items-center gap-1.5 cursor-pointer">
|
|
<Video className="size-3.5 text-sky-600 dark:text-sky-400" />
|
|
<span>Generate Link Google Meet Otomatis</span>
|
|
</Label>
|
|
<p className="text-[11px] text-zinc-500">
|
|
Mengirim <code className="text-[10px]">conferenceData.createRequest</code> (<code className="text-[10px]">conferenceDataVersion: 1</code>)
|
|
</p>
|
|
</div>
|
|
<Switch
|
|
id="meet-toggle"
|
|
checked={generateMeet}
|
|
onCheckedChange={setGenerateMeet}
|
|
disabled={loading}
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-1.5 pt-1 border-t border-zinc-100 dark:border-zinc-800/80">
|
|
<Label htmlFor="manual-meet-link" className="text-[11px] font-medium text-zinc-600 dark:text-zinc-400">
|
|
Link Google Meet Manual (Fallback / Custom Meet URL)
|
|
</Label>
|
|
<Input
|
|
id="manual-meet-link"
|
|
type="url"
|
|
placeholder="https://meet.google.com/xxx-xxxx-xxx (opsional)"
|
|
value={manualMeetLink}
|
|
onChange={(e) => setManualMeetLink(e.target.value)}
|
|
disabled={loading}
|
|
className="h-8 text-xs"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Location */}
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="event-location" className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Lokasi / Ruangan (Opsional)
|
|
</Label>
|
|
<Input
|
|
id="event-location"
|
|
placeholder="misal: Ruang Meeting Lantai 2"
|
|
value={location}
|
|
onChange={(e) => setLocation(e.target.value)}
|
|
disabled={loading}
|
|
className="h-9 text-sm"
|
|
/>
|
|
</div>
|
|
|
|
{/* Description */}
|
|
<div className="space-y-1.5">
|
|
<Label className="text-xs font-semibold text-zinc-700 dark:text-zinc-300">
|
|
Deskripsi / Catatan (Opsional)
|
|
</Label>
|
|
<DescriptionRichEditor
|
|
value={description}
|
|
onChange={setDescription}
|
|
disabled={loading}
|
|
placeholder="Add description"
|
|
/>
|
|
</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-zinc-900 hover:bg-zinc-800 text-white dark:bg-zinc-100 dark:hover:bg-zinc-200 dark:text-zinc-900"
|
|
>
|
|
{loading && <Loader2 className="size-3.5 mr-1.5 animate-spin" />}
|
|
{isEdit ? 'Simpan Perubahan' : 'Buat Event'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
export default function EventFormModal({
|
|
open,
|
|
onOpenChange,
|
|
event,
|
|
initialDate,
|
|
defaultCalendarId,
|
|
calendars,
|
|
onSuccess,
|
|
}: EventFormModalProps) {
|
|
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 && (
|
|
<EventFormInner
|
|
key={`${event?.id || 'new'}-${initialDate || ''}`}
|
|
event={event}
|
|
initialDate={initialDate}
|
|
defaultCalendarId={defaultCalendarId}
|
|
calendars={calendars}
|
|
onClose={() => onOpenChange(false)}
|
|
onSuccess={onSuccess}
|
|
/>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|