"use client"; import { IRealtimeData } from "@/lib/global_state"; import { RouterVote } from "@/lib/router_hipmi/router_vote"; import { MainColor } from "@/app_modules/_global/color/color_pallet"; import ComponentGlobal_InputCountDown from "@/app_modules/_global/component/input_countdown"; import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil"; import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal"; import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan"; import notifikasiToAdmin_funCreate from "@/app_modules/notifikasi/fun/create/create_notif_to_admin"; import { Box, Button, Center, Group, Stack, Text, TextInput, Textarea, } from "@mantine/core"; import { DatePickerInput } from "@mantine/dates"; import { IconMinus, IconPlus } from "@tabler/icons-react"; import { useAtom } from "jotai"; import _ from "lodash"; import moment from "moment"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { WibuRealtime } from "wibu-pkg"; import { Vote_funCreate } from "../fun/create/create_vote"; import { gs_vote_hotMenu } from "../global_state"; import { clientLogger } from "@/util/clientLogger"; export default function Vote_Create() { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [hotMenu, setHotMenu] = useAtom(gs_vote_hotMenu); const [data, setData] = useState({ title: "", deskripsi: "", awalVote: "", akhirVote: "", }); const [listVote, setListVote] = useState([ { name: "Nama Pilihan", value: "", }, { name: "Nama Pilihan", value: "", }, ]); async function onSave() { if (_.values(data).includes("")) return ComponentGlobal_NotifikasiPeringatan("Lengkapi Data"); const cekAwalVote = moment(data.awalVote).format(); if (cekAwalVote === "Invalid date") return ComponentGlobal_NotifikasiPeringatan("Lengkapi Tanggal"); const cekAkhirVote = moment(data.akhirVote).format(); if (cekAkhirVote === "Invalid date") return ComponentGlobal_NotifikasiPeringatan("Lengkapi Tanggal"); if (_.values(listVote.map((e) => e.value)).includes("")) return ComponentGlobal_NotifikasiPeringatan("Lengkapi Pilihan Voting"); // console.log("berhasil"); try { setIsLoading(true); const res = await Vote_funCreate(data as any, listVote); if (res.status === 201) { const dataNotifikasi: IRealtimeData = { appId: res.data?.id as any, status: res.data?.Voting_Status?.name as any, userId: res.data?.authorId as any, pesan: res.data?.title as any, kategoriApp: "VOTING", title: "Voting baru", }; const notif = await notifikasiToAdmin_funCreate({ data: dataNotifikasi as any, }); if (notif.status === 201) { WibuRealtime.setData({ type: "notification", pushNotificationTo: "ADMIN", }); WibuRealtime.setData({ type: "trigger", pushNotificationTo: "ADMIN", dataMessage: dataNotifikasi, }); setHotMenu(2); router.replace(RouterVote.status({ id: "2" })); ComponentGlobal_NotifikasiBerhasil(res.message); } } else { setIsLoading(false); ComponentGlobal_NotifikasiGagal(res.message); } } catch (error) { setIsLoading(false); clientLogger.error("Error create voting", error); } } return ( <> { setData({ ...data, title: val.target.value, }); }} />