"use client" import { keyWibu, LayoutNavbarNew, TEMA } from '@/module/_global'; import LayoutModal from '@/module/_global/layout/layout_modal'; import { useHookstate } from '@hookstate/core'; import { Box, Button, rem, Skeleton, Stack, TextInput } from '@mantine/core'; import { useShallowEffect } from '@mantine/hooks'; import { useParams, useRouter } from 'next/navigation'; import { useState } from 'react'; import toast from 'react-hot-toast'; import { useWibuRealtime } from 'wibu-realtime'; import { funEditProject, funGetOneProjectById } from '../lib/api_project'; export default function EditTaskProject() { const router = useRouter() const [name, setName] = useState("") const [openModal, setOpenModal] = useState(false) const param = useParams<{ id: string }>() const [loading, setLoading] = useState(true) const tema = useHookstate(TEMA) const [touched, setTouched] = useState({ name: false, }); const [dataRealTime, setDataRealtime] = useWibuRealtime({ WIBU_REALTIME_TOKEN: keyWibu, project: "sdm" }) async function onSubmit() { try { const res = await funEditProject(param.id, { name }) if (res.success) { setDataRealtime([{ category: "project-detail", id: param.id, }]) toast.success(res.message) router.push("./") } else { toast.error(res.message) } } catch (error) { console.error(error) toast.error("Gagal mengedit Kegiatan, coba lagi nanti") } } function onCheck() { if (name == "") { setTouched({ ...touched, name: true }) return false } setOpenModal(true) } function onValidation(kategori: string, val: string) { if (kategori == 'title') { setName(val) if (val === "") { setTouched({ ...touched, name: true }) } else { setTouched({ ...touched, name: false }) } } } async function getOneData() { try { setLoading(true) const res = await funGetOneProjectById(param.id, 'data'); if (res.success) { setName(res.data.title); } else { toast.error(res.message); } setLoading(false); } catch (error) { console.error(error); toast.error("Gagal mendapatkan data Kegiatan, coba lagi nanti"); } finally { setLoading(false); } } useShallowEffect(() => { getOneData(); }, [param.id]) return ( {loading ? : { onValidation('title', e.target.value) }} error={ touched.name && ( name == "" ? "Kegiatan Tidak Boleh Kosong" : null ) } /> } {loading ? : } setOpenModal(false)} description="Apakah Anda yakin ingin mengedit Kegiatan ini?" onYes={(val) => { if (val) { onSubmit() } setOpenModal(false) }} /> ); }