"use client"; import { LayoutNavbarNew, TEMA } from "@/module/_global"; import { useHookstate } from "@hookstate/core"; import { ActionIcon, Box, Button, Flex, Group, rem, SimpleGrid, Stack, Text, TextInput } from "@mantine/core"; import { DatePicker } from "@mantine/dates"; import { useShallowEffect } from "@mantine/hooks"; import moment from "moment"; import { useState } from "react"; import toast from "react-hot-toast"; import { HiChevronLeft } from "react-icons/hi2"; import { NewIFormDateProject } from "../lib/type_project"; export default function ViewDateEndTask({ onClose, onSet }: { onClose: (val: boolean) => void, onSet: (val: NewIFormDateProject) => void }) { const [value, setValue] = useState<[Date | null, Date | null]>([null, null]); const [title, setTitle] = useState("") const tema = useHookstate(TEMA) const [acuan, setAcuan] = useState(false) const [touched, setTouched] = useState({ title: false, date: false }); 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") onSet({ dateStart: moment(value[0]).format('YYYY-MM-DD'), dateEnd: moment(value[1]).format('YYYY-MM-DD'), title: title }) } function onCheck() { const cek = checkAll() if (!cek) return false onSubmit() } function checkAll() { let nilai = true if (title == "") { setTouched(touched => ({ ...touched, title: true })) nilai = false } if (value[0] == null || value[1] == null) { setTouched(touched => ({ ...touched, date: true })) nilai = false } return nilai } function onValidation(kategori: string, val: string) { if (kategori == 'title') { setTitle(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(() => { if (acuan) { onValidation('date', String(value)) } else { setAcuan(true) } }, [value]) return ( { onClose(true) }} bg={tema.get().bgIcon} size="lg" radius="lg" aria-label="Settings"> } title={"Tanggal dan Tugas"} menu /> Tanggal Mulai * {value[0] ? `${moment(value[0]).format('DD-MM-YYYY')}` : ""} Tanggal Berakhir * {value[1] ? `${moment(value[1]).format('DD-MM-YYYY')}` : ""} { (touched && touched.date) ? Tanggal Tidak Boleh Kosong : <> } { onValidation('title', e.target.value) }} error={ touched.title && ( title == "" ? "Judul Tugas Tidak Boleh Kosong" : null ) } /> ); }