"use client"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Form, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import withReactContent from "sweetalert2-react-content"; import Swal from "sweetalert2"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { getLocaleTime, getLocaleTimestamp, textEllipsis, } from "@/utils/globals"; import { detailMediaSummary, getMediaBlastCampaignList, saveMediaBlastBroadcast, } from "@/service/broadcast/broadcast"; import { error } from "@/config/swal"; import { Link, useRouter } from "@/i18n/routing"; import { useEffect, useRef, useState } from "react"; import { useParams } from "next/navigation"; import Select from "react-select"; import makeAnimated from "react-select/animated"; import { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import dynamic from "next/dynamic"; const CustomEditor = dynamic( () => import("@/components/editor/custom-editor"), { ssr: false } ); const animatedComponent = makeAnimated(); const FormSchema = z.object({ selected: z .array( z.object({ id: z.number(), label: z.string(), value: z.string(), }) ) .min(1, "Pilih minimal satu campaign"), title: z.string().min(1, "Required"), url: z.string().min(1, "Required"), thumbnail: z.string().min(1, "Required"), detail: z.string().min(1, "Required"), messageType: z .array(z.string()) .min(1, "Pilih minimal satu tipe pesan (WA / Email)"), }); interface Campaign { id: string; name: string; } export default function ContentBlast() { const editor = useRef(null); const id = useParams()?.id; const MySwal = withReactContent(Swal); const router = useRouter(); const [dataSelectCampaign, setDataSelectCampaign] = useState([]); const [openModal, setOpenModal] = useState(false); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { selected: [], detail: "", messageType: [], }, }); const selectedTypes = form.watch("messageType"); const onSubmit = async (data: z.infer) => { MySwal.fire({ title: "Kirim Broadcast?", text: "Pesan akan dikirim ke semua campaign yang dipilih", icon: "warning", showCancelButton: true, cancelButtonColor: "#d33", confirmButtonColor: "#3085d6", confirmButtonText: "Kirim", }).then((result) => { if (result.isConfirmed) save(data); }); }; function htmlToWaText(html: string) { // Hilangkan link menjadi URL saja html = html.replace(/]*href="([^"]+)"[^>]*>(.*?)<\/a>/gi, "$1"); // Ambil gambar sebagai URL html = html.replace(/]*src="([^"]+)".*?>/gi, "$1"); // Hapus semua HTML tag html = html.replace(/<\/?[^>]+>/gi, ""); // Decode HTML entities html = html.replace(/&/g, "&"); // Rapikan spasi return html.replace(/\s+/g, " ").trim(); } const save = async (data: z.infer) => { const selectedCampaign = data.selected; const mediaRes = await detailMediaSummary(String(id)); const details = mediaRes?.data?.data; for (let i = 0; i < selectedCampaign.length; i++) { const campaignId = selectedCampaign[i].id; for (let mt of data.messageType) { let finalBody = data.detail; if (mt === "wa") { finalBody = textEllipsis(details?.description || "", 150); } const payload = { mediaUploadId: id, mediaBlastCampaignId: campaignId, subject: mt === "wa" ? `*${data.title}*` : data.title, body: mt === "wa" ? htmlToWaText(finalBody) : finalBody, type: mt, isScheduled: false, thumbnail: data.thumbnail, sendDate: getLocaleTimestamp(new Date()), sendTime: getLocaleTime(new Date()), contentUrl: data.url, }; console.log("REQ =>", payload); await saveMediaBlastBroadcast(payload); } } setOpenModal(true); }; // const save = async (data: z.infer) => { // const selectedCampaign = data.selected; // for (let i = 0; i < selectedCampaign.length; i++) { // const campaignId = selectedCampaign[i].id; // // Loop WA / Email // for (let mt of data.messageType) { // const payload = { // mediaUploadId: id, // mediaBlastCampaignId: campaignId, // subject: mt === "wa" ? `*${data.title}*` : data.title, // // body: data.detail.replace(/\n/g, ""), // body: mt === "wa" ? htmlToWaText(data.detail) : data.detail, // type: mt, // <-- WA / email // isScheduled: false, // thumbnail: data.thumbnail, // sendDate: getLocaleTimestamp(new Date()), // sendTime: getLocaleTime(new Date()), // contentUrl: data.url, // }; // console.log("REQ =>", payload); // await saveMediaBlastBroadcast(payload); // } // } // setOpenModal(true); // }; useEffect(() => { async function init() { const response = await detailMediaSummary(String(id)); const details = response?.data?.data; if (!details) return; form.setValue("thumbnail", details.smallThumbnailLink); form.setValue("title", details.title); form.setValue("url", details.pageUrl); let pageUrl = details?.pageUrl || ""; if (pageUrl.includes("mediahub.polri.go.id")) { pageUrl = pageUrl.replace( /(\.id)(\/|$)/, (match: any, p1: any, p2: any) => { return p2.startsWith("/in") ? match : `${p1}/in${p2}`; } ); } const body = `

Berita hari ini !!!

${pageUrl}

${details.title}

${textEllipsis(details.description, 150)}

`; form.setValue("detail", body); } async function loadCampaign() { const response = await getMediaBlastCampaignList(); const campaign = response?.data?.data?.content; handleLabelCampaign(campaign); } init(); loadCampaign(); }, [id]); function handleLabelCampaign(data: any) { const arr = data.map((item: any) => ({ id: item.id, label: item.title, value: item.title, })); setDataSelectCampaign(arr); } return (

Broadcast

{/* SELECT CAMPAIGN */} ( Pilih Campaign )} /> {/* DETAIL */} ( Isi Pesan )} /> {/* BUTTONS */}
{/* MODAL SENT */} Terkirim!
Pesan telah dikirim sesuai pilihan Anda.
); } // "use client"; // import { z } from "zod"; // import { useForm } from "react-hook-form"; // import { zodResolver } from "@hookform/resolvers/zod"; // import { // Form, // FormField, // FormItem, // FormLabel, // FormMessage, // } from "@/components/ui/form"; // import withReactContent from "sweetalert2-react-content"; // import Swal from "sweetalert2"; // import { Input } from "@/components/ui/input"; // import { Button } from "@/components/ui/button"; // import { // getLocaleTime, // getLocaleTimestamp, // textEllipsis, // } from "@/utils/globals"; // import { // detailMediaSummary, // getMediaBlastCampaignList, // saveMediaBlastBroadcast, // saveMediaBlastCampaign, // } from "@/service/broadcast/broadcast"; // import { error } from "@/config/swal"; // import { Link, useRouter } from "@/i18n/routing"; // import { useEffect, useRef, useState } from "react"; // import { useParams } from "next/navigation"; // import Select from "react-select"; // import makeAnimated from "react-select/animated"; // import { Textarea } from "@/components/ui/textarea"; // import { // Dialog, // DialogContent, // DialogFooter, // DialogHeader, // DialogTitle, // } from "@/components/ui/dialog"; // import dynamic from "next/dynamic"; // const CustomEditor = dynamic( // () => { // return import("@/components/editor/custom-editor"); // }, // { ssr: false } // ); // const animatedComponent = makeAnimated(); // const FormSchema = z.object({ // title: z.string({ // required_error: "Required", // }), // url: z.string({ // required_error: "Required", // }), // thumbnail: z.string({ // required_error: "Required", // }), // detail: z.string({ // required_error: "Required", // }), // selected: z // .array( // z.object({ // id: z.number(), // label: z.string(), // value: z.string(), // }) // ) // .refine((value) => value.length > 0, { // message: "Required", // }), // }); // interface Campaign { // id: string; // name: string; // } // export default function ContentBlast(props: { type: string }) { // const editor = useRef(null); // const id = useParams()?.id; // const MySwal = withReactContent(Swal); // const router = useRouter(); // const { type } = props; // const [dataSelectCampaign, setDataSelectCampaign] = useState([]); // const [openModal, setOpenModal] = useState(false); // const form = useForm>({ // resolver: zodResolver(FormSchema), // defaultValues: { selected: [], detail: "" }, // }); // 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 selectedCampaign = form.getValues("selected"); // for (let i = 0; i < selectedCampaign.length; i++) { // const reqData = { // mediaUploadId: id, // mediaBlastCampaignId: selectedCampaign[i].id, // subject: type == "wa" ? `*${data.title}*` : data.title, // body: data.detail?.replace(/\n/g, ""), // type: type, // isScheduled: false, // thumbnail: data?.thumbnail, // sendDate: getLocaleTimestamp(new Date()), // sendTime: getLocaleTime(new Date()), // contentUrl: data.url, // }; // console.log("req =>", reqData); // const response = await saveMediaBlastBroadcast(reqData); // } // setOpenModal(true); // }; // useEffect(() => { // async function initState() { // const response = await detailMediaSummary(String(id)); // const details = response?.data?.data; // let pageUrl = details?.pageUrl || ""; // if (pageUrl.includes("mediahub.polri.go.id")) { // pageUrl = pageUrl.replace( // /(\.id)(\/|$)/, // (match: any, p1: any, p2: any) => { // return p2.startsWith("/in") ? match : `${p1}/in${p2}`; // } // ); // } // if (details != undefined) { // form.setValue("thumbnail", details.smallThumbnailLink); // let body = `

Berita hari ini !!!

//
//
// //
// ${pageUrl} //

${details?.title}

//

// ${textEllipsis(details?.description, 150)} //

//
//
`; // form.setValue("title", `${details?.title}`); // form.setValue( // "url", // details?.pageUrl || "https://mediahub.polri.go.id" // ); // if (type == "wa") { // body = `${textEllipsis(details?.description, 150)}`; // form.setValue("detail", body); // } else { // form.setValue("detail", body); // } // } // } // async function getCampaign() { // const response = await getMediaBlastCampaignList(); // const campaign = response?.data?.data?.content; // handleLabelCampaign(campaign); // console.log(campaign); // } // initState(); // getCampaign(); // }, [id]); // function handleLabelCampaign(data: any) { // const optionArr: any = []; // data.map((option: any) => { // optionArr.push({ // id: option.id, // label: option.title, // value: option.title, // }); // }); // console.log("option arr", optionArr); // setDataSelectCampaign(optionArr); // } // return ( //
// //

Broadcast

// ( // // Subject // // // // )} // /> // ( // // Detail Perencanaan // {type === "wa" ? ( //