Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit c8abd25

Browse files
unsupported file format
1 parent e43d2a5 commit c8abd25

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

src/components/file-step/FileSelector.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
color: $textColor;
1010
cursor: pointer;
1111

12+
&--error {
13+
color: $errorTextColor;
14+
}
15+
1216
&[data-active='true'] {
1317
background: darken($fillColor, 10%);
1418
transition: background 0.1s ease-out;
Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,69 @@
1-
import React, { useCallback, useRef } from 'react';
1+
import React, { useCallback, useRef, useState } from 'react';
22
import { useDropzone } from 'react-dropzone';
33
import { useLocale } from '../../locale/LocaleContext';
44

55
import './FileSelector.scss';
66

7-
export const FileSelector: React.FC<{ onSelected: (file: File) => void }> = ({
7+
export const FileSelector: React.FC<{
8+
onSelected: (file: File) => void
9+
}> = ({
810
onSelected
911
}) => {
10-
const onSelectedRef = useRef(onSelected);
11-
onSelectedRef.current = onSelected;
12+
const onSelectedRef = useRef(onSelected);
13+
onSelectedRef.current = onSelected;
14+
const [unsupportedFileFormat, setUnsupportedFileFormat] = useState(false)
15+
const supportedFileFormats = ["text/csv", "text/plain"]
1216

13-
const dropHandler = useCallback((acceptedFiles: File[]) => {
14-
// silently ignore if nothing to do
15-
if (acceptedFiles.length < 1) {
16-
return;
17+
const dropHandler = useCallback((acceptedFiles: File[]) => {
18+
// silently ignore if nothing to do
19+
if (acceptedFiles.length < 1) {
20+
return;
21+
}
22+
23+
const file = acceptedFiles[0];
24+
if (!supportedFileFormats.includes(file.type)) return setUnsupportedFileFormat(true)
25+
onSelectedRef.current(file);
26+
}, []);
27+
28+
const { getRootProps, getInputProps, isDragActive } = useDropzone({
29+
onDrop: dropHandler
30+
});
31+
32+
const l10n = useLocale('fileStep');
33+
34+
const errorBlock = () => {
35+
return (
36+
<div className="CSVImporter_FileSelector--error">
37+
{l10n.unsupportedFileFormatError}. {l10n.activeDragDropPrompt}
38+
</div>
39+
)
1740
}
1841

19-
const file = acceptedFiles[0];
20-
onSelectedRef.current(file);
21-
}, []);
22-
23-
const { getRootProps, getInputProps, isDragActive } = useDropzone({
24-
onDrop: dropHandler
25-
});
26-
27-
const l10n = useLocale('fileStep');
28-
29-
return (
30-
<div
31-
className="CSVImporter_FileSelector"
32-
data-active={!!isDragActive}
33-
{...getRootProps()}
34-
>
35-
<input {...getInputProps()} />
36-
37-
{isDragActive ? (
38-
<span>{l10n.activeDragDropPrompt}</span>
39-
) : (
40-
<span>{l10n.initialDragDropPrompt}</span>
41-
)}
42-
</div>
43-
);
44-
};
42+
return (
43+
<div
44+
className="CSVImporter_FileSelector"
45+
data-active={!!isDragActive}
46+
{...getRootProps()}
47+
>
48+
<input {...getInputProps()} />
49+
50+
{isDragActive ? (
51+
<span>
52+
{
53+
unsupportedFileFormat ?
54+
errorBlock() :
55+
l10n.activeDragDropPrompt
56+
}
57+
</span>
58+
) : (
59+
<span>
60+
{
61+
unsupportedFileFormat ?
62+
errorBlock() :
63+
l10n.initialDragDropPrompt
64+
}
65+
</span>
66+
)}
67+
</div>
68+
);
69+
};

src/locale/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ImporterLocale {
1111

1212
getImportError: (message: string) => string;
1313
getDataFormatError: (message: string) => string;
14+
unsupportedFileFormatError: string;
1415
goBackButton: string;
1516
nextButton: string;
1617

@@ -71,6 +72,7 @@ export const enUS: ImporterLocale = {
7172

7273
getImportError: (message) => `Import error: ${message}`,
7374
getDataFormatError: (message) => `Please check data formatting: ${message}`,
75+
unsupportedFileFormatError: 'File format not supported',
7476
goBackButton: 'Go Back',
7577
nextButton: 'Choose columns',
7678

@@ -133,6 +135,7 @@ export const deDE: ImporterLocale = {
133135
getImportError: (message) => `Fehler beim Import: ${message}`,
134136
getDataFormatError: (message: string) =>
135137
`Bitte Datenformat überprüfen: ${message}`,
138+
unsupportedFileFormatError : 'Dateiformat nicht unterstützt',
136139
goBackButton: 'Zurück',
137140

138141
rawFileContentsHeading: 'Originaler Datei-Inhalt',
@@ -193,6 +196,7 @@ export const itIT: ImporterLocale = {
193196

194197
getImportError: (message) => `Errore durante l'importazione: ${message}`,
195198
getDataFormatError: (message) => `Si prega di controllare il formato dei dati: ${message}`,
199+
unsupportedFileFormatError : 'Formato file non supportato',
196200
goBackButton: 'Torna indietro',
197201
nextButton: 'Seleziona le colonne',
198202

@@ -253,6 +257,7 @@ export const ptBR: ImporterLocale = {
253257

254258
getImportError: (message) => `Erro ao importar: ${message}`,
255259
getDataFormatError: (message) => `Por favor confira a formatação dos dados: ${message}`,
260+
unsupportedFileFormatError : 'Formato de arquivo não suportado',
256261
goBackButton: 'Voltar',
257262
nextButton: 'Escolher Colunas',
258263

@@ -314,6 +319,7 @@ export const daDK: ImporterLocale = {
314319
getImportError: (message) => `Import-fejl: ${message}`,
315320
getDataFormatError: (message) =>
316321
`Kontrollér venligst data-formatering: ${message}`,
322+
unsupportedFileFormatError : 'Filformat understøttes ikke',
317323
goBackButton: "Gå tilbage",
318324
nextButton: "Vælg kolonner",
319325

@@ -375,6 +381,7 @@ export const trTR: ImporterLocale = {
375381
getImportError: (message) => `Import hatası: ${message}`,
376382
getDataFormatError: (message) =>
377383
`Lütfen veri formatını kontrol edin: ${message}`,
384+
unsupportedFileFormatError : 'Dosya formatı desteklenmiyor',
378385
goBackButton: "Geri",
379386
nextButton: "Kolonları Seç",
380387

test/basics.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ describe('importer basics', () => {
6262
expect(await fileInput.getAttribute('type')).to.equal('file');
6363
});
6464

65+
it('with unsupported file selected', async () => {
66+
const filePath = path.resolve(__dirname, './fixtures/img.png');
67+
68+
const fileInput = await getDriver().findElement(By.xpath('//input'));
69+
await fileInput.sendKeys(filePath);
70+
71+
await getDriver().wait(
72+
until.elementLocated(By.xpath('//*[contains(., "File format not supported")]')),
73+
300 // extra time
74+
);
75+
76+
const errorBlock = await getDriver().findElement(By.xpath('//*[contains(., "File format not supported")]'))
77+
expect(await errorBlock.getText()).to.include("File format not supported")
78+
})
79+
6580
describe('with file selected', () => {
6681
beforeEach(async () => {
6782
const filePath = path.resolve(__dirname, './fixtures/simple.csv');

test/fixtures/img.png

8.44 KB
Loading

0 commit comments

Comments
 (0)