import toast from '@/common/libs/toast' import { useResultStore } from '@/common/stores/useResultStore' import { Button, Modal, ModalBody, ModalContent, ModalHeader } from '@nextui-org/react' import { useMutation } from '@tanstack/react-query' import { AlertTriangleIcon } from 'lucide-react' import React, { ChangeEvent, FormEvent, useMemo, useState } from 'react' type Props = { modal: { isOpen: boolean, onOpenChange: () => void } } const ImportModal = ({ modal }: Props) => { const [file, setFile] = useState() const { companies, filter } = useResultStore() const selectedCompany = useMemo(() => { return companies.find((c) => c.value == filter.company) }, [companies, filter.company]) const handleFileChange = (e: ChangeEvent) => { if (e.target.files) setFile(e.target.files[0]) } const importMutation = useMutation({ mutationKey: ['import-product'], mutationFn: async () => { if (!file) return return await fetch(`/api/product/import?companyId=${filter.company}`, { method: 'POST', body: file, headers: { 'content-type': file.type, 'content-length': `${file.size}` } }) }, onSuccess(data) { if (data?.status === 200) { toast('Berhasil import product') setFile(undefined) } else { toast('Gagal import product') } }, }) const handleSubmit = (e: FormEvent) => { e.preventDefault() importMutation.mutate() } return ( Import Product
Hati-hati aksi ini akan menghapus semua data produk dan hasil stock opname di perusahaan {selectedCompany?.label}
) } export default ImportModal