Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) O3-4144 Use allowedFileExtension Hook rather than allowedExtensions from Context type. #2107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { Controller, useForm, type SubmitHandler } from 'react-hook-form';
import { Button, Form, ModalBody, ModalFooter, ModalHeader, Stack, TextArea, TextInput } from '@carbon/react';
import { DocumentPdf, DocumentUnknown } from '@carbon/react/icons';
import { useAllowedFileExtensions } from '@openmrs/esm-patient-common-lib';
import { type UploadedFile, UserHasAccess } from '@openmrs/esm-framework';
import CameraMediaUploaderContext from './camera-media-uploader-context.resources';
import styles from './file-review.scss';
Expand Down Expand Up @@ -74,7 +75,7 @@ const FilePreview: React.FC<FilePreviewProps> = ({
clearData,
}) => {
const { t } = useTranslation();
const { allowedExtensions } = useContext(CameraMediaUploaderContext);
const { allowedFileExtensions } = useAllowedFileExtensions();
const fileNameWithoutExtension = uploadedFile.fileName.trim().replace(/\.[^\\/.]+$/, '');

const schema = z.object({
Expand All @@ -99,7 +100,7 @@ const FilePreview: React.FC<FilePreviewProps> = ({
const { fileName, fileDescription } = data;

const sanitizedFileName =
allowedExtensions?.reduce((name, extension) => {
allowedFileExtensions?.reduce((name, extension) => {
const regex = new RegExp(`\\.(${extension})+$`, 'i');
return name.replace(regex, '');
}, fileName) || fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FileUploaderDropContainer, InlineNotification } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { useConfig } from '@openmrs/esm-framework';
import { readFileAsString } from '../utils';
import { useAllowedFileExtensions } from '@openmrs/esm-patient-common-lib';
import CameraMediaUploaderContext from './camera-media-uploader-context.resources';
import styles from './media-uploader.scss';

Expand All @@ -14,7 +15,8 @@ interface ErrorNotification {
const MediaUploaderComponent = () => {
const { t } = useTranslation();
const { maxFileSize } = useConfig();
const { setFilesToUpload, allowedExtensions, multipleFiles } = useContext(CameraMediaUploaderContext);
const { setFilesToUpload, multipleFiles } = useContext(CameraMediaUploaderContext);
const { allowedFileExtensions } = useAllowedFileExtensions();
const [errorNotification, setErrorNotification] = useState<ErrorNotification>(null);

const upload = useCallback(
Expand All @@ -28,8 +30,8 @@ const MediaUploaderComponent = () => {
'exceeds the size limit of',
)} ${maxFileSize} MB.`,
});
} else if (!isFileExtensionAllowed(file.name, allowedExtensions)) {
const lastExtension = allowedExtensions.pop();
} else if (!isFileExtensionAllowed(file.name, allowedFileExtensions)) {
const lastExtension = allowedFileExtensions.pop();

setErrorNotification({
title: t('unsupportedFileType', 'Unsupported file type'),
Expand All @@ -39,7 +41,7 @@ const MediaUploaderComponent = () => {
{
fileName: file.name,
lastExtension: lastExtension,
supportedExtensions: allowedExtensions.join(', '),
supportedExtensions: allowedFileExtensions.join(', '),
},
),
});
Expand All @@ -62,16 +64,16 @@ const MediaUploaderComponent = () => {
}
});
},
[setFilesToUpload, maxFileSize, t, allowedExtensions],
[setFilesToUpload, maxFileSize, t, allowedFileExtensions],
);

const isFileExtensionAllowed = (fileName: string, allowedExtensions: string[]): boolean => {
if (!allowedExtensions) {
const isFileExtensionAllowed = (fileName: string, allowedFileExtensions: string[]): boolean => {
if (!allowedFileExtensions) {
return true;
}

const fileExtension = fileName.split('.').pop();
return allowedExtensions?.includes(fileExtension.toLowerCase());
return allowedFileExtensions?.includes(fileExtension.toLowerCase());
};

return (
Expand All @@ -93,13 +95,13 @@ const MediaUploaderComponent = () => {
})}
.{' '}
{t('supportedFiletypes', 'Supported files are {{supportedFiles}}', {
supportedFiles: allowedExtensions?.join(', '),
supportedFiles: allowedFileExtensions?.join(', '),
})}
.
</p>
<div className={styles.uploadFile}>
<FileUploaderDropContainer
accept={allowedExtensions?.map((ext) => '.' + ext) || ['*']}
accept={allowedFileExtensions?.map((ext) => '.' + ext) || ['*']}
labelText={t('fileSizeInstructions', 'Drag and drop files here or click to upload')}
tabIndex={0}
multiple={multipleFiles}
Expand Down