Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import {
ACCOUNTING_OBJECT_IMPORT_TEMPLATES_TARGET,
getErrorsCount,
Import,
importErrorsAtom,
importUploadedFile,
isDirtyImportSortaments,
isNewFileUploadedAtom,
isValidatedImportAtom,
} from '@/entities/accounting-object-import'
import { ErrorsBlock, IMPORT_TEMPLATE_URL, ImportErrorsCountBlock } from '@/features/import'
import { FileModel, Nullable } from '@/shared/@types'
import PlusIcon from '@/shared/assets/icons/common/plus.svg'
import { EXCEL_ACCEPT, EXCEL_EXTENSIONS } from '@/shared/config'
import { forceDownload, stopDefaultEvent } from '@/shared/helpers'
import { useUpload } from '@/shared/hooks'
import { httpClient, notify, useTranslate } from '@/shared/lib'
import { Button, File } from '@tmk/ui-kit'
import cn from 'classnames'
import { useAtom, useAtomValue } from 'jotai'
import { extension } from 'mime-types'
import { DragEvent, FC, useRef, useState } from 'react'
interface UploadImportFileProps {
saveImport: () => Promise<void>
isValidateLoading: boolean
isEditMode?: boolean
importData?: Import
}
export const UploadImportFile: FC<UploadImportFileProps> = ({ saveImport, isValidateLoading, importData }) => {
const [dragging, setDragging] = useState(false)
const [importsErrors, setImportsError] = useAtom(importErrorsAtom)
const [uploadedFile, setUploadedFile] = useAtom(importUploadedFile)
const ref = useRef<Nullable<HTMLInputElement>>(null)
const isDirtySortaments = useAtomValue(isDirtyImportSortaments)
const [isValidated, setIsValidated] = useAtom(isValidatedImportAtom)
const [isFileNew, setIsFileNew] = useAtom(isNewFileUploadedAtom)
const { t } = useTranslate(['accounting-object', 'common'])
const { upload } = useUpload({
optimistic: true,
})
const uploadFile = async (file: File) => {
setImportsError([])
const isExcel = EXCEL_EXTENSIONS.includes(extension(file.type) as string)
if (!isExcel)
notify(t('common:You_can_only_upload_excel_files'), {
status: 'info',
})
if (isExcel) {
await upload({
action: '',
method: 'POST',
file,
onSuccess: data => {
setUploadedFile(data)
},
})
setIsFileNew(true)
}
}
const handleDrop = (e: DragEvent<HTMLDivElement>) => {
stopDefaultEvent(e)
setDragging(false)
if (e.dataTransfer.files.length > 1) {
notify(t('common:You_can_only_upload_one_file'), {
status: 'info',
})
}
if (uploadedFile) {
notify(t('common:First_remove_uploaded_file'), {
status: 'error',
})
return
}
uploadFile(e.dataTransfer.files[0])
}
const downloadTemplate = async () => {
const response = await httpClient<Blob>({
url: ACCOUNTING_OBJECT_IMPORT_TEMPLATES_TARGET,
responseType: 'blob',
headers: {
Accept: EXCEL_ACCEPT,
},
}).then(res => res.data)
forceDownload(response ? window.URL.createObjectURL(response) : IMPORT_TEMPLATE_URL, 'template.xlsx').then(url =>
window.URL.revokeObjectURL(url)
)
}
const isEmptyFile = importsErrors?.length === 1 && importsErrors[0]?.payload?.File === 'emptyFile'
return (
<div className='bg-white w-full p-5 flex flex-col gap-y-2.5'>
<div className='flex items-center gap-x-2.5'>
<h2 className='text-black'>{t('common:File') + ' *'}</h2>
<Button variant='border-icon' onClick={() => ref.current?.click()} disabled={!!uploadedFile}>
<PlusIcon className='stroke-currentColor' />
</Button>
</div>
<input
hidden
ref={ref}
type='file'
multiple={false}
onChange={e => e.target.files?.[0] && uploadFile(e.target.files?.[0])}
onClick={e => {
const target = e.target as HTMLInputElement
if (target) target.value = ''
}}
/>
<div
onDrop={handleDrop}
onDragOver={stopDefaultEvent}
onDragEnter={e => [stopDefaultEvent(e), setDragging(true)]}
onDragLeave={e => [stopDefaultEvent(e), setDragging(false)]}
className={cn(
'w-full h-[100px] border border-dashed border-gray rounded-base flex flex-col justify-center items-center',
{
'bg-gray-tertiary': dragging,
'bg-transparent': !dragging,
}
)}
>
{isValidated && uploadedFile ? (
<>
{isEmptyFile ? (
<span className='text-main-regular text-text-secondary'>{importsErrors[0]?.message}</span>
) : (
<ImportErrorsCountBlock
objectsCount={(importData?.errorCount || 0) + (importData?.successCount || 0)}
errorsCount={getErrorsCount(importsErrors)}
t={t}
/>
)}
</>
) : (
<span className='text-main-regular text-text-secondary'>
{t('Upload a form')}
<span className='font-bold'>{t('Delivery of pipes')}</span>,{t('in Excel format by pressing the')}{' '}
<span className='font-bold'> + </span> {t('or move it to this area.')}
</span>
)}
{uploadedFile ? (
<div className='flex items-center gap-x-2.5 mt-2.5'>
<File
file={uploadedFile as FileModel}
t={t}
truncateMaxWidth={555}
onRemove={() => [setIsValidated(false), setUploadedFile(null)]}
/>
{(isFileNew || isDirtySortaments) && (
<Button onClick={saveImport} loading={isValidateLoading}>
<h2>{t('common:Check')}</h2>
</Button>
)}
</div>
) : (
<>
{!isValidated && (
<Button variant='text' className='mt-2.5' onClick={downloadTemplate}>
<h2 className='text-blue-dark underline underline-offset-4'>{t('common:Download_template')}</h2>
</Button>
)}
</>
)}
</div>
{!!importsErrors.length && !isEmptyFile && <ErrorsBlock />}
</div>
)
}
|