"use client" import { globalRole, keyWibu, LayoutDrawer, LayoutNavbarNew, TEMA } from "@/module/_global"; import { globalIsAdminDivision, globalIsMemberDivision } from "@/module/division_new"; import { useHookstate } from "@hookstate/core"; import { ActionIcon, Avatar, Badge, Box, Center, Divider, Flex, Grid, Group, rem, Skeleton, Spoiler, Text, TextInput } from "@mantine/core"; import { useMediaQuery, useShallowEffect } from "@mantine/hooks"; import moment from "moment"; import "moment/locale/id"; import { useParams, useRouter } from "next/navigation"; import { useState } from "react"; import toast from "react-hot-toast"; import { GrChatOption } from "react-icons/gr"; import { HiMenu } from "react-icons/hi"; import { VscSend } from "react-icons/vsc"; import { useWibuRealtime } from "wibu-realtime"; import { funCreateComent, funGetDiscussionById } from "../lib/api_discussion"; import { IDetailDiscussion } from "../lib/type_discussion"; import { globalRefreshDiscussion } from "../lib/val_discussion"; import DrawerDetailDiscussion from "./drawer_detail_discussion"; export default function DetailDiscussion({ id, idDivision }: { id: string, idDivision: string }) { const [isData, setData] = useState() const [isComent, setIsComent] = useState("") const param = useParams<{ id: string, detail: string }>() const [isLoad, setIsLoad] = useState(true) const refresh = useHookstate(globalRefreshDiscussion) const roleLogin = useHookstate(globalRole) const [isCreator, setCreator] = useState(false) const [isUser, setUser] = useState('') const adminLogin = useHookstate(globalIsAdminDivision) const memberDivision = useHookstate(globalIsMemberDivision) const tema = useHookstate(TEMA) const router = useRouter() const isMobile = useMediaQuery('(max-width: 369px)'); const isMobile2 = useMediaQuery("(max-width: 438px)"); const [dataRealTime, setDataRealtime] = useWibuRealtime({ WIBU_REALTIME_TOKEN: keyWibu, project: "sdm" }) const getData = async (loading: boolean) => { try { setIsLoad(loading) const response = await funGetDiscussionById(id) setData(response.data) setIsLoad(false) setCreator(response.data.isCreator) setUser(response.user) } catch (error) { console.error(error) } finally { setIsLoad(false) } } useShallowEffect(() => { getData(true) }, [refresh.get()]) useShallowEffect(() => { if (dataRealTime && dataRealTime.some((i: any) => i.category == 'discussion-detail' && i.id == id)) { getData(false) } if (dataRealTime && dataRealTime.some((i: any) => i.category == 'discussion-delete' && i.id == id && i.user != isUser)) { if ((roleLogin.get() == "user" || roleLogin.get() == "coadmin") && !adminLogin.get()) { toast.error("Data telah diarsipkan, anda akan beralih ke halaman list diskusi") setTimeout(() => { router.push(`/division/${param.id}/discussion`) }, 1000) } else { getData(true) } } }, [dataRealTime]) async function reloadData() { try { const response = await funGetDiscussionById(id) setData(response.data) } catch (error) { console.error(error) } } const sendComent = async () => { try { if (isComent.trim() == "") { return toast.error("Masukkan Komentar Anda") } const response = await funCreateComent(id, { comment: isComent, idDiscussion: param.detail }) if (response.success) { setIsComent("") setDataRealtime([{ category: "discussion-detail", id: id, }]) reloadData() } else { toast.error(response.message) } } catch (error) { console.error(error) } } const [openDrawer, setOpenDrawer] = useState(false) return ( setOpenDrawer(true)} bg={tema.get().bgIcon} size="lg" radius="lg" aria-label="Settings"> : <> } /> setOpenDrawer(false)}> setOpenDrawer(false)} id={id} status={Number(isData?.status)} idDivision={idDivision} active={isData?.isActive == false ? false : true} /> {isLoad ? Array(1) .fill(null) .map((_, i) => ( )) : <> {isData?.totalComments == 0 ? {isData?.username} {!isData?.isActive ? ARSIP : {isData?.status === 1 ? "BUKA" : "TUTUP"} } {isData?.createdAt} {isData?.desc} {isData?.totalComments ? {isData?.totalComments} Komentar : ""} : {isData?.username} {!isData?.isActive ? ARSIP : {isData?.status === 1 ? "BUKA" : "TUTUP"} } {isData?.createdAt} {isData?.desc} {isData?.totalComments ? {isData?.totalComments} Komentar : ""} } } {isLoad ? Array(2) .fill(0) .map((_, i) => ( )) : isData?.DivisionDisscussionComment.map((v, i) => { return ( {v.username} {moment(v.createdAt).format("lll").replace('pukul', '')} {v.comment} ); }) } {isLoad ? : {300 - isComent.length} karakter tersisa setIsComent(e.target.value)} value={isComent} maxLength={300} />
}
) }