"use client"; import React, { useEffect, useRef, useState } from "react"; import { useForm, Controller } from "react-hook-form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Card } from "@/components/ui/card"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { useParams, useRouter } from "next/navigation"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Checkbox } from "@/components/ui/checkbox"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import JoditEditor from "jodit-react"; import { createTask, getTask } from "@/service/task"; import { getContestById } from "@/service/contest/contest"; import page from "@/app/[locale]/page"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; import { CalendarIcon } from "lucide-react"; import { format, parseISO } from "date-fns"; import { Calendar } from "@/components/ui/calendar"; import { DateRange } from "react-day-picker"; const contestSchema = z.object({ theme: z.string().min(1, { message: "Judul diperlukan" }), hastagCode: z.string().min(1, { message: "Judul diperlukan" }), description: z.string().min(2, { message: "Narasi Penugasan harus lebih dari 2 karakter.", }), }); export type taskDetail = { id: number; theme: string; hastagCode: string; assignedToTopLevel: string; assignmentType: { id: number; name: string; }; assignmentMainType: { id: number; name: string; }; createdAt: string; targetOutput: string; targetParticipantTopLevel: string; description: string; is_active: string; }; export default function FormContestDetail() { const MySwal = withReactContent(Swal); const router = useRouter(); const editor = useRef(null); type ContestSchema = z.infer; const { id } = useParams() as { id: string }; console.log(id); // State for various form fields const [taskOutput, setTaskOutput] = useState({ all: false, video: false, audio: false, image: false, text: false, }); // const [assignmentType, setAssignmentType] = useState("mediahub"); // const [assignmentCategory, setAssignmentCategory] = useState("publication"); const [mainType, setMainType] = useState("1"); const [taskType, setTaskType] = useState("atensi-khusus"); const [broadcastType, setBroadcastType] = useState(""); // untuk Tipe Penugasan const [type, setType] = useState("1"); const [selectedTarget, setSelectedTarget] = useState("all"); const [detail, setDetail] = useState(); const [refresh] = useState(false); const [date, setDate] = useState(); const [platformTypeVisible, setPlatformTypeVisible] = useState(false); const [unitSelection, setUnitSelection] = useState({ allUnit: false, mabes: false, polda: false, polres: false, }); const { control, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(contestSchema), }); // const handleRadioChange = (event: React.ChangeEvent) => { // const selectedValue = Number(event.target.value); // setMainType(selectedValue); // setPlatformTypeVisible(selectedValue === 2); // }; useEffect(() => { async function initState() { if (id) { const response = await getContestById(id); const details = response.data?.data; setDetail(details); if (details?.createdAt) { const parsedDate = parseISO(details.createdAt); setDate({ from: parsedDate, to: parsedDate }); } } } initState(); }, [id, refresh]); useEffect(() => { if (detail?.targetOutput) { const outputSet = new Set(detail.targetOutput.split(",").map(Number)); // Membagi string ke dalam array dan mengonversi ke nomor setTaskOutput({ all: outputSet.has(0), video: outputSet.has(2), audio: outputSet.has(4), image: outputSet.has(1), text: outputSet.has(3), }); } }, [detail?.targetOutput]); useEffect(() => { if (detail?.targetParticipantTopLevel) { const outputSet = new Set( detail.targetParticipantTopLevel.split(",").map(Number) ); setUnitSelection({ allUnit: outputSet.has(0), mabes: outputSet.has(1), polda: outputSet.has(2), polres: outputSet.has(3), }); } }, [detail?.targetParticipantTopLevel]); const save = async (data: ContestSchema) => { const fileTypeMapping = { all: "1", video: "2", audio: "3", image: "4", text: "5", }; const selectedOutputs = Object.keys(taskOutput) .filter((key) => taskOutput[key as keyof typeof taskOutput]) // Ambil hanya yang `true` .map((key) => fileTypeMapping[key as keyof typeof fileTypeMapping]) // Konversi ke nilai string .join(","); const requestData = { ...data, // assignmentType, // assignmentCategory, target: selectedTarget, unitSelection, assignedToRole: "3", taskType: taskType, broadcastType: broadcastType, assignmentMainTypeId: mainType, assignmentPurpose: "1", assignmentTypeId: type, fileTypeOutput: selectedOutputs, id: null, description: data.description, platformType: "", theme: data.theme, }; // const response = await createTask(requestData); console.log("Form Data Submitted:", requestData); // console.log("response", response); MySwal.fire({ title: "Sukses", text: "Data berhasil disimpan.", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then(() => { router.push("/en/shared/contest"); }); }; const onSubmit = (data: ContestSchema) => { 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); } }); }; return (

Form Contest

( )} /> {errors.hastagCode?.message && (

{errors.hastagCode.message}

)}
{/* Input Title */}
( )} /> {errors.theme?.message && (

{errors.theme.message}

)}
{Object.keys(taskOutput).map((key) => (
setTaskOutput({ ...taskOutput, [key]: value }) } />
))}
{Object.keys(unitSelection).map((key) => (
setUnitSelection({ ...unitSelection, [key]: value }) } />
))}
( )} /> {errors.description?.message && (

{errors.description.message}

)}
); }