import { FC, useEffect, useRef, useState } from "react"; import FullCalendar from "@fullcalendar/react"; import dayGridPlugin from "@fullcalendar/daygrid"; import { ChevronLeftIcon, ChevronRightIcon, RoundedButton, } from "./Datepicker/components/utils"; import { dayDate, monthYearDate } from "@/lib/utils/date"; import { X } from "lucide-react"; interface PopupPos { x: number; y: number; } export const CalenderGoogle: FC<{}> = () => { const calendarRef = useRef(null); const [popupPos, setPopupPos] = useState({ x: 0, y: 0 }); const [showPopup, setShowPopup] = useState(false); const [listMore, setListMore] = useState([] as any[]); const [now, setNow] = useState(new Date()); const [detailDate, setDetailDate] = useState(new Date()); const handlePrev = () => { const calendarApi = calendarRef.current?.getApi(); calendarApi?.prev(); setNow(calendarApi?.getDate() || new Date()); }; const handleNext = () => { const calendarApi = calendarRef.current?.getApi(); calendarApi?.next(); setNow(calendarApi?.getDate() || new Date()); }; const handleToday = () => { const calendarApi = calendarRef.current?.getApi(); calendarApi?.today(); }; const [events, setEvents] = useState([ { title: "Meeting", start: "2025-02-25T10:00:00", end: "2025-02-25T11:00:00", allDay: true, color: "#2196f3", extendedProps: { id: 1, }, }, { title: "Lunch Break", start: "2025-02-26T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 2, }, }, { title: "Lunch Break", start: "2025-02-24T12:00:00", end: "2025-02-28T12:00:00", allDay: true, extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, { title: "Lunch Break", start: "2025-02-22T12:00:00", end: "2025-02-28T12:00:00", allDay: true, color: "#ff5722", extendedProps: { id: 3, }, }, ]); return (
{monthYearDate(now)}
{ console.log(info.allSegs); const clickedDate = info.date; setDetailDate(clickedDate); console.log("Tanggal yang diklik:", clickedDate); const allEvents = info.allSegs.map( (seg: any) => seg?.eventRange?.def?.extendedProps || seg?.eventRange?.def ); const linkRect = info.jsEvent.target.getBoundingClientRect(); setPopupPos({ x: linkRect.left + window.scrollX, y: linkRect.top + window.scrollY, }); const list = [] as any[]; info.allSegs.map((info: any) => { const event = info?.event; list.push(event); }); setListMore(list); setShowPopup(true); return "none"; // Mencegah FullCalendar pindah ke tampilan hari }} // Ketika klik event eventClick={(info: any) => { const event = info?.event?.extendedProps; // setSelectedEvent(info.event); // setShowEventPopup(true); }} /> {showPopup && ( setShowPopup(false)} popupPos={popupPos} >
{dayDate(detailDate)}
setShowPopup(false)} >
{listMore.map((evt, idx) => (
{evt?.title}
))}
)}
); }; interface CenteredPopupProps { children: React.ReactNode; onClose: () => void; popupPos: PopupPos; } const CenteredPopup: React.FC = ({ children, onClose, popupPos, }) => { const popupRef = useRef(null); const [adjustedPos, setAdjustedPos] = useState({ top: popupPos.y, left: popupPos.x, }); const [classname, setClassname] = useState(null as any); useEffect(() => { setClassname(css` top: ${popupPos.y}px; left: ${popupPos.x}px; transform: translate(-50%, -50%); `); }, []); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( popupRef.current && !popupRef.current.contains(event.target as Node) ) { onClose(); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [onClose]); useEffect(() => { if (popupRef.current) { const rect = popupRef.current.getBoundingClientRect(); let newY = popupPos.y; let newX = popupPos.x; // Jika bagian bawah popup melebihi tinggi viewport, geser ke atas if (newY + rect.height > window.innerHeight) { newY = window.innerHeight - rect.height - 10; // beri margin 10px console.log("CEKKK"); setClassname(css` bottom: 0; left: ${popupPos.x}px; transform: translateX(-50%); `); } } }, [popupPos]); return (
{children}
); };