"use client"; import { LayoutNavbarNew, TEMA } from "@/module/_global"; import LayoutModal from "@/module/_global/layout/layout_modal"; import { useHookstate } from "@hookstate/core"; import { Box, Button, 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 { funEditDetailTask, funGetDetailTask } from "../lib/api_task"; export default function EditDetailTask() { const [value, setValue] = useState<[Date | null, Date | null]>([null, null]); const router = useRouter() const [title, setTitle] = useState("") const param = useParams<{ id: string, detail: string }>() const [openModal, setOpenModal] = useState(false) const [loading, setLoading] = useState(true) const [idTugas, setIdTugas] = useState("") const tema = useHookstate(TEMA) const [touched, setTouched] = useState({ title: false, }); async function onSubmit() { if (value[0] == null || value[1] == null) return toast.error("Error! harus memilih tanggal") if (title == "") return toast.error("Error! harus memasukkan judul tugas") try { const res = await funEditDetailTask(param.detail, { title: title, dateStart: value[0], dateEnd: value[1], }) if (res.success) { toast.success(res.message); router.push(`/division/${param.id}/task/${idTugas}`) } else { toast.error(res.message); } } catch (error) { console.error(error); toast.error("Gagal edit detail tugas divisi, coba lagi nanti"); } } async function getOneData() { try { setLoading(true) const res = await funGetDetailTask(param.detail); if (res.success) { setIdTugas(res.data.idProject) setTitle(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 divisi, coba lagi nanti"); } finally { setLoading(false) } } useShallowEffect(() => { getOneData(); }, [param.detail]) function onCheck() { if (Object.values(touched).some((v) => v == true)) return false setOpenModal(true) } function onValidation(kategori: string, val: string) { if (kategori == 'title') { setTitle(val) if (val === "") { setTouched({ ...touched, title: true }) } else { setTouched({ ...touched, title: false }) } } } 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 ? : { onValidation('title', e.target.value) }} /> } {loading ? : } setOpenModal(false)} description="Apakah Anda yakin ingin mengubah data?" onYes={(val) => { if (val) { onSubmit() } setOpenModal(false) }} /> ); }