This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e43d2a5
commit c8abd25
Showing
5 changed files
with
85 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,69 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
import React, { useCallback, useRef, useState } from 'react'; | ||
import { useDropzone } from 'react-dropzone'; | ||
import { useLocale } from '../../locale/LocaleContext'; | ||
|
||
import './FileSelector.scss'; | ||
|
||
export const FileSelector: React.FC<{ onSelected: (file: File) => void }> = ({ | ||
export const FileSelector: React.FC<{ | ||
onSelected: (file: File) => void | ||
}> = ({ | ||
onSelected | ||
}) => { | ||
const onSelectedRef = useRef(onSelected); | ||
onSelectedRef.current = onSelected; | ||
const onSelectedRef = useRef(onSelected); | ||
onSelectedRef.current = onSelected; | ||
const [unsupportedFileFormat, setUnsupportedFileFormat] = useState(false) | ||
const supportedFileFormats = ["text/csv", "text/plain"] | ||
|
||
const dropHandler = useCallback((acceptedFiles: File[]) => { | ||
// silently ignore if nothing to do | ||
if (acceptedFiles.length < 1) { | ||
return; | ||
const dropHandler = useCallback((acceptedFiles: File[]) => { | ||
// silently ignore if nothing to do | ||
if (acceptedFiles.length < 1) { | ||
return; | ||
} | ||
|
||
const file = acceptedFiles[0]; | ||
if (!supportedFileFormats.includes(file.type)) return setUnsupportedFileFormat(true) | ||
onSelectedRef.current(file); | ||
}, []); | ||
|
||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ | ||
onDrop: dropHandler | ||
}); | ||
|
||
const l10n = useLocale('fileStep'); | ||
|
||
const errorBlock = () => { | ||
return ( | ||
<div className="CSVImporter_FileSelector--error"> | ||
{l10n.unsupportedFileFormatError}. {l10n.activeDragDropPrompt} | ||
</div> | ||
) | ||
} | ||
|
||
const file = acceptedFiles[0]; | ||
onSelectedRef.current(file); | ||
}, []); | ||
|
||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ | ||
onDrop: dropHandler | ||
}); | ||
|
||
const l10n = useLocale('fileStep'); | ||
|
||
return ( | ||
<div | ||
className="CSVImporter_FileSelector" | ||
data-active={!!isDragActive} | ||
{...getRootProps()} | ||
> | ||
<input {...getInputProps()} /> | ||
|
||
{isDragActive ? ( | ||
<span>{l10n.activeDragDropPrompt}</span> | ||
) : ( | ||
<span>{l10n.initialDragDropPrompt}</span> | ||
)} | ||
</div> | ||
); | ||
}; | ||
return ( | ||
<div | ||
className="CSVImporter_FileSelector" | ||
data-active={!!isDragActive} | ||
{...getRootProps()} | ||
> | ||
<input {...getInputProps()} /> | ||
|
||
{isDragActive ? ( | ||
<span> | ||
{ | ||
unsupportedFileFormat ? | ||
errorBlock() : | ||
l10n.activeDragDropPrompt | ||
} | ||
</span> | ||
) : ( | ||
<span> | ||
{ | ||
unsupportedFileFormat ? | ||
errorBlock() : | ||
l10n.initialDragDropPrompt | ||
} | ||
</span> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.