From 968f2642cccf56eb7ab6030693b6b3635cf3d08e Mon Sep 17 00:00:00 2001 From: Sabda Yagra Date: Sat, 19 Jul 2025 19:51:23 +0700 Subject: [PATCH] fixing --- components/audio-player.tsx | 30 +--- components/form/content/audio-detail-form.tsx | 58 ++++---- components/form/content/audio-form.tsx | 133 ++++++------------ components/main/audio-detail.tsx | 8 +- 4 files changed, 83 insertions(+), 146 deletions(-) diff --git a/components/audio-player.tsx b/components/audio-player.tsx index 133e9676..7e523f50 100644 --- a/components/audio-player.tsx +++ b/components/audio-player.tsx @@ -2,34 +2,19 @@ import React, { useRef, useState, useEffect } from "react"; interface AudioPlayerProps { urlAudio: string; - fileName: string; // ✅ Tambahkan props ini + fileName: string; } const AudioPlayer: React.FC = ({ urlAudio, fileName }) => { const audioRef = useRef(null); const [currentTime, setCurrentTime] = useState(0); - const playAudio = () => { - audioRef.current?.play(); - }; - - const pauseAudio = () => { - audioRef.current?.pause(); - }; - - const stopAudio = () => { - if (audioRef.current) { - audioRef.current.pause(); - audioRef.current.currentTime = 0; - } - }; - useEffect(() => { const audio = audioRef.current; if (!audio) return; const updateTime = () => { - setCurrentTime(audio.currentTime); + setCurrentTime(audio?.currentTime); }; audio.addEventListener("timeupdate", updateTime); @@ -38,22 +23,17 @@ const AudioPlayer: React.FC = ({ urlAudio, fileName }) => { }; }, []); - const formatTime = (time: number) => { - const minutes = Math.floor(time / 60); - const seconds = Math.floor(time % 60).toString().padStart(2, "0"); - return `${minutes}:${seconds}`; - }; - return (

{fileName}

+ {/* {urlAudio} */}
diff --git a/components/form/content/audio-form.tsx b/components/form/content/audio-form.tsx index 082e236c..24bf5174 100644 --- a/components/form/content/audio-form.tsx +++ b/components/form/content/audio-form.tsx @@ -211,9 +211,6 @@ export default function FormAudio() { tags: z .array(z.string().min(1)) .min(1, { message: "Wajib isi minimal 1 tag" }), - publishedFor: z - .array(z.string()) - .min(1, { message: "Minimal 1 target publish harus dipilih." }), }); const { @@ -230,7 +227,6 @@ export default function FormAudio() { rewriteDescription: "", category: "", tags: [], - publishedFor: [], }, }); @@ -483,8 +479,10 @@ export default function FormAudio() { const handleCheckboxChange = (id: string): void => { if (id === "all") { if (publishedFor.includes("all")) { + // Uncheck all checkboxes setPublishedFor([]); } else { + // Select all checkboxes setPublishedFor( options .filter((opt: any) => opt.id !== "all") @@ -495,6 +493,8 @@ export default function FormAudio() { const updatedPublishedFor = publishedFor.includes(id) ? publishedFor.filter((item) => item !== id) : [...publishedFor, id]; + + // Remove "all" if any checkbox is unchecked if (publishedFor.includes("all") && id !== "all") { setPublishedFor(updatedPublishedFor.filter((item) => item !== "all")); } else { @@ -505,6 +505,7 @@ export default function FormAudio() { useEffect(() => { if (articleBody) { + // Set ke dua field jika rewrite juga aktif setValue("description", articleBody); setValue("rewriteDescription", articleBody); } @@ -512,21 +513,13 @@ export default function FormAudio() { const save = async (data: AudioSchema) => { loading(); - - if (files.length === 0) { - MySwal.fire("Error", "Minimal 1 file harus diunggah.", "error"); - return; - } - const finalTags = tags.join(", "); const finalTitle = isSwitchOn ? title : data.title; - // const finalDescription = articleBody || data.description; const finalDescription = isSwitchOn ? data.description : selectedFileType === "rewrite" ? data.rewriteDescription : data.descriptionOri; - if (!finalDescription?.trim()) { MySwal.fire("Error", "Deskripsi tidak boleh kosong.", "error"); return; @@ -574,36 +567,40 @@ export default function FormAudio() { const response = await createMedia(requestData); console.log("Form Data Submitted:", requestData); + if (response?.error) { + MySwal.fire("Error", response?.message, "error"); + return; + } Cookies.set("idCreate", response?.data?.data, { expires: 1 }); id = response?.data?.data; + const formMedia = new FormData(); - const thumbnail = files[0]; - formMedia.append("file", thumbnail); + console.log("Thumbnail : ", files[0]); + formMedia.append("file", files[0]); const responseThumbnail = await uploadThumbnail(id, formMedia); if (responseThumbnail?.error == true) { error(responseThumbnail?.message); return false; } } - const progressInfoArr = files.map((item) => ({ - percentage: 0, - fileName: item.name, - })); + + 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, - fileTypeId == "2" || fileTypeId == "4" ? item.duration : "0" - ); + await uploadResumableFile(index, String(id), item, "0"); }); Cookies.remove("idCreate"); + + // MySwal.fire("Sukses", "Data berhasil disimpan.", "success"); }; const onSubmit = (data: AudioSchema) => { @@ -650,7 +647,7 @@ export default function FormAudio() { filename: file.name, filetype: file.type, duration, - isWatermark: "false", + isWatermark: "false", }, onBeforeRequest: function (req) { var xhr = req.getUnderlyingObject(); @@ -1416,73 +1413,29 @@ export default function FormAudio() { )} - ( -
-
- - - {options.map((option) => { - const isAllChecked = - field.value.length === - options.filter((opt: any) => opt.id !== "all").length; - - const isChecked = +
+
+ + {options.map((option) => ( +
+ { - let updated: string[] = []; - - if (option.id === "all") { - updated = isAllChecked - ? [] - : options - .filter((opt: any) => opt.id !== "all") - .map((opt: any) => opt.id); - } else { - updated = isChecked - ? field.value.filter((val) => val !== option.id) - : [...field.value, option.id]; - - if (isAllChecked && option.id !== "all") { - updated = updated.filter((val) => val !== "all"); - } - } - - field.onChange(updated); - setPublishedFor(updated); - }; - - return ( -
- - -
- ); - })} - - {errors.publishedFor && ( -

- {errors.publishedFor.message} -

- )} + ? publishedFor.length === + options.filter((opt: any) => opt.id !== "all") + .length + : publishedFor.includes(option.id) + } + onCheckedChange={() => handleCheckboxChange(option.id)} + /> +
-
- )} - /> + ))} +
+
diff --git a/components/main/audio-detail.tsx b/components/main/audio-detail.tsx index 385be5af..461c594d 100644 --- a/components/main/audio-detail.tsx +++ b/components/main/audio-detail.tsx @@ -99,9 +99,7 @@ const DetailAudio = () => { const initFetch = async () => { const response = await getDetail(String(slug)); - console.log("detailAudio", response); const responseGet = await getPublicSuggestionList(slug?.split("-")?.[0]); - setIsFromSPIT(response?.data?.data?.isFromSPIT); setWidth(window.innerWidth); setContent(response?.data?.data); @@ -405,7 +403,7 @@ const DetailAudio = () => { const { default: WaveSurfer } = await import("wavesurfer.js"); if (wavesurfer.current) { - wavesurfer.current.destroy(); // 🔥 Hapus instance lama sebelum membuat yang baru + wavesurfer.current.destroy(); } setPlaying(false); @@ -414,7 +412,7 @@ const DetailAudio = () => { return [ Math.floor((time % 3600) / 60), // minutes - `00${Math.floor(time % 60)}`.slice(-2), // seconds + `00${Math.floor(time % 60)}`.slice(-2), ].join(":"); }; @@ -445,7 +443,7 @@ const DetailAudio = () => { return () => { if (wavesurfer.current) { - wavesurfer.current.destroy(); // 🔥 Hapus saat unmount + wavesurfer.current.destroy(); } }; }