File validity check function almost working function
Mon Nov 13 2023 12:45:27 GMT+0000 (Coordinated Universal Time)
Saved by
@alfred555
#react.js
// Check the validity of uploaded files
const checkFileValidity = useCallback(async (files: any[], documentTypeID: number) => {
const maxFileSize = 20 * 1024 * 1024;
const passportSizePhotoValidDocTypes = ['jpeg', 'png', 'jpg', 'bmp'];
const docPresent = draftsmanUploadedDocumentList.filter((doc:any) => {
if (doc.documentTypeID === 4) return doc;
});
console.log('docPresent', docPresent);
let isMaxSize = true;
// Filter valid files based on extension and file size
const validFiles = files.filter((file: { name: string; size: number; }) => {
console.log('validFile', file);
const validExt = file.name.split('.').pop();
if (file.size <= maxFileSize) isMaxSize = false;
const checkValidityCondition = passportSizePhotoValidDocTypes.includes(validExt);
if (documentTypeID === 4) {
if (checkValidityCondition) return file;
setShowInvalidFilesModal(true);
} else {
return file;
}
});
console.log('validFiles', validFiles);
if (documentTypeID === 4) {
if (docPresent.length === 0) {
saveValidFiles(validFiles, documentTypeID);
} else {
await dispatch(resetErrorState());
await dispatch(setErrorInfo('You can only add one passport size photo'));
}
} else if (documentTypeID !== 4) {
saveValidFiles(validFiles, documentTypeID);
}
// Filter invalid files
const invalidFiles = files.filter((file: any) => !validFiles.includes(file));
if (invalidFiles.length > 0) {
setShowInvalidFilesModal(true);
} else if (validFiles.length > 0) {
saveValidFiles(validFiles, documentTypeID);
// // Update the documentTypeArray state with the new file name
const updatedDocumentTypeArray = documentTypeArray?.map((docType) => {
if (docType.documentTypeID === documentTypeID) {
return { ...docType, documentName: validFiles[0]?.name };
}
return docType;
});
setDocumentTypeArray(updatedDocumentTypeArray);
console.log('Updated Document Array:', updatedDocumentTypeArray);
}
}, [saveValidFiles, documentTypeArray, setShowInvalidFilesModal, setDocumentTypeArray]);
content_copyCOPY
Comments