"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, Flex, Group, rem, SimpleGrid, Skeleton, Stack, Text, TextInput } from '@mantine/core'; import { DatePicker } from '@mantine/dates'; import { useShallowEffect } from '@mantine/hooks'; import moment from 'moment'; import { useParams, useRouter } from 'next/navigation'; import { useState } from 'react'; import toast from 'react-hot-toast'; import { useWibuRealtime } from 'wibu-realtime'; import { funEditDetailProject, funGetDetailProject } from '../lib/api_project'; export default function EditDetailTaskProject() { const [value, setValue] = useState<[Date | null, Date | null]>([null, null]); const [name, setName] = useState("") const [idProject, setIdProject] = useState("") const param = useParams<{ id: string }>() const [openModal, setOpenModal] = useState(false) const [loading, setLoading] = useState(true) const [loadingModal, setLoadingModal] = useState(false) const tema = useHookstate(TEMA) const [touched, setTouched] = useState({ title: false, date: false, }); const [dataRealTime, setDataRealtime] = useWibuRealtime({ WIBU_REALTIME_TOKEN: keyWibu, project: "sdm" }) const router = useRouter() async function onSubmit() { if (value[0] == null || value[1] == null) return toast.error("Error! harus memilih tanggal") if (name == "") return toast.error("Error! harus memasukkan judul tugas") try { setLoadingModal(true) const res = await funEditDetailProject(param.id, { title: name, dateStart: moment(value[0]).format('YYYY-MM-DD'), dateEnd: moment(value[1]).format('YYYY-MM-DD'), }) if (res.success) { setDataRealtime([{ category: "project-detail-task", id: idProject, }]) toast.success(res.message); router.push('/project/' + idProject) } else { toast.error(res.message); } } catch (error) { console.error(error); toast.error("Gagal edit detail tugas Kegiatan, coba lagi nanti"); } finally { setLoadingModal(false) setOpenModal(false) } } async function getOneData() { try { setLoading(true) const res = await funGetDetailProject(param.id); if (res.success) { setIdProject(res.data.idProject) setName(res.data.title) setValue([ new Date(moment(res.data.dateStart).format('YYYY-MM-DD')), new Date(moment(res.data.dateEnd).format('YYYY-MM-DD')), ]) } else { toast.error(res.message); } setLoading(false); } catch (error) { console.error(error); toast.error("Gagal mendapatkan detail tugas Kegiatan, coba lagi nanti"); } finally { setLoading(false); } } useShallowEffect(() => { getOneData(); }, [param.id]) function onCheck() { if (Object.values(touched).some((v) => v == true)) return false setOpenModal(true) } function onValidation(kategori: string, val: string) { if (kategori == 'title') { setName(val) if (val === "") { setTouched({ ...touched, title: true }) } else { setTouched({ ...touched, title: false }) } } else if (kategori == 'date') { const array = val.split(",") if (array[0] == '' || array[1] == '') { setTouched({ ...touched, date: true }) } else { setTouched({ ...touched, date: false }) } } } useShallowEffect(() => { onValidation('date', String(value)) }, [value]) return ( {loading ? : <> Tanggal Mulai * {value[0] ? `${moment(value[0]).format('DD-MM-YYYY')}` : ""} } {loading ? : <> Tanggal Berakhir * {value[1] ? `${moment(value[1]).format('DD-MM-YYYY')}` : ""} } { (!loading && touched && touched.date) ? Tanggal Tidak Boleh Kosong : <> } {loading ? : { onValidation('title', e.target.value) }} error={ touched.title && ( name == "" ? "Judul Tugas Tidak Boleh Kosong" : null ) } /> } {loading ? : } setOpenModal(false)} description="Apakah Anda yakin ingin mengubah data?" onYes={(val) => { if (val) { onSubmit() } else { setOpenModal(false) } }} /> ); }