-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1884 from Inist-CNRS/feat/import-hidden-resources
Feat import hidden resources
- Loading branch information
Showing
6 changed files
with
152 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { compose } from 'recompose'; | ||
import { polyglot as polyglotPropTypes } from '../../propTypes'; | ||
import translate from 'redux-polyglot/translate'; | ||
import UploadIcon from '@mui/icons-material/Upload'; | ||
import { Button, CircularProgress, styled } from '@mui/material'; | ||
import { importHiddenResources } from '../api/hiddenResource'; | ||
import { toast } from '../../../../common/tools/toast'; | ||
import { useLocation, Redirect } from 'react-router-dom'; | ||
|
||
const VisuallyHiddenInput = styled('input')({ | ||
clip: 'rect(0 0 0 0)', | ||
clipPath: 'inset(50%)', | ||
height: 1, | ||
overflow: 'hidden', | ||
position: 'absolute', | ||
bottom: 0, | ||
left: 0, | ||
whiteSpace: 'nowrap', | ||
width: 1, | ||
}); | ||
|
||
const ImportButton = ({ p: polyglot }) => { | ||
const buttonLabel = polyglot.t('import'); | ||
const location = useLocation(); | ||
const [uploading, setUploading] = useState(false); | ||
const [done, setDone] = useState(false); | ||
|
||
const handleFileChange = async event => { | ||
setUploading(true); | ||
const file = event.target.files[0]; | ||
const formData = new FormData(); | ||
formData.append('file', file); | ||
const response = await importHiddenResources(formData); | ||
setUploading(false); | ||
if (response.error) { | ||
toast(polyglot.t('import_error'), { | ||
type: toast.TYPE.ERROR, | ||
}); | ||
} else { | ||
setDone(true); | ||
toast(polyglot.t('import_successful'), { | ||
type: toast.TYPE.SUCCESS, | ||
}); | ||
} | ||
}; | ||
|
||
if (done) { | ||
return <Redirect to={location} />; | ||
} | ||
|
||
return ( | ||
<Button | ||
component="label" | ||
variant="text" | ||
className="export" | ||
startIcon={ | ||
uploading ? <CircularProgress size="1em" /> : <UploadIcon /> | ||
} | ||
disabled={uploading} | ||
> | ||
{buttonLabel} | ||
<VisuallyHiddenInput | ||
onChange={handleFileChange} | ||
type="file" | ||
accept="application/json" | ||
/> | ||
</Button> | ||
); | ||
}; | ||
|
||
ImportButton.propTypes = { | ||
p: polyglotPropTypes.isRequired, | ||
}; | ||
|
||
export default compose(translate)(ImportButton); |
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