mirror of
https://github.com/suitenumerique/docs.git
synced 2026-08-17 21:25:43 +02:00
🛂(frontend) add guard before importing a doc
We could still have 400 error when importing ".markdown", because the backend only accepts ".md" for markdown files. We added another guard on the extensions to prevent this error and make sure the user send acceptable formats.
This commit is contained in:
@@ -33,7 +33,7 @@ export const ContentTypes: {
|
||||
},
|
||||
Markdown: {
|
||||
mime: 'text/markdown',
|
||||
extensions: ['.md', '.markdown'],
|
||||
extensions: ['.md'],
|
||||
},
|
||||
OctetStream: {
|
||||
mime: 'application/octet-stream',
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
useToastProvider,
|
||||
} from '@gouvfr-lasuite/cunningham-react';
|
||||
import { t } from 'i18next';
|
||||
import { useMemo } from 'react';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useDropzone } from 'react-dropzone';
|
||||
|
||||
import { useConfig } from '@/core';
|
||||
@@ -61,12 +61,37 @@ export const useImport = ({ onDragOver }: UseImportProps) => {
|
||||
);
|
||||
}, [config?.CONVERSION_FILE_EXTENSIONS_ALLOWED]);
|
||||
|
||||
const toastInvalidFileType = useCallback(
|
||||
(fileName: string) => {
|
||||
const allowedExtensions = Object.values(ACCEPT).flat().join(', ');
|
||||
toast(
|
||||
t(
|
||||
allowedExtensions
|
||||
? `The document "{{documentName}}" import has failed (only {{allowedExtensions}} files are allowed)`
|
||||
: `The document "{{documentName}}" import has failed`,
|
||||
{
|
||||
documentName: fileName,
|
||||
allowedExtensions,
|
||||
},
|
||||
),
|
||||
VariantType.ERROR,
|
||||
);
|
||||
},
|
||||
[ACCEPT, toast],
|
||||
);
|
||||
|
||||
const { getRootProps, getInputProps, open } = useDropzone({
|
||||
accept: ACCEPT,
|
||||
maxSize: MAX_FILE_SIZE.bytes,
|
||||
onDrop(acceptedFiles) {
|
||||
onDragOver(false);
|
||||
const allowedExtensions = Object.values(ACCEPT).flat();
|
||||
for (const file of acceptedFiles) {
|
||||
const ext = `.${file.name.split('.').pop()?.toLowerCase()}`;
|
||||
if (!allowedExtensions.includes(ext)) {
|
||||
toastInvalidFileType(file.name);
|
||||
continue;
|
||||
}
|
||||
importDoc([file, file.type]);
|
||||
}
|
||||
},
|
||||
@@ -94,20 +119,7 @@ export const useImport = ({ onDragOver }: UseImportProps) => {
|
||||
VariantType.ERROR,
|
||||
);
|
||||
} else {
|
||||
const allowedExtensions = Object.values(ACCEPT).flat().join(', ');
|
||||
|
||||
toast(
|
||||
t(
|
||||
allowedExtensions
|
||||
? `The document "{{documentName}}" import has failed (only {{allowedExtensions}} files are allowed)`
|
||||
: `The document "{{documentName}}" import has failed`,
|
||||
{
|
||||
documentName: rejection.file.name,
|
||||
allowedExtensions,
|
||||
},
|
||||
),
|
||||
VariantType.ERROR,
|
||||
);
|
||||
toastInvalidFileType(rejection.file.name);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user