"use client"; import React, { ChangeEvent, 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 { register } from "module"; import { Switch } from "@/components/ui/switch"; import Cookies from "js-cookie"; import { createMedia, getTagsBySubCategoryId, listEnableCategory, } from "@/service/content/content"; import { detailMedia } from "@/service/curated-content/curated-content"; import { Badge } from "@/components/ui/badge"; import { MailIcon } from "lucide-react"; import dynamic from "next/dynamic"; const imageSchema = z.object({ title: z.string().min(1, { message: "Judul diperlukan" }), description: z .string() .min(2, { message: "Narasi Penugasan harus lebih dari 2 karakter." }), creatorName: z.string().min(1, { message: "Creator diperlukan" }), // tags: z.string().min(1, { message: "Judul diperlukan" }), }); type Category = { id: string; name: string; }; type Detail = { id: string; title: string; description: string; slug: string; categoryId: { id: number; name: string; }; creatorName: string; categoryName: string; thumbnailLink: string; tags: string; }; const CustomEditor = dynamic( () => { return import("@/components/editor/custom-editor"); }, { ssr: false } ); export default function FormImageUpdate() { const MySwal = withReactContent(Swal); const router = useRouter(); const { id } = useParams() as { id: string }; console.log(id); const editor = useRef(null); type ImageSchema = z.infer; const [selectedFiles, setSelectedFiles] = useState([]); const taskId = Cookies.get("taskId"); const scheduleId = Cookies.get("scheduleId"); const scheduleType = Cookies.get("scheduleType"); const [categories, setCategories] = useState([]); const [selectedCategory, setSelectedCategory] = useState(); const [tags, setTags] = useState([]); const [detail, setDetail] = useState(); const [refresh, setRefresh] = useState(false); const [selectedPublishers, setSelectedPublishers] = useState([]); const [articleBody, setArticleBody] = useState(""); const [selectedTarget, setSelectedTarget] = useState(""); const [unitSelection, setUnitSelection] = useState({ allUnit: false, mabes: false, polda: false, polres: false, }); let fileTypeId = "1"; const { control, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(imageSchema), }); // const handleKeyDown = (e: any) => { // const newTag = e.target.value.trim(); // Ambil nilai input // if (e.key === "Enter" && newTag) { // e.preventDefault(); // Hentikan submit form // if (!tags.includes(newTag)) { // setTags((prevTags) => [...prevTags, newTag]); // Tambah tag baru // setValue("tags", ""); // Kosongkan input // } // } // }; const handleRemoveTag = (index: any) => { setTags((prevTags) => prevTags.filter((_, i) => i !== index)); }; const handleImageChange = (event: ChangeEvent) => { if (event.target.files) { const files = Array.from(event.target.files); setSelectedFiles((prevImages: any) => [...prevImages, ...files]); console.log("DATAFILE::", selectedFiles); } }; const handleRemoveImage = (index: number) => { setSelectedFiles((prevImages) => prevImages.filter((_, i) => i !== index)); }; const handleCheckboxChange = (id: number) => { setSelectedPublishers((prev) => prev.includes(id) ? prev.filter((item) => item !== id) : [...prev, id] ); }; useEffect(() => { async function initState() { getCategories(); } initState(); }, []); const getCategories = async () => { try { const category = await listEnableCategory(fileTypeId); const resCategory: Category[] = category?.data?.data?.content; setCategories(resCategory); console.log("data category", resCategory); if (scheduleId && scheduleType === "3") { const findCategory = resCategory.find((o) => o.name.toLowerCase().includes("pers rilis") ); if (findCategory) { // setValue("categoryId", findCategory.id); setSelectedCategory(findCategory.id); // Set the selected category const response = await getTagsBySubCategoryId(findCategory.id); setTags(response?.data?.data); } } } catch (error) { console.error("Failed to fetch categories:", error); } }; useEffect(() => { async function initState() { if (id) { const response = await detailMedia(id); const details = response?.data?.data; setDetail(details); if (details.publishedForObject) { const publisherIds = details.publishedForObject.map( (obj: any) => obj.id ); setSelectedPublishers(publisherIds); } const matchingCategory = categories.find( (category) => category.id === details.categoryId ); if (matchingCategory) { setSelectedTarget(matchingCategory.name); } setSelectedTarget(details.categoryId); // Untuk dropdown } } initState(); }, [refresh, setValue]); const save = async (data: ImageSchema) => { const requestData = { ...data, id: detail?.id, title: data.title, description: data.description, htmlDescription: data.description, fileTypeId, categoryId: selectedTarget, subCategoryId: selectedTarget, uploadedBy: "2b7c8d83-d298-4b19-9f74-b07924506b58", statusId: "1", publishedFor: "6", creatorName: data.creatorName, tags: "siap", isYoutube: false, isInternationalMedia: false, }; const response = await createMedia(requestData); console.log("Form Data Submitted:", requestData); MySwal.fire({ title: "Sukses", text: "Data berhasil disimpan.", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then(() => { router.push("/en/contributor/content/image"); }); }; const onSubmit = (data: ImageSchema) => { 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 (
{detail !== undefined ? (

Form Konten Foto

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

{errors.title.message}

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

{errors.description.message}

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

{errors.creatorName.message}

)}
Thumbnail Gambar Utama
{detail?.tags?.split(",").map((tag, index) => ( {tag.trim()} ))}
handleCheckboxChange(5)} />
handleCheckboxChange(6)} />
handleCheckboxChange(7)} />
handleCheckboxChange(8)} />

Kotak Saran (0)

Keterangan:

{/*

{detail?.status}

*/}
) : ( "" )}
); }