"use client"; import { keyWibu, LayoutDrawer, LayoutNavbarNew, TEMA } from "@/module/_global"; import LayoutModal from "@/module/_global/layout/layout_modal"; import { useHookstate } from "@hookstate/core"; import { Avatar, Box, Button, Divider, Flex, Grid, Group, rem, SimpleGrid, Stack, Text, TextInput } from "@mantine/core"; import { Dropzone } from '@mantine/dropzone'; import { useMediaQuery } from "@mantine/hooks"; import _ from "lodash"; import { useParams, useRouter } from "next/navigation"; import { useRef, useState } from "react"; import toast from "react-hot-toast"; import { FaTrash } from "react-icons/fa6"; import { IoIosArrowDropright } from "react-icons/io"; import { useWibuRealtime } from "wibu-realtime"; import { funCreateTask } from "../lib/api_task"; import { IFormDateTask, IFormMemberTask, IListFileTask } from "../lib/type_task"; import { globalMemberTask } from "../lib/val_task"; import ViewDateEndTask from "./create_date_end_task"; import CreateUsersProject from "./create_users_project"; import ResultsDateAndTask from "./results_date-and_task"; import ResultsFile from "./results_file"; export default function CreateTask() { const router = useRouter() const param = useParams<{ id: string }>() const [loadingModal, setLoadingModal] = useState(false) const [openDrawerFile, setOpenDrawerFile] = useState(false) const [openDrawerTask, setOpenDrawerTask] = useState(false) const [openMember, setOpenMember] = useState(false) const [openTugas, setOpenTugas] = useState(false) const [openModal, setOpenModal] = useState(false) const member = useHookstate(globalMemberTask) const memberValue = member.get() as IFormMemberTask[] const [dataTask, setDataTask] = useState([]) const openRef = useRef<() => void>(null) const [fileForm, setFileForm] = useState([]) const [listFile, setListFile] = useState([]) const [indexDelFile, setIndexDelFile] = useState(0) const [indexDelTask, setIndexDelTask] = useState(0) const isMobile = useMediaQuery('(max-width: 369px)'); const [title, setTitle] = useState("") const tema = useHookstate(TEMA) const [touched, setTouched] = useState({ title: false, }); const [data, setData] = useWibuRealtime({ WIBU_REALTIME_TOKEN: keyWibu, project: "sdm" }) function deleteFile(index: number) { setListFile([...listFile.filter((val, i) => i !== index)]) setFileForm([...fileForm.filter((val, i) => i !== index)]) setOpenDrawerFile(false) } function deleteTask(index: number) { setDataTask([...dataTask.filter((val, i) => i !== index)]) setOpenDrawerTask(false) } async function onSubmit() { try { setLoadingModal(true) const fd = new FormData(); for (let i = 0; i < fileForm.length; i++) { fd.append(`file${i}`, fileForm[i]); } fd.append("data", JSON.stringify({ idDivision: param.id, title, task: dataTask, member: memberValue })) const response = await funCreateTask(fd) if (response.success) { setData(response.notif) toast.success(response.message) setTitle("") member.set([]) setFileForm([]) setListFile([]) setDataTask([]) router.push(`/division/${param.id}/task/`) } else { toast.error(response.message) } } catch (error) { console.error(error) toast.error("Gagal menambahkan tugas divisi, coba lagi nanti"); } finally { setLoadingModal(false) setOpenModal(false) } } function onCheck() { const cek = checkAll() if (!cek) return false if (dataTask.length == 0) return toast.error("Error! silahkan tambahkan tugas") if (memberValue.length <= 1) return toast.error("Error! silahkan pilih anggota lebih dari 1") setOpenModal(true) } function checkAll() { let nilai = true if (title === "") { setTouched(touched => ({ ...touched, title: 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 }) } } } if (openTugas) return { setOpenTugas(false) }} onSet={(val) => { setDataTask([...dataTask, val]) setOpenTugas(false) }} />; if (openMember) return setOpenMember(false)} /> return ( { onValidation('title', e.target.value) }} required error={ touched.title && ( title == "" ? "Judul Tugas Tidak Boleh Kosong" : null ) } /> { setOpenTugas(true) }}> Tambah Tanggal & Tugas // setOpenDrawer(true) openRef.current?.() } > Upload File setOpenMember(true)}> Tambah Anggota { dataTask.length > 0 && Tanggal & Tugas { dataTask.map((v, i) => { return ( { setIndexDelTask(i) setOpenDrawerTask(true) }}> ) }) } } { listFile.length > 0 && File { listFile.map((v, i) => { return ( { setIndexDelFile(i) setOpenDrawerFile(true) }}> ) }) } } { member.length > 0 && Anggota Terpilih Total {member.length} Anggota {member.get().map((v: any, i: any) => { return ( {v.name} Anggota ); })} } { if (!files || _.isEmpty(files)) return toast.error('Tidak ada file yang dipilih') setFileForm([...fileForm, files[0]]) setListFile([...listFile, { name: files[0].name, extension: files[0].type.split("/")[1] }]) }} activateOnClick={false} maxSize={100 * 1024 ** 2} accept={['image/png', 'image/jpeg', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']} onReject={(files) => { return toast.error('File yang diizinkan: .csv, .png, .jpg, .pdf, .doc, .docx, .xls, .xlsx dengan ukuran maksimal 100 MB') }} > {/* Drawer pilih file */} {/* setOpenDrawer(false)} title={"Pilih File"} > openRef.current?.()}>
Pilih file diperangkat
router.push("/task/create?page=file-save")}>
Pilih file yang sudah ada
*/} {/* Drawer hapus file */} setOpenDrawerFile(false)} title={""} > deleteFile(indexDelFile)}> Hapus File {/* Drawer hapus tugas */} setOpenDrawerTask(false)} title={""} > deleteTask(indexDelTask)}> Hapus Tugas setOpenModal(false)} description="Apakah Anda yakin ingin menambahkan data?" onYes={(val) => { if (val) { onSubmit() } else { setOpenModal(false) } }} />
); }