-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(settings): allow importing videos data from a json file
- Loading branch information
Showing
6 changed files
with
119 additions
and
10 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
85 changes: 85 additions & 0 deletions
85
src/ui/components/pages/Settings/SavedVideosOptions/ImportVideosData.tsx
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,85 @@ | ||
import React, { useState, useRef, ChangeEvent, MouseEvent } from 'react'; | ||
import { Button } from '@mui/material'; | ||
import FileUploadIcon from '@mui/icons-material/FileUpload'; | ||
import { Nullable } from 'types'; | ||
import { readFile } from 'helpers/file'; | ||
import { nanoid } from '@reduxjs/toolkit'; | ||
import { useAppDispatch } from 'store'; | ||
import { setVideos } from 'store/reducers/videos'; | ||
import { Alert } from 'ui/components/shared'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
interface IImportVideosDataProps {} | ||
|
||
export const id = nanoid(); | ||
export const icon = <FileUploadIcon />; | ||
export const label = 'Import videos data'; | ||
export const color = 'secondary'; | ||
|
||
function ImportVideosData(props: IImportVideosDataProps) { | ||
const [openSuccessAlert, setOpenSuccessAlert] = useState(false); | ||
const fileInputRef = useRef<Nullable<HTMLInputElement>>(null); | ||
const dispatch = useAppDispatch(); | ||
|
||
const importVideos = (event: ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
if (!file) { | ||
return; | ||
} | ||
try { | ||
readFile(file).then((content) => { | ||
const videos = JSON.parse(content as string); | ||
dispatch(setVideos({ list: videos })); | ||
setOpenSuccessAlert(true); | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
variant="contained" | ||
color={color} | ||
startIcon={icon} | ||
sx={{ textTransform: 'none' }} | ||
onClick={() => { | ||
fileInputRef.current?.click(); | ||
}} | ||
> | ||
{label} | ||
</Button> | ||
<input | ||
type="file" | ||
ref={fileInputRef} | ||
style={{ | ||
display: 'none', | ||
visibility: 'hidden', | ||
overflow: 'hidden', | ||
width: 0, | ||
height: 0, | ||
}} | ||
accept=".json" | ||
onClick={(event: MouseEvent<HTMLInputElement>) => { | ||
event.currentTarget.value = ''; | ||
}} | ||
onChange={importVideos} | ||
/> | ||
{ReactDOM.createPortal( | ||
<Alert | ||
open={openSuccessAlert} | ||
severity="success" | ||
closable | ||
syncOpen | ||
onClose={() => setOpenSuccessAlert(false)} | ||
> | ||
Videos imported successfully! | ||
</Alert>, | ||
document.getElementById('layout-content-portal')!, | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default ImportVideosData; |
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