import { useEffect, useState } from 'react' import { useForm } from '@mantine/form' import { Modal, Button, Text, Box, Switch, Checkbox, FileInput, TextInput, Group, ActionIcon, } from '@mantine/core' import { IconPlus, IconTrash } from '@tabler/icons-react' import { create } from '@/rtk/helpers/CRUD' import { createDocument } from '@/api/Admin' import { getHeaderInfo } from '@/rtk/helpers/tokenCreator' import { notifications } from '@mantine/notifications' type MProps = { opened: boolean close: () => void getAllData: () => void } const ModalAddDocument = ({ opened, close, getAllData }: MProps) => { const [loadingSubmit, setLoadingSubmit] = useState(false) const form = useForm({ initialValues: { type: 'file', files: [] as { title: string; file: File }[], links: [] as { title: string; uri: string }[], is_active: true, }, }) useEffect(() => { form.reset() }, []) const handleCreate = async (values: any) => { try { if (values.type === 'file' && values.files.length === 0) { notifications.show({ title: 'Error', message: 'No files uploaded!!!', color: 'red', }) return } if (values.type === 'link' && values.links.length === 0) { notifications.show({ title: 'Error', message: 'No links provided!!!', color: 'red', }) return } setLoadingSubmit(true) const header = await getHeaderInfo() const formData = new FormData() header.headers['Content-Type'] = 'multipart/form-data' formData.append('type', values.type) formData.append('is_active', values.is_active ? '1' : '0') if (values.type === 'file') { values.files.forEach((item: any, index: number) => { formData.append(`files[${index}][title]`, item.title) formData.append(`files[${index}][file]`, item.file) }) } else { values.links.forEach((item: any, index: number) => { formData.append(`links[${index}][title]`, item.title) formData.append(`links[${index}][uri]`, item.uri) }) } const res = await create(createDocument, formData, getAllData, header) if (res === true) { resetForm() } } catch (error) { console.error(error) } finally { setLoadingSubmit(false) } } const resetForm = () => { close() form.reset() } return ( Add Document } >
form.setFieldValue( 'type', event.currentTarget.checked ? 'file' : 'link', ) } mb={'md'} disabled={loadingSubmit} /> {form.values.type === 'file' ? ( file.file)} onChange={(files) => { if (files) { const newFiles = files.map((file) => ({ title: file.name.split('.')[0], file, })) form.setFieldValue('files', newFiles) } }} disabled={loadingSubmit} required /> {form.values.files.map((item, index) => ( form.setFieldValue( `files.${index}.title`, event.currentTarget.value, ) } maxLength={255} disabled={loadingSubmit} required /> {item.file?.name} ))} ) : ( {form.values.links.map((_, index) => ( form.setFieldValue( `links.${index}.title`, event.currentTarget.value, ) } maxLength={255} disabled={loadingSubmit} required /> form.setFieldValue( `links.${index}.uri`, event.currentTarget.value, ) } disabled={loadingSubmit} required /> form.setFieldValue( 'links', form.values.links.filter((_, i) => i !== index), ) } disabled={loadingSubmit} mt={24} > ))} )} form.setFieldValue('is_active', event.currentTarget.checked) } disabled={loadingSubmit} />
) } export default ModalAddDocument