This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added image upload functionality (#27)
* added image upload functionality * Image upload whth linear progress. This closes #14
- Loading branch information
1 parent
9a533a5
commit 0b805c2
Showing
6 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,85 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { gql, useMutation } from '@apollo/client'; | ||
import { | ||
Dialog, | ||
DialogTitle, | ||
DialogContent, | ||
DialogActions, | ||
DialogButton, | ||
} from '@rmwc/dialog'; | ||
import '@rmwc/dialog/styles'; | ||
import { LinearProgress } from '@rmwc/linear-progress'; | ||
import '@rmwc/linear-progress/styles'; | ||
|
||
const MUTATION = gql` | ||
mutation ($file: Upload!) { | ||
upload(file: $file) | ||
} | ||
`; | ||
|
||
const UploadDialog = ({ open, setOpen }) => { | ||
const [mutate, { data, loading, error }] = useMutation(MUTATION); | ||
let history = useHistory(); | ||
|
||
const onChange = ({ | ||
target: { | ||
validity, | ||
files: [file], | ||
}, | ||
}) => { | ||
if (validity.valid) mutate({ variables: { file } }); | ||
}; | ||
|
||
if (data && data.upload) { | ||
// console.log(data.upload); | ||
setTimeout(() => { | ||
history.push('/'); | ||
history.go(0); | ||
setOpen(false); | ||
}, 2000); | ||
} | ||
if (loading) { | ||
console.log(loading); | ||
} | ||
if (error) { | ||
console.log(error); | ||
} | ||
|
||
return ( | ||
<Dialog | ||
open={open} | ||
onClose={(evt) => { | ||
console.log(evt.detail.action); | ||
setOpen(false); | ||
}} | ||
onClosed={(evt) => console.log(evt.detail.action)} | ||
> | ||
<DialogTitle>Upload Photo</DialogTitle> | ||
<DialogContent> | ||
<input type="file" required onChange={onChange} /> | ||
<br /> | ||
{loading && ( | ||
<> | ||
<br /> | ||
<LinearProgress /> | ||
</> | ||
)} | ||
{data && data.upload && ( | ||
<span style={{ color: 'green' }}>Photo uploaded successfully!</span> | ||
)} | ||
</DialogContent> | ||
<DialogActions> | ||
<DialogButton action="close">Close</DialogButton> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
UploadDialog.propTypes = { | ||
open: PropTypes.bool, | ||
setOpen: PropTypes.func, | ||
}; | ||
|
||
export default UploadDialog; |
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,16 @@ | ||
import React, { useState } from 'react'; | ||
import { TopAppBarActionItem } from '@rmwc/top-app-bar'; | ||
import UploadDialog from '../UploadDialog'; | ||
|
||
const Upload = () => { | ||
const [open, setOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<UploadDialog open={open} setOpen={setOpen} /> | ||
<TopAppBarActionItem icon="file_upload" onClick={() => setOpen(true)} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default Upload; |