"use client"; import SiteBreadcrumb from "@/components/site-breadcrumb"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Link, useRouter } from "@/i18n/routing"; import { CalendarIcon } from "lucide-react"; import React, { useEffect, useRef, useState } from "react"; import { cn } from "@/lib/utils"; import { format } from "date-fns"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { Checkbox } from "@/components/ui/checkbox"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { getWeeklyPlanList, savePlanning, } from "@/service/agenda-setting/agenda-setting"; import { id } from "date-fns/locale"; import { close, error, loading } from "@/config/swal"; import { getOnlyDate } from "@/utils/globals"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { getUserLevelForAssignments } from "@/service/task"; import dynamic from "next/dynamic"; const FormSchema = z.object({ date: z.date({ required_error: "Required", }), title: z.string({ required_error: "Required", }), detail: z.string({ required_error: "Required", }), output: z.array(z.string()).refine((value) => value.some((item) => item), { message: "Required", }), unit: z.array(z.string()).refine((value) => value.some((item) => item), { message: "Required", }), type: z.string({ required_error: "Required", }), parentId: z.string({ required_error: "Required", }), media: z.array(z.string()).refine((value) => value.some((item) => item), { message: "Required", }), }); const items = [ { id: "2", label: "Audio Visual", }, { id: "1", label: "Foto", }, { id: "4", label: "Audio", }, { id: "3", label: "Text", }, ]; const medias = [ { id: "5", label: "X", }, { id: "1", label: "Facebook", }, { id: "2", label: "Instagram", }, { id: "3", label: "Youtube", }, { id: "4", label: "Tiktok", }, ]; const units = [ { id: "1", label: "Mabes Polri", }, { id: "2", label: "Polda", }, { id: "3", label: "Polres", }, { id: "4", label: "Satker", }, ]; const CustomEditor = dynamic( () => { return import("@/components/editor/custom-editor"); }, { ssr: false } ); export default function CreateDaily() { const MySwal = withReactContent(Swal); const [weeklyList, setWeeklyList] = useState(); const router = useRouter(); const [selected, setSelected] = useState<{ [key: string]: boolean }>({}); const [selectAll, setSelectAll] = useState<{ [key: string]: boolean }>({}); const [listDest, setListDest] = useState([]); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { unit: [], output: [], detail: "", media: [], }, }); const editor = useRef(null); useEffect(() => { getWeeklyPlanning(); }, []); async function getWeeklyPlanning() { const res = await getWeeklyPlanList(new Date().getDate(), 2); if (res?.data !== null) { const rawUser = res?.data?.data; const optionArr = rawUser.map((option: any) => ({ id: option.id, label: option.title, value: String(option.id), })); setWeeklyList(optionArr); } } const onSubmit = async (data: z.infer) => { if (form.getValues("detail") == "") { form.setError("detail", { type: "manual", message: "Required", }); } else { MySwal.fire({ title: "Simpan Data", text: "Apakah Anda yakin ingin menyimpan data ini?", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Simpan", }).then((result) => { if (result.isConfirmed) { save(data); } }); } }; const save = async (data: z.infer) => { const getSelectedString = () => { return Object.keys(selected) .filter((key) => selected[key]) .join(", "); }; loading(); const reqData = { planningTypeId: 2, time: "1", title: data.title, assignmentTypeId: data.type, //string description: data.detail, assignedToLevel: unit?.join(","), //string assignmentPurpose: getSelectedString(), //string fileTypeOutput: data.output?.join(","), //string status: "Open", date: getOnlyDate(data.date), platformType: data.media?.join(","), parentId: Number(data.parentId), //number assignmentMainTypeId: 2, }; // console.log("req =>", reqData); const response = await savePlanning(reqData); if (response?.error) { error(response?.message); return false; } close(); MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { router.push("/curator/task-plan/medsos-mediahub"); } }); }; const output = form.watch("output"); const media = form.watch("media"); const unit = form.watch("unit"); const isAllChecked = output && items.every((item) => output.includes(item.id)); const isAllMediaChecked = media && medias.every((item) => media.includes(item.id)); const isAllUnitChecked = unit && units.every((item) => unit.includes(item.id)); useEffect(() => { async function initState() { const response = await getUserLevelForAssignments(); setListDest(response?.data?.data.list); } initState(); }, []); const handleParentChange = (listId: string) => { setSelected((prev) => ({ ...prev, [listId]: !prev[listId], })); }; const handleSelectAllPolres = (listId: string, isChecked: boolean) => { setSelectAll((prev) => ({ ...prev, [listId]: isChecked, })); setSelected((prev) => { const updatedState = { ...prev }; listDest .find((list: any) => list.id === listId) ?.subDestination.forEach((subDes: any) => { updatedState[`${listId}${subDes.id}`] = isChecked; }); return updatedState; }); }; const handleChildChange = (childId: string) => { setSelected((prev) => ({ ...prev, [childId]: !prev[childId], })); }; return (
Bulanan Mingguan
Harian

Perencanaan MediaHub

( Judul Perencanaan )} /> (
Output Tugas
{ if (checked) { form.setValue( "output", items.map((item) => item.id) ); } else { form.setValue("output", []); } }} />
{items.map((item) => ( { return ( { form.setValue( "output", checked ? [...(output ?? []), item.id] : (output ?? []).filter((value) => value !== item.id) ); }} /> {item.label} ); }} /> ))}
)} /> (
Pelaksana Tugas
{ if (checked) { form.setValue( "unit", units.map((item) => item.id) ); } else { form.setValue("unit", []); } }} />
{units.map((item) => ( { return ( { form.setValue( "unit", checked ? [...(unit ?? []), item.id] : (unit ?? []).filter((value) => value !== item.id) ); }} /> {item.label} ); }} /> ))} {`[Kustom]`} Daftar Wilayah Polda dan Polres
{listDest?.map((list: any) => (
handleParentChange(list.id) } disabled={unit?.includes("2") ?? false} />
handleSelectAllPolres( list.id, Boolean(e) ) } disabled={unit?.includes("3") ?? false} />
{list.subDestination.map( (subDes: any) => (
handleChildChange( `${list.id}${subDes.id}` ) } disabled={unit?.includes("3") ?? false} />
) )}
))}
)} /> (
Jenis Platform
{ if (checked) { form.setValue( "media", medias.map((item) => item.id) ); } else { form.setValue("media", []); } }} />
{medias.map((item) => ( { return ( { form.setValue( "media", checked ? [...(media ?? []), item.id] : (media ?? []).filter( (value) => value !== item.id ) ); }} /> {item.label} ); }} /> ))}
)} /> ( Jenis Penugasan Publikasi Amplifikasi Kontra )} /> ( Perencanaan Mingguan )} /> ( Pilih Tanggal )} /> ( Detail Perencanaan )} />
); }