"use client"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { Popover, PopoverArrow, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { CalendarIcon } from "lucide-react"; import React, { useEffect, useState } from "react"; import { format } from "date-fns"; import { cn } from "@/lib/utils"; import { Checkbox } from "@/components/ui/checkbox"; import { Icon } from "@iconify/react/dist/iconify.js"; import { detailSchedule, listSchedule, listScheduleNextPublic, listSchedulePrevPublic, listScheduleTodayPublic, searchSchedules } from "@/service/schedule/schedule"; import { usePathname, useRouter } from "@/i18n/routing"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"; import { close, loading } from "@/config/swal"; import { useTranslations } from "next-intl"; import { Input } from "@/components/ui/input"; const timeList = [ { id: "6", time: "06:00", }, { id: "7", time: "07:00", }, { id: "8", time: "08:00", }, { id: "9", time: "09:00", }, { id: "10", time: "10:00", }, { id: "11", time: "11:00", }, { id: "12", time: "12:00", }, { id: "13", time: "13:00", }, { id: "14", time: "14:00", }, { id: "15", time: "15:00", }, { id: "16", time: "16:00", }, { id: "17", time: "17:00", }, { id: "18", time: "18:00", }, { id: "19", time: "19:00", }, { id: "20", time: "20:00", }, { id: "21", time: "21:00", }, { id: "22", time: "22:00", }, { id: "23", time: "23:00", }, { id: "24", time: "24:00", }, { id: "1", time: "01:00", }, { id: "2", time: "02:00", }, { id: "3", time: "03:00", }, { id: "4", time: "04:00", }, { id: "5", time: "05:00", }, ]; const city = [ { key: 1, id: "metro-jaya", name: "Polda Metro Jaya", }, { key: 2, id: "jawa-barat", name: "Polda Jawa Barat", }, { key: 3, id: "banten", name: "Polda Banten", }, { key: 4, id: "jawa-tengah", name: "Polda Jawa Tengah", }, { key: 5, id: "daerah-istimewa-yogyakarta", name: "Polda D.I Yogyakarta", }, { key: 6, id: "jawa-timur", name: "Polda Jawa Timur", }, { key: 7, id: "aceh", name: "Polda Aceh", }, { key: 8, id: "sumatera-utara", name: "Polda Sumatera Utara", }, { key: 9, id: "sumatera-barat", name: "Polda Sumatera Barat", }, ]; const Schedule = (props: any) => { const router = useRouter(); const asPath = usePathname(); const [startDate, setStartDate] = useState(new Date()); const [dateAWeek, setDateAWeek] = useState([]); const [todayList, setTodayList] = useState([]); const [prevdayList, setPrevdayList] = useState([]); const [nextdayList, setNextdayList] = useState([]); const [schedules, setSchedules] = useState([]); const [openDialog, setOpenDialog] = useState(false); const [detail, setDetail] = useState(); const [content, setContent] = useState(); const { id } = props; const t = useTranslations("LandingPage"); const [search, setSearch] = useState(); const [scheduleSearch, setScheduleSearch] = useState(); let typingTimer: any; const doneTypingInterval = 1500; const [regionFilter, setRegionFilter] = useState([]); const [regionName, setRegionName] = useState([]); const isPolda = asPath.includes("/polda"); async function doneTyping() { if (search?.length > 2) { const resSchedule = await searchSchedules(search); setScheduleSearch(resSchedule.data?.data); } else { setScheduleSearch([]); } } const handleKeyUp = () => { clearTimeout(typingTimer); typingTimer = setTimeout(doneTyping, doneTypingInterval); }; const handleKeyDown = () => { clearTimeout(typingTimer); }; useEffect(() => { async function getDataSchedule() { const response = await detailSchedule(id); setDetail(response?.data?.data); setContent(response?.data?.data?.files); } getDataSchedule(); }, [id]); useEffect(() => { async function initState() { const group = isPolda ? asPath.split("/")[2] : regionFilter?.join(","); const resSchedule = await listSchedule(group); setSchedules(resSchedule.data?.data); console.log(resSchedule); setDateAWeek(dateList); getDataByDate(); // let today = new Date(); // if (!dateList.includes(today.getDate())){ // changeNextWeek(); // } } initState(); console.log("Filter ::", regionFilter); }, [regionName]); useEffect(() => { async function initState() { getDataByDate(); // const group = isPolda ? asPath.split("/")[2] : regionFilter?.join(","); const resSchedule = await listSchedule(); setSchedules(resSchedule.data?.data); console.log(resSchedule); setDateAWeek(dateList); } initState(); }, []); async function getDataByDate() { const resToday = await listScheduleTodayPublic(); const today = resToday.data?.data; setTodayList(today); const resNext = await listScheduleNextPublic(); const next = resNext.data?.data; setNextdayList(next); const resPrev = await listSchedulePrevPublic(); const prev = resPrev.data?.data; setPrevdayList(prev); } const curr = new Date(); const startDays = (curr.getDay() + 7 - 1) % 7; curr.setDate(curr.getDate() - startDays); const dateListInit = []; curr.setDate(curr.getDate() - curr.getDay() + 1); for (let i = 0; i < 7; i++) { dateListInit.push(new Date(curr).toISOString().slice(0, 10)); curr.setDate(curr.getDate() + 1); } const [dateList, setDateList] = useState(dateListInit); const handleChangeDate = (date: Date | undefined) => { setStartDate(date); const dateListTemp = []; const curr = date; if (curr) { const startDays = (curr.getDay() + 7 - 1) % 7; curr.setDate(curr.getDate() - startDays); curr.setDate(curr.getDate() - curr.getDay() + 1); for (let i = 0; i < 7; i++) { dateListTemp.push(new Date(curr).toISOString().slice(0, 10)); curr.setDate(curr.getDate() + 1); } console.log("Change Date :", dateListTemp); setDateList(dateListTemp); setDateAWeek(dateListTemp); } }; function getLastWeek(today: Date | undefined) { if (today) { return new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7); } } function getNextWeek(today: Date | undefined) { if (today) { return new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7); } } const changeNextWeek = () => { console.log("Today :", startDate); const dayInNextWeek = getNextWeek(startDate); console.log("Next week :", dayInNextWeek); const dateListTemp = []; const curr = dayInNextWeek; if (curr) { const startDays = (curr.getDay() + 7 - 1) % 7; curr.setDate(curr.getDate() - startDays); curr.setDate(curr.getDate() - curr.getDay() + 1); for (let i = 0; i < 7; i++) { const newDate = new Date(curr); if (i == 0) { setStartDate(newDate); } const dateFormatter = Intl.DateTimeFormat("sv-SE"); dateListTemp.push(dateFormatter.format(newDate)); curr.setDate(curr.getDate() + 1); } console.log(dateListTemp); setDateList(dateListTemp); setDateAWeek(dateListTemp); } }; const changePrevWeek = () => { console.log(startDate); const dayInPrevWeek = getLastWeek(startDate); console.log("Prev week :", dayInPrevWeek); console.log(startDate); const dateListTemp = []; const curr = dayInPrevWeek; if (curr) { const startDays = (curr.getDay() + 7 - 1) % 7; curr.setDate(curr.getDate() - startDays); curr.setDate(curr.getDate() - curr.getDay() + 1); for (let i = 0; i < 7; i++) { const newDate = new Date(curr); if (i == 0) { setStartDate(newDate); } const dateFormatter = Intl.DateTimeFormat("sv-SE"); dateListTemp.push(dateFormatter.format(newDate)); curr.setDate(curr.getDate() + 1); } console.log(dateListTemp); setDateList(dateListTemp); setDateAWeek(dateListTemp); } }; const getItem = async (itemFound: any) => { loading(); const response = await detailSchedule(itemFound?.id); setDetail(response?.data?.data); setContent(response?.data?.data?.files); console.log("item Found", itemFound); close(); setOpenDialog(true); }; const handleRegionFilter = (e: any) => { let regions = [...regionFilter]; if (e.target.checked) { regions = [...regionFilter, e.target.value]; } else { regions.splice(regionFilter.indexOf(e.target.value), 1); } console.log(regions); setRegionFilter(regions); }; const getListDataCity = () => { let filteredReg: any = []; let regName = ""; for (const element of regionFilter) { for (const element_ of city) { if (element == element_.id) { regName = element_.name; filteredReg = [ ...filteredReg, { id: element_.id, key: element_.key, name: regName, }, ]; } } } console.log(filteredReg); setRegionName(filteredReg); filteredReg = []; }; const doFilter = () => { getListDataCity(); }; const deleteFilterhandler = (filterId: any) => { console.log("hapus", filterId); const deletedReg = regionName.filter((list: any) => list.id !== filterId); const filtered = regionFilter.filter((list: any) => list !== filterId); setRegionName(deletedReg); setRegionFilter(filtered); }; function setItemSchedule(id: string, date: string) { const itemFound: any = schedules?.filter((s: any) => s.dateInRange.includes(date) && s.timeIndex.split(",").includes(id)); if (itemFound?.length > 0) { if (itemFound?.length == 1) { return ( { getItem(itemFound[0]); }} >

{itemFound[0]?.title}

{itemFound[0].isYoutube == true ?

LIVE

: ""} {/*

{itemFound[0].address}

*/}
); } // for (let i = 0; i < itemFound.length; i++) { // const item = itemFound[i]; // } return (

