"use client"; import dayjs from "dayjs"; import { useEffect, useRef, useState } from "react"; import utc from "dayjs/plugin/utc"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { close, loading } from "@/config/swal"; import { useSearchParams } from "next/navigation"; import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; import "swiper/css/free-mode"; import "swiper/css/navigation"; import "swiper/css/thumbs"; import Image from "next/image"; import { FreeMode, Navigation, Thumbs } from "swiper/modules"; import { Swiper as SwiperType } from "swiper/types"; import { getMonthlyPlanList, getPlanningDailyByTypeId, getPlanningDailyMedsosByPlatform, getPlanningMonthlyPerSocmed, getWeeklyPlanList, getWeeklyPlanListByParentId, } from "@/service/agenda-setting/agenda-setting"; import TaskPlanMediahubTable from "@/app/[locale]/(protected)/curator/task-plan/mediahub/components/table"; import weekday from "dayjs/plugin/weekday"; import { Icon } from "@iconify/react/dist/iconify.js"; import { FacebookIcon, InstagramIcon, TiktokIcon, XIcon, YoutubeIcon, } from "@/components/icon"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import TaskPlanningSocialMediaTable from "./social-media-modal/table"; const WEEKDAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; const TODAY = dayjs().format("YYYY-MM-DD"); export default function SingleViewSocialMediaTable() { const params = useSearchParams(); const [selectedMonthItem, setSelectedMonthItem] = useState< string | undefined >(undefined); const [selectedWeekly, setSelectedWeekly] = useState( undefined ); const [selectedDate, setSelectedDate] = useState( new Date(TODAY).getDate() ); const [nowDate, setNowDate] = useState(TODAY); const INITIAL_YEAR = dayjs().format("YYYY"); const INITIAL_MONTH = dayjs().format("M"); const size = 20; const page: string | undefined | null = params?.get("page"); const id: string | undefined | null = params?.get("id"); const pages = page ? Number(page) - 1 : 0; const no = (size || 10) * pages; const [selectedMonth, setSelectedMonth] = useState( dayjs(new Date(parseInt(INITIAL_YEAR), parseInt(INITIAL_MONTH) - 1, 1)) ); const [selectedMonthTitle, setSelectedMonthTitle] = useState(""); const [days, setDays] = useState([]); const weekday = require("dayjs/plugin/weekday"); const weekOfYear = require("dayjs/plugin/weekOfYear"); const [planningData, setPlanningData] = useState([]); dayjs.extend(utc); dayjs.extend(weekday); dayjs.extend(weekOfYear); let currentMonthDays: any; let previousMonthDays: any; let nextMonthDays: any; const [monthlyList, setMonthlyList] = useState([]); const [weeklyList, setWeeklyList] = useState([]); const [getData, setGetData] = useState([]); const [isOpen, setIsOpen] = useState(false); const [selectedDateNow, setSelectedDateNow] = useState(""); const [selectedPlatform, setSelectedPlatform] = useState(""); useEffect(() => { createCalendar("START"); }, []); function createDaysForCurrentMonth( year: string, month: string, daysInMonth: number ) { const days: any = []; for (let day = 1; day <= daysInMonth; day++) { const date = dayjs( new Date(parseInt(year), parseInt(month) - 1, day) ).format("YYYY-MM-DD"); days.push({ date, isCurrentMonth: true, isToday: date === TODAY, }); } return days; } async function getMonthlyPlanning(dates: number) { const res = await getMonthlyPlanList(dates, 2); setMonthlyList(res?.data?.data); } async function getWeeklyPlanning( id: string | undefined, date: number | undefined ) { if (id) { const res = await getWeeklyPlanListByParentId(id, 2); setWeeklyList(res?.data?.data); } else { const res = await getWeeklyPlanList(date, 2, true); setWeeklyList(res?.data?.data); } } function createCalendar( year: string = INITIAL_YEAR, month: string = INITIAL_MONTH ) { year = year === "START" ? INITIAL_YEAR : year; fetchData(month, year); setSelectedMonthTitle( dayjs(new Date(parseInt(year), parseInt(month) - 1)) .utc() .local() .format("MMMM YYYY") ); currentMonthDays = createDaysForCurrentMonth( year, month, dayjs(`${year}-${month}-01`).daysInMonth() ); getMonthlyPlanning(year ? currentMonthDays[0]?.date : TODAY); getWeeklyPlanning(undefined, year ? currentMonthDays[0]?.date : TODAY); previousMonthDays = createDaysForPreviousMonth(year, month); nextMonthDays = createDaysForNextMonth(year, month); const listDay = [ ...previousMonthDays, ...currentMonthDays, ...nextMonthDays, ]; setDays(listDay); } function getWeekday(date: string | undefined) { return dayjs(date).weekday(); } function createDaysForPreviousMonth(year: string, month: string) { const firstDayOfTheMonthWeekday = getWeekday(currentMonthDays[0]?.date); const previousMonth = dayjs(`${year}-${month}-01`).subtract(1, "month"); // Cover first day of the month being sunday (firstDayOfTheMonthWeekday == 0) const visibleNumberOfDaysFromPreviousMonth = firstDayOfTheMonthWeekday ? firstDayOfTheMonthWeekday - 1 : 6; const previousMonthLastMondayDayOfMonth = dayjs(currentMonthDays[0].date) .subtract(visibleNumberOfDaysFromPreviousMonth, "day") .date(); return [...new Array(visibleNumberOfDaysFromPreviousMonth)].map( (day, index) => ({ date: dayjs( `${previousMonth.year()}-${previousMonth.month() + 1}-${ previousMonthLastMondayDayOfMonth + index }` ).format("YYYY-MM-DD"), dayOfMonth: previousMonthLastMondayDayOfMonth + index, isCurrentMonth: false, }) ); } function createDaysForNextMonth(year: string, month: string) { const lastDayOfTheMonthWeekday = getWeekday( `${year}-${month}-${currentMonthDays.length}` ); const nextMonth = dayjs(`${year}-${month}-01`).add(1, "month"); const visibleNumberOfDaysFromNextMonth = lastDayOfTheMonthWeekday ? 7 - lastDayOfTheMonthWeekday : lastDayOfTheMonthWeekday; return [...new Array(visibleNumberOfDaysFromNextMonth)].map( (day, index) => ({ date: dayjs( `${nextMonth.year()}-${nextMonth.month() + 1}-${index + 1}` ).format("YYYY-MM-DD"), dayOfMonth: index + 1, isCurrentMonth: false, }) ); } async function fetchData( month: string, year: string, parentId?: string | undefined ) { loading(); const res = await getPlanningMonthlyPerSocmed(month, year, 2, parentId); close(); setPlanningData(res?.data?.data); } function getPrevMonth() { const selectedMonthNew = dayjs(selectedMonth).subtract(1, "month"); createCalendar( selectedMonthNew.format("YYYY"), selectedMonthNew.format("M") ); fetchData("", selectedMonthNew?.format("YYYY-MM-DD")); setSelectedMonth(selectedMonthNew); setSelectedDate(1); } function getPresentMonth() { const selectedMonthNew = dayjs( new Date(parseInt(INITIAL_YEAR), parseInt(INITIAL_MONTH) - 1, 1) ); createCalendar( selectedMonthNew.format("YYYY"), selectedMonthNew.format("M") ); fetchData("", TODAY); setSelectedDate(Number(dayjs().format("D"))); setSelectedMonth(selectedMonthNew); } function getNextMonth() { const selectedMonthNew = dayjs(selectedMonth).add(1, "month"); createCalendar( selectedMonthNew.format("YYYY"), selectedMonthNew.format("M") ); fetchData("", selectedMonthNew?.format("YYYY-MM-DD")); setSelectedMonth(selectedMonthNew); setSelectedDate(1); } const onSelectedMonthItem = (id: string | undefined) => { // fetchData(date) setSelectedMonthItem(id); getWeeklyPlanning(id, undefined); }; const onSelectedWeekly = (id: string | undefined) => { setSelectedWeekly(id); fetchData("", String(id)); }; const removeSelection = () => { setSelectedMonthItem(undefined); setSelectedWeekly(undefined); }; const onSelectedSocmed = async (date: string, platform: string) => { setSelectedDateNow(date); setSelectedPlatform(platform); setIsOpen(true); }; const getDataForThisDate = (day: number) => { const todayData = planningData?.find((a: any) => a?.day == day); if (todayData) { return (
onSelectedSocmed(todayData.planningList[0].date, "5") } className="flex flex-row gap-5 rounded-sm bg-[#0C0705] text-white px-3 py-1 justify-center cursor-pointer" > Total: {todayData.totalTwitter} onSelectedSocmed(todayData.planningList[0].date, "1") } className="flex flex-row gap-5 rounded-sm bg-[#1877F2] text-white px-3 py-1 justify-center cursor-pointer" > Total: {todayData.totalFacebook} onSelectedSocmed(todayData.planningList[0].date, "2") } className="flex flex-row gap-5 rounded-sm bg-[#d62976] text-white px-3 py-1 justify-center cursor-pointer" > Total: {todayData.totalInstagram} onSelectedSocmed(todayData.planningList[0].date, "3") } className="flex flex-row gap-5 rounded-sm bg-[#FF0000] text-white px-3 py-1 justify-center cursor-pointer" > Total: {todayData.totalYoutube} onSelectedSocmed(todayData.planningList[0].date, "4") } className="flex flex-row gap-5 rounded-sm bg-[#000000] text-white px-3 py-1 justify-center cursor-pointer" > Total: {todayData.totalTiktok}
); } }; return (

{selectedMonthTitle}

Rencana Bulanan

{monthlyList?.length > 0 ? (
{monthlyList?.map((item: any) => (
onSelectedMonthItem(item.id)} >

{item.title}

))}
) : (
Rencana Bulanan Belum Tersedia
)}

Rencana Mingguan

{weeklyList?.length > 0 ? (
{weeklyList?.map((item: any) => ( onSelectedWeekly(item.id)} >

{item.title}

))}
) : (
Rencana Mingguan Belum Tersedia
)}
    {WEEKDAYS.map((weekday, index) => (
  1. {weekday}
  2. ))}
    {days?.map((day: any, index: number) => (
  1. {parseInt(day.date.split("-")[2])} {day.isCurrentMonth && getDataForThisDate(parseInt(day.date.split("-")[2]))}
  2. ))}
Perencanaan Medsos Harian
); }