update 15 mei
This commit is contained in:
@@ -1,246 +1,225 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, Clock, MapPin } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { CalendarDays, MapPin, Clock, ChevronLeft, ChevronRight, Calendar as CalendarIcon } from "lucide-react";
|
||||
|
||||
// --- Tipe Data Mock ---
|
||||
type Booking = {
|
||||
id: number;
|
||||
room: string;
|
||||
date: string; // Format YYYY-MM-DD
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
purpose: string;
|
||||
status: "Approved" | "Pending";
|
||||
};
|
||||
export default function CalendarViewPage() {
|
||||
const [bookings, setBookings] = useState<any[]>([]);
|
||||
const [rooms, setRooms] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// --- Data Dummy (Mock Data) ---
|
||||
const mockBookings: Booking[] = [
|
||||
{
|
||||
id: 1,
|
||||
room: "Ruang T-301",
|
||||
date: "2026-02-05",
|
||||
startTime: "07:30",
|
||||
endTime: "10:00",
|
||||
purpose: "Kuliah Sistem Kendali",
|
||||
status: "Approved",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
room: "Lab IoT",
|
||||
date: "2026-02-05", // Tanggal sama dengan atas
|
||||
startTime: "13:00",
|
||||
endTime: "15:00",
|
||||
purpose: "Praktikum Embedded System",
|
||||
status: "Approved",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
room: "Ruang T-302",
|
||||
date: "2026-02-12",
|
||||
startTime: "09:00",
|
||||
endTime: "11:30",
|
||||
purpose: "Seminar Proposal",
|
||||
status: "Pending",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
room: "Lab Kendali",
|
||||
date: "2026-02-20",
|
||||
startTime: "10:00",
|
||||
endTime: "12:00",
|
||||
purpose: "Riset Skripsi",
|
||||
status: "Approved",
|
||||
},
|
||||
];
|
||||
// --- STATE UNTUK NAVIGASI MINGGU ---
|
||||
const [startDate, setStartDate] = useState(() => {
|
||||
const d = new Date();
|
||||
d.setHours(0, 0, 0, 0);
|
||||
return d;
|
||||
});
|
||||
|
||||
export default function CalendarPage() {
|
||||
const [currentDate, setCurrentDate] = useState(new Date(2026, 1)); // Februari 2026
|
||||
const [selectedDate, setSelectedDate] = useState<string | null>(null);
|
||||
// Generate 7 Hari ke depan berdasarkan startDate
|
||||
const weekDates = Array.from({ length: 7 }).map((_, i) => {
|
||||
const d = new Date(startDate);
|
||||
d.setDate(startDate.getDate() + i);
|
||||
return d;
|
||||
});
|
||||
|
||||
// --- Helper Functions ---
|
||||
const getDaysInMonth = (year: number, month: number) => new Date(year, month + 1, 0).getDate();
|
||||
const getFirstDayOfMonth = (year: number, month: number) => new Date(year, month, 1).getDay();
|
||||
// FORMAT RENTANG TANGGAL
|
||||
const firstDay = weekDates[0].toLocaleDateString('id-ID', { day: 'numeric', month: 'short' });
|
||||
const lastDay = weekDates[6].toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' });
|
||||
const dateRangeLabel = `${firstDay} - ${lastDay}`;
|
||||
|
||||
const handlePrevMonth = () => {
|
||||
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() - 1));
|
||||
setSelectedDate(null);
|
||||
// Fungsi Navigasi
|
||||
const goToPreviousWeek = () => {
|
||||
setStartDate(prev => {
|
||||
const newDate = new Date(prev);
|
||||
newDate.setDate(prev.getDate() - 7);
|
||||
return newDate;
|
||||
});
|
||||
};
|
||||
|
||||
const handleNextMonth = () => {
|
||||
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() + 1));
|
||||
setSelectedDate(null);
|
||||
const goToNextWeek = () => {
|
||||
setStartDate(prev => {
|
||||
const newDate = new Date(prev);
|
||||
newDate.setDate(prev.getDate() + 7);
|
||||
return newDate;
|
||||
});
|
||||
};
|
||||
|
||||
const handleDateClick = (day: number) => {
|
||||
const year = currentDate.getFullYear();
|
||||
const month = String(currentDate.getMonth() + 1).padStart(2, "0");
|
||||
const dateStr = String(day).padStart(2, "0");
|
||||
const fullDate = `${year}-${month}-${dateStr}`;
|
||||
|
||||
// Toggle seleksi: Jika diklik lagi, tutup detailnya
|
||||
if (selectedDate === fullDate) {
|
||||
setSelectedDate(null);
|
||||
} else {
|
||||
setSelectedDate(fullDate);
|
||||
}
|
||||
const goToToday = () => {
|
||||
const d = new Date();
|
||||
d.setHours(0, 0, 0, 0);
|
||||
setStartDate(d);
|
||||
};
|
||||
// ------------------------------------------
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const [roomsRes, bookingsRes] = await Promise.all([
|
||||
axios.get("http://localhost:8080/api/rooms", { headers: { Authorization: `Bearer ${token}` } }),
|
||||
axios.get("http://localhost:8080/api/bookings", { headers: { Authorization: `Bearer ${token}` } })
|
||||
]);
|
||||
|
||||
setRooms(roomsRes.data.data || []);
|
||||
setBookings(bookingsRes.data.data || []);
|
||||
} catch (error) {
|
||||
console.error("Gagal memuat data", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
// Fungsi memfilter booking
|
||||
const getBookingsForCell = (roomId: number, dateObj: Date) => {
|
||||
return bookings.filter(b => {
|
||||
if (b.room_id !== roomId) return false;
|
||||
const bDate = new Date(b.start_time);
|
||||
return (
|
||||
bDate.getDate() === dateObj.getDate() &&
|
||||
bDate.getMonth() === dateObj.getMonth() &&
|
||||
bDate.getFullYear() === dateObj.getFullYear()
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// --- Generate Calendar Grid ---
|
||||
const year = currentDate.getFullYear();
|
||||
const month = currentDate.getMonth();
|
||||
const daysInMonth = getDaysInMonth(year, month);
|
||||
const firstDay = getFirstDayOfMonth(year, month);
|
||||
|
||||
// Array kosong untuk padding hari sebelum tanggal 1
|
||||
const emptyDays = Array.from({ length: firstDay }, (_, i) => i);
|
||||
// Array tanggal 1 sampai akhir bulan
|
||||
const days = Array.from({ length: daysInMonth }, (_, i) => i + 1);
|
||||
// Format Jam
|
||||
const formatTime = (isoString: string) => {
|
||||
return new Date(isoString).toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit' });
|
||||
};
|
||||
|
||||
// Nama Bulan
|
||||
const monthNames = [
|
||||
"Januari", "Februari", "Maret", "April", "Mei", "Juni",
|
||||
"Juli", "Agustus", "September", "Oktober", "November", "Desember"
|
||||
];
|
||||
|
||||
// Filter booking untuk tanggal yang dipilih
|
||||
const selectedBookings = mockBookings.filter((b) => b.date === selectedDate);
|
||||
if (loading) return <div className="p-8 text-gray-500 font-medium">Menyusun matriks jadwal...</div>;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6 lg:flex-row">
|
||||
<div className="w-full">
|
||||
|
||||
{/* --- KIRI: KALENDER --- */}
|
||||
<div className="flex-1 rounded-xl border border-stroke bg-white shadow-sm border-gray-200 dark:bg-boxdark">
|
||||
{/* Header Kalender */}
|
||||
<div className="flex items-center justify-between border-b border-stroke p-4 border-gray-200 sm:p-6">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="h-10 w-10 flex items-center justify-center rounded-full bg-yellow-50 text-yellow-600">
|
||||
<CalendarIcon size={20} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-lg font-bold text-black ">
|
||||
{monthNames[month]} {year}
|
||||
</h2>
|
||||
<p className="text-xs text-gray-500">Pilih tanggal untuk melihat detail.</p>
|
||||
</div>
|
||||
{/* HEADER & NAVIGASI */}
|
||||
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<CalendarDays size={28} />
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<button onClick={handlePrevMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
|
||||
<ChevronLeft size={20} />
|
||||
</button>
|
||||
<button onClick={handleNextMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
|
||||
<ChevronRight size={20} />
|
||||
</button>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Weekly Calendar View</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Pantau kepadatan jadwal ruangan S-CLASS.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Grid Kalender */}
|
||||
<div className="p-4 sm:p-6">
|
||||
{/* Nama Hari */}
|
||||
<div className="grid grid-cols-7 mb-2">
|
||||
{["Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"].map((d) => (
|
||||
<span key={d} className="text-center text-sm font-medium text-gray-500 py-2">
|
||||
{d}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{/* Kontrol Navigasi Minggu */}
|
||||
<div className="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm w-fit">
|
||||
<button
|
||||
onClick={goToPreviousWeek}
|
||||
className="p-2 hover:bg-gray-100 rounded-md transition-colors text-gray-600 hover:text-blue-600 shrink-0"
|
||||
title="Minggu Sebelumnya"
|
||||
>
|
||||
<ChevronLeft size={20} />
|
||||
</button>
|
||||
|
||||
<div className="w-px h-6 bg-gray-200 mx-1"></div>
|
||||
|
||||
{/* TULISAN RENTANG TANGGAL DINAMIS (Lebar Fix w-44 untuk menghilangkan warning Tailwind) */}
|
||||
<button
|
||||
onClick={goToToday}
|
||||
title="Kembali ke minggu ini"
|
||||
className="w-44 py-2 hover:bg-gray-100 rounded-md transition-colors text-sm font-semibold text-gray-700 hover:text-blue-600 flex items-center justify-center gap-2"
|
||||
>
|
||||
<CalendarIcon size={16} className="shrink-0" />
|
||||
<span className="truncate">{dateRangeLabel}</span>
|
||||
</button>
|
||||
|
||||
{/* Tanggal */}
|
||||
<div className="grid grid-cols-7 gap-2">
|
||||
{/* Render Empty Cells */}
|
||||
{emptyDays.map((_, i) => (
|
||||
<div key={`empty-${i}`} className="h-24 sm:h-32"></div>
|
||||
))}
|
||||
<div className="w-px h-6 bg-gray-200 mx-1"></div>
|
||||
|
||||
{/* Render Date Cells */}
|
||||
{days.map((day) => {
|
||||
// Format tanggal hari ini untuk pengecekan data
|
||||
const currentDayStr = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
||||
const hasBooking = mockBookings.some((b) => b.date === currentDayStr);
|
||||
const isSelected = selectedDate === currentDayStr;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={day}
|
||||
onClick={() => handleDateClick(day)}
|
||||
className={`relative flex h-24 sm:h-32 cursor-pointer flex-col justify-between rounded border p-2 transition hover:bg-gray-50 dark:hover:bg-meta-4
|
||||
${isSelected
|
||||
? "border-yellow-500 bg-yellow-50 dark:bg-slate-800"
|
||||
: "border-stroke border-gray-200 bg-white dark:bg-boxdark"
|
||||
}`}
|
||||
>
|
||||
<span className={`text-sm font-medium ${isSelected ? "text-yellow-600" : "text-black "}`}>
|
||||
{day}
|
||||
</span>
|
||||
|
||||
{/* Indikator Booking (Dot / Bar) */}
|
||||
{hasBooking && (
|
||||
<div className="flex flex-col gap-1">
|
||||
{mockBookings
|
||||
.filter(b => b.date === currentDayStr)
|
||||
.slice(0, 2) // Batasi tampilan max 2 baris di grid
|
||||
.map((b, idx) => (
|
||||
<div key={idx} className="truncate rounded-sm bg-blue-100 px-1 py-0.5 text-[10px] font-medium text-blue-700 dark:bg-blue-900 dark:text-blue-100">
|
||||
{b.startTime} - {b.room}
|
||||
</div>
|
||||
))}
|
||||
{mockBookings.filter(b => b.date === currentDayStr).length > 2 && (
|
||||
<span className="text-[10px] text-gray-400 text-center">+ Lainnya</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<button
|
||||
onClick={goToNextWeek}
|
||||
className="p-2 hover:bg-gray-100 rounded-md transition-colors text-gray-600 hover:text-blue-600 shrink-0"
|
||||
title="Minggu Depan"
|
||||
>
|
||||
<ChevronRight size={20} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* --- KANAN: DETAIL SIDEBAR --- */}
|
||||
{selectedDate && (
|
||||
<div className="w-full lg:w-80 shrink-0 animate-in fade-in slide-in-from-right duration-300">
|
||||
<div className="rounded-xl border border-stroke bg-white p-6 shadow-sm border-gray-200 dark:bg-boxdark">
|
||||
<h3 className="mb-4 text-xl font-bold text-black border-b border-stroke pb-2 border-gray-200">
|
||||
Jadwal {selectedDate}
|
||||
</h3>
|
||||
|
||||
{selectedBookings.length > 0 ? (
|
||||
<div className="flex flex-col gap-4">
|
||||
{selectedBookings.map((booking) => (
|
||||
<div key={booking.id} className="rounded-lg border border-gray-100 bg-gray-50 p-4 border-gray-200 bg-gray-50">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<span className={`px-2 py-0.5 rounded text-xs font-medium ${
|
||||
booking.status === "Approved"
|
||||
? "bg-green-100 text-green-700"
|
||||
: "bg-yellow-100 text-yellow-700"
|
||||
}`}>
|
||||
{booking.status}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<h4 className="font-bold text-black mb-1">{booking.purpose}</h4>
|
||||
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500 mt-2">
|
||||
<MapPin size={16} /> {booking.room}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500 mt-1">
|
||||
<Clock size={16} /> {booking.startTime} - {booking.endTime}
|
||||
</div>
|
||||
{/* Pembungkus Tabel */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden relative">
|
||||
<div className="overflow-x-auto">
|
||||
{/* Lebar tabel dipertahankan menggunakan min-w-[1000px] karena ini ukuran presisi khusus */}
|
||||
<table className="w-full min-w-250 text-left border-collapse">
|
||||
|
||||
{/* HEADER TABEL: Sumbu X (Tanggal) */}
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="sticky left-0 z-10 bg-gray-50 border-b border-r border-gray-200 p-4 font-bold text-gray-700 w-48 shadow-[2px_0_5px_-2px_rgba(0,0,0,0.1)]">
|
||||
<div className="flex items-center gap-2">
|
||||
<MapPin size={18} className="text-blue-500" /> Ruangan
|
||||
</div>
|
||||
</th>
|
||||
|
||||
{weekDates.map((date, idx) => (
|
||||
<th key={idx} className="border-b border-gray-200 p-4 min-w-50 text-center">
|
||||
<div className="font-bold text-gray-800">
|
||||
{date.toLocaleDateString('id-ID', { weekday: 'long' })}
|
||||
</div>
|
||||
<div className="text-xs font-medium text-gray-500 mt-0.5">
|
||||
{date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' })}
|
||||
</div>
|
||||
</th>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="py-10 text-center">
|
||||
<p className="text-gray-400 text-sm">Tidak ada jadwal pada tanggal ini.</p>
|
||||
<button className="mt-4 text-sm font-medium text-yellow-600 hover:underline">
|
||||
+ Tambah Jadwal
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
{/* BODY TABEL: Sumbu Y (Ruangan) */}
|
||||
<tbody>
|
||||
{rooms.map((room) => (
|
||||
<tr key={room.room_id} className="hover:bg-blue-50/30 transition-colors">
|
||||
|
||||
<td className="sticky left-0 z-10 bg-white border-b border-r border-gray-200 p-4 shadow-[2px_0_5px_-2px_rgba(0,0,0,0.1)] group-hover:bg-blue-50/30 transition-colors">
|
||||
<div className="font-bold text-gray-800">{room.name}</div>
|
||||
<div className="text-xs text-gray-500">{room.category}</div>
|
||||
</td>
|
||||
|
||||
{weekDates.map((date, idx) => {
|
||||
const dailyBookings = getBookingsForCell(room.room_id, date);
|
||||
|
||||
return (
|
||||
<td key={idx} className="border-b border-gray-100 border-r border-r-gray-50 p-2 align-top">
|
||||
{dailyBookings.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{dailyBookings.map((b, i) => (
|
||||
<div key={i} className={`border rounded-md p-2 shadow-sm border-l-4
|
||||
${b.status === 'Approved' ? 'bg-green-50 border-green-200 border-l-green-500' :
|
||||
b.status === 'Pending' ? 'bg-yellow-50 border-yellow-200 border-l-yellow-500' :
|
||||
'bg-blue-50 border-blue-200 border-l-blue-500'}`}>
|
||||
<p className="text-xs font-bold text-gray-800 truncate" title={b.purpose}>
|
||||
{b.purpose}
|
||||
</p>
|
||||
<div className="flex flex-wrap items-center gap-1 text-[10px] text-gray-600 font-semibold mt-1">
|
||||
<Clock size={10} />
|
||||
{formatTime(b.start_time)} - {formatTime(b.end_time)}
|
||||
<span className="ml-auto text-[9px] uppercase bg-white/60 px-1 rounded border border-gray-100">
|
||||
{b.status || 'Pending'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="h-full w-full min-h-10 flex items-center justify-center text-gray-300 text-xs">
|
||||
-
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user