{`${itemFound?.length} Jadwal Bersamaan`}

Lihat Jadwal {itemFound?.map((list: any) => ( { getItem(itemFound[0]); }} >

{list.title}

{list.isYoutube == true ?

LIVE

: ""} {/*

{list.address}

*/}
))}
); } } return ( <> {/* Awal Komponen Kiri */}
{ handleChangeDate(e); }} initialFocus />
Filter

Filter

Region Filter

{city?.map((list) => (
{" "}

{list?.name}

))}
{regionName?.map((list: any) => (
))}
{timeList.map((times) => ( ))}
Time Table changePrevWeek()} className="cursor-pointer h-fit self-center bottom-0"> {/* {" "} */}

{dateAWeek[0]?.split("-")[2]}

Monday

{dateAWeek[1]?.split("-")[2]}
Tuesday
{dateAWeek[2]?.split("-")[2]}
Wednesday
{dateAWeek[3]?.split("-")[2]}
Thursday
{dateAWeek[4]?.split("-")[2]}
Friday
{dateAWeek[5]?.split("-")[2]}
Saturday
changeNextWeek()} className={`text-center border cursor-pointer border-r-0 border-gray-100 dark:border-gray-700 py-6 min-w-[100px] ${ new Date().toISOString().slice(0, 10) == dateAWeek[6] ? "bg-[#BE0106] text-white rounded-lg" : "" }`} >

