"use client"; import React, { ChangeEvent, Fragment, 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 { register } from "module"; import { Switch } from "@/components/ui/switch"; import Cookies from "js-cookie"; import { createMedia, deleteFile, getTagsBySubCategoryId, listEnableCategory, uploadThumbnail, } from "@/service/content/content"; import { detailMedia } from "@/service/curated-content/curated-content"; import { Badge } from "@/components/ui/badge"; import { CloudUpload, MailIcon } from "lucide-react"; import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; import "swiper/css/free-mode"; import "swiper/css/navigation"; import "swiper/css/pagination"; import "swiper/css/thumbs"; import "swiper/css"; import "swiper/css/navigation"; import { FreeMode, Navigation, Pagination, Thumbs } from "swiper/modules"; import { useDropzone } from "react-dropzone"; import Image from "next/image"; import { Icon } from "@iconify/react/dist/iconify.js"; import { Upload } from "tus-js-client"; import { getCsrfToken } from "@/service/auth"; import { error, loading } from "@/lib/swal"; import { useTranslations } from "next-intl"; import dynamic from "next/dynamic"; const videoSchema = 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: number; category: { id: number; name: string; }; creatorName: string; categoryName: string; thumbnailLink: string; tags: string; }; interface FileWithPreview extends File { preview: string; } type Option = { id: string; name: string; }; const CustomEditor = dynamic( () => { return import("@/components/editor/custom-editor"); }, { ssr: false } ); export default function FormVideoUpdate() { const MySwal = withReactContent(Swal); const router = useRouter(); const { id } = useParams() as { id: string }; console.log(id); const editor = useRef(null); type VideoSchema = z.infer; let progressInfo: any = []; let counterUpdateProgress = 0; const [progressList, setProgressList] = useState([]); let uploadPersen = 0; const [isStartUpload, setIsStartUpload] = useState(false); const [counterProgress, setCounterProgress] = useState(0); const t = useTranslations("Form"); 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 [detailVideo, setDetailVideo] = useState([]); const [thumbsSwiper, setThumbsSwiper] = useState(null); const [files, setFiles] = useState([]); const [selectedTarget, setSelectedTarget] = useState(""); const [unitSelection, setUnitSelection] = useState({ allUnit: false, mabes: false, polda: false, polres: false, }); const [publishedFor, setPublishedFor] = useState([]); const inputRef = useRef(null); const [selectedOptions, setSelectedOptions] = useState<{ [fileId: number]: string[]; }>({}); const options: Option[] = [ { id: "all", name: "SEMUA" }, { id: "5", name: "UMUM" }, { id: "6", name: "JOURNALIS" }, { id: "7", name: "POLRI" }, { id: "8", name: "KSP" }, ]; let fileTypeId = "2"; const { getRootProps, getInputProps } = useDropzone({ onDrop: (acceptedFiles) => { setFiles(acceptedFiles.map((file) => Object.assign(file))); }, accept: { "video/*": [], }, }); const { control, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(videoSchema), }); // 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 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)); }; useEffect(() => { async function initState() { getCategories(); } initState(); }, []); const handleAddTag = (e: React.KeyboardEvent) => { if (e.key === "Enter" && e.currentTarget.value.trim()) { e.preventDefault(); const newTag = e.currentTarget.value.trim(); if (!tags.includes(newTag)) { setTags((prevTags) => [...prevTags, newTag]); // Tambahkan tag baru if (inputRef.current) { inputRef.current.value = ""; // Kosongkan input } } } }; const handleRemoveTag = (index: number) => { setTags((prevTags) => prevTags.filter((_, i) => i !== index)); }; const handleEditTag = (index: number, newValue: string) => { setTags((prevTags) => prevTags.map((tag, i) => (i === index ? newValue : tag)) ); }; 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); setSelectedTarget(String(details.category.id)); // Set form values immediately and then again after a delay to ensure editor is ready setValue("title", details.title); setValue("description", details.htmlDescription); setValue("creatorName", details.creatorName); // Set again after delay to ensure editor has loaded setTimeout(() => { setValue("title", details.title); setValue("description", details.htmlDescription); setValue("creatorName", details.creatorName); }, 500); if (details?.files) { setFiles(details.files); const initialOptions: { [key: number]: string[] } = {}; details.files.forEach((file: any) => { if (file.placements) { initialOptions[file.id] = mapPlacementsToOptions(file.placements); } }); setSelectedOptions(initialOptions); } if (details?.publishedFor) { // Split string "7" to an array ["7"] if needed setPublishedFor(details.publishedFor.split(",")); } if (details?.tags) { setTags(details.tags.split(",").map((tag: string) => tag.trim())); } // const matchingCategory = categories.find( // (category) => category.id === details.categoryId // ); // console.log("LALALALAL", matchingCategory) // if (matchingCategory) { // setSelectedTarget(String(matchingCategory.id)); // } // setSelectedTarget(details.categoryId); // Untuk dropdown const filesData = details.files || []; const fileUrls = filesData.map((files: { url: string }) => files.url ? files.url : "default-image.jpg" ); setDetailVideo(fileUrls); } } initState(); }, [refresh, setValue]); const mapPlacementsToOptions = (placements: string): string[] => { const mapping: Record = { all: "all", mabes: "nasional", polda: "wilayah", polres: "internasional", }; // Jika placements hanya "all", langsung aktifkan semua checkbox if (placements.trim() === "all") { return ["all", "nasional", "wilayah", "internasional"]; } const options = placements .split(",") .map((p) => mapping[p.trim()]) .filter(Boolean); const allSelected = ["nasional", "wilayah", "internasional"].every((opt) => options.includes(opt) ); return allSelected ? ["all", ...options] : options; }; const handleCheckboxChange = (id: string) => { if (id === "all") { // Select all options except "all" const allOptions = options .filter((opt) => opt.id !== "all") .map((opt) => opt.id); setPublishedFor( publishedFor.length === allOptions.length ? [] : allOptions ); } else { // Toggle individual option setPublishedFor((prev) => prev.includes(id) ? prev.filter((item) => item !== id) : [...prev, id] ); } }; // const save = async (data: VideoSchema) => { // loading(); // const finalTags = tags.join(", "); // 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: publishedFor.join(","), // creatorName: data.creatorName, // tags: finalTags, // isYoutube: false, // isInternationalMedia: false, // }; // const response = await createMedia(requestData); // console.log("Form Data Submitted:", requestData); // const formMedia = new FormData(); // const thumbnail = files[0]; // formMedia.append("file", thumbnail); // const responseThumbnail = await uploadThumbnail(id, formMedia); // if (responseThumbnail?.error == true) { // error(responseThumbnail?.message); // return false; // } // const progressInfoArr = []; // for (const item of files) { // progressInfoArr.push({ percentage: 0, fileName: item.name }); // } // progressInfo = progressInfoArr; // setIsStartUpload(true); // setProgressList(progressInfoArr); // close(); // // showProgress(); // files.map(async (item: any, index: number) => { // await uploadResumableFile(index, String(id), item, "0"); // }); // // MySwal.fire({ // // title: "Sukses", // // text: "Data berhasil disimpan.", // // icon: "success", // // confirmButtonColor: "#3085d6", // // confirmButtonText: "OK", // // }).then(() => { // // router.push("/en/contributor/content/video"); // // }); // }; const save = async (data: VideoSchema) => { loading(); const finalTags = tags.join(", "); 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: publishedFor.join(","), creatorName: data.creatorName, tags: finalTags, isYoutube: false, isInternationalMedia: false, }; const response = await createMedia(requestData); console.log("Form Data Submitted (Metadata):", requestData); if (response?.error) { error(response?.message); return false; } if (selectedFiles.length > 0) { const thumbnail = selectedFiles[0]; const formMedia = new FormData(); formMedia.append("file", thumbnail); const responseThumbnail = await uploadThumbnail(id, formMedia); if (responseThumbnail?.error) { // Perbaiki pengecekan error error(responseThumbnail?.message); return false; } console.log("Thumbnail uploaded:", responseThumbnail); } const newVideoFilesToUpload = files.filter((file) => file instanceof File); if (newVideoFilesToUpload.length > 0) { const progressInfoArr = newVideoFilesToUpload.map((item) => ({ percentage: 0, fileName: item.name, })); progressInfo = progressInfoArr; setIsStartUpload(true); setProgressList(progressInfoArr); newVideoFilesToUpload.map(async (item: File, index: number) => { await uploadResumableFile(index, String(id), item, "0"); }); } else { successSubmit("/in/contributor/content/video"); } }; const onSubmit = (data: VideoSchema) => { 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); } }); }; async function uploadResumableFile( idx: number, id: string, file: any, duration: string ) { console.log(idx, id, file, duration); // const placements = getPlacement(file.placements); // console.log("Placementttt: : ", placements); const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; console.log("CSRF TOKEN : ", csrfToken); const headers = { "X-XSRF-TOKEN": csrfToken, }; const upload = new Upload(file, { endpoint: `${process.env.NEXT_PUBLIC_API}/media/file/upload`, headers: headers, retryDelays: [0, 3000, 6000, 12_000, 24_000], chunkSize: 20_000, metadata: { mediaid: id, filename: file.name, filetype: file.type, duration, isWatermark: "false", // hardcode }, onBeforeRequest: function (req) { var xhr = req.getUnderlyingObject(); xhr.withCredentials = true; }, onError: async (e: any) => { console.log("Error upload :", e); error(e); }, onChunkComplete: ( chunkSize: any, bytesAccepted: any, bytesTotal: any ) => { const uploadPersen = Math.floor((bytesAccepted / bytesTotal) * 100); progressInfo[idx].percentage = uploadPersen; counterUpdateProgress++; console.log(counterUpdateProgress); setProgressList(progressInfo); setCounterProgress(counterUpdateProgress); }, onSuccess: async () => { uploadPersen = 100; progressInfo[idx].percentage = 100; counterUpdateProgress++; setCounterProgress(counterUpdateProgress); successTodo(); }, }); upload.start(); } const successSubmit = (redirect: string) => { MySwal.fire({ title: "Sukses", text: "Data berhasil disimpan.", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then(() => { router.push(redirect); }); }; function successTodo() { let counter = 0; for (const element of progressInfo) { if (element.percentage == 100) { counter++; } } if (counter == progressInfo.length) { setIsStartUpload(false); // hideProgress(); Cookies.remove("idCreate"); successSubmit("/in/contributor/content/video"); } } const handleRemoveAllFiles = () => { setFiles([]); }; const renderFilePreview = (file: FileWithPreview) => { if (file?.type?.startsWith("image")) { return ( {file.name} ); } else { return ; } }; const handleRemoveFile = (file: FileWithPreview) => { const uploadedFiles = files; const filtered = uploadedFiles.filter((i) => i.name !== file.name); setFiles([...filtered]); }; const fileList = files.map((file: any) => (
{renderFilePreview(file)}
{file.fileName || file.name}
{Math.round(file.size / 100) / 10 > 1000 ? ( <>{(Math.round(file.size / 100) / 10000).toFixed(1)} ) : ( <>{(Math.round(file.size / 100) / 10).toFixed(1)} )} {" kb"}
)); const handleCheckboxChangeImage = (fileId: number, value: string) => { setSelectedOptions((prev: any) => { const currentSelections = prev[fileId] || []; if (value === "all") { // If "all" is clicked, toggle all options if (currentSelections.includes("all")) { return { ...prev, [fileId]: [] }; // Deselect all } return { ...prev, [fileId]: ["all", "nasional", "wilayah", "internasional"], }; // Select all } else { // If any other checkbox is clicked, toggle that checkbox const updatedSelections = currentSelections.includes(value) ? currentSelections.filter((option: any) => option !== value) : [...currentSelections, value]; // If all individual options are selected, include "all" automatically const isAllSelected = ["nasional", "wilayah", "internasional"].every( (opt) => updatedSelections.includes(opt) ); return { ...prev, [fileId]: isAllSelected ? ["all", ...updatedSelections] : updatedSelections.filter((opt: any) => opt !== "all"), }; } }); }; function success() { MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { // window.location.reload(); } }); } const handleDeleteFile = (id: number) => { MySwal.fire({ title: "Hapus file", text: "Apakah Anda yakin ingin menghapus file ini?", icon: "warning", showCancelButton: true, cancelButtonColor: "#3085d6", confirmButtonColor: "#d33", confirmButtonText: "Hapus", }).then((result) => { if (result.isConfirmed) { doDelete(id); } }); }; async function doDelete(id: number) { const data = { id }; try { const response = await deleteFile(data); if (response?.error) { error(response.message); return; } // Jika berhasil, hapus file dari state lokal setFiles((prevFiles: any) => prevFiles.filter((file: any) => file.id !== id) ); success(); } catch (err) { error("Terjadi kesalahan saat menghapus file"); } } return (
{detail !== undefined ? (

{t("form-video", { defaultValue: "Form Video" })}

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

{errors.title.message}

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

{errors.description.message}

)}
{/* */}

{/* Drop files here or click to upload. */} {t("drag-file", { defaultValue: "Drag File" })}

{t("upload-file-video-max", { defaultValue: "Upload File Video Max", })}
{files.length ? (
{fileList}
{/* */}
) : null} {files.length > 0 && (
{files.map((file: any) => (
{file.fileName}
))}
)}
( )} /> {errors.creatorName?.message && (

{errors.creatorName.message}

)}
{/*
Thumbnail Gambar Utama
*/}
{ const file = e.target.files?.[0]; if (file) { setSelectedFiles([file]); } }} className="dark:border dark:border-gray-500 dark:rounded-lg" /> {selectedFiles.length > 0 ? ( Thumbnail Baru ) : ( Thumbnail Lama )}
{tags.map((tag, index) => ( handleEditTag(index, e.target.value)} className="bg-black text-white border-none focus:outline-none w-auto" /> ))}
{options.map((option: Option) => (
opt.id !== "all").length : publishedFor.includes(option.id) } onCheckedChange={() => handleCheckboxChange(option.id)} />
))}

{t("suggestion-box", { defaultValue: "Suggestion Box" })} (0)

{t("information", { defaultValue: "Information" })}:

{/*

{detail?.status}

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