{dateAWeek[6]?.split("-")[2]}

Sunday

{/* */}
changeNextWeek()} className="cursor-pointer">
{times.time} {setItemSchedule(times.id, dateList[0])} {setItemSchedule(times.id, dateList[1])} {setItemSchedule(times.id, dateList[2])} {setItemSchedule(times.id, dateList[3])} {setItemSchedule(times.id, dateList[4])} {setItemSchedule(times.id, dateList[5])} {setItemSchedule(times.id, dateList[6])}
{/* komponen Kanan */}
setSearch(e.target.value)} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} type="text" placeholder={t("titleSchedule")} className="pl-8 pr-4 py-1 w-full border rounded-full text-sm focus:outline-none" />
{/* jadwal hari ini */} {/*
Jadwal Hari Ini
{todayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
*/} Jadwal Hari ini {todayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
Jadwal Sebelumnya {prevdayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
Jadwal Selanjutnya {nextdayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
{/* jadwal sebelumnya */} {/*
Jadwal Sebelumnya
{prevdayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
*/} {/* jadwal selanjutnya */} {/*
Jadwal Selanjutnya
{nextdayList?.map((list: any) => (
{new Date(list.startDate).getDate()}

{list?.title}

{list?.startTime} - {list?.endTime} WIB

{list?.address}

Pembicara :

{list?.speakerTitle} {list?.speakerName}

))}
*/}

JADWAL / {detail?.isYoutube == true ? "LIVE STREAMING" : "DETAIL"}

{detail?.title}

{detail?.date} {detail?.startTime} - {detail?.endTime} {detail?.timezone ? detail.timezone : "WIB"}

{detail?.address}

{detail?.speakerTitle || ""} {detail?.speakerName || ""}{" "}

Close
); }; export default Schedule;