aboutsummaryrefslogtreecommitdiff
path: root/src/components/UploadImage/UploadImage.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/UploadImage/UploadImage.tsx')
-rw-r--r--src/components/UploadImage/UploadImage.tsx42
1 files changed, 18 insertions, 24 deletions
diff --git a/src/components/UploadImage/UploadImage.tsx b/src/components/UploadImage/UploadImage.tsx
index 42ee989..e6f6d05 100644
--- a/src/components/UploadImage/UploadImage.tsx
+++ b/src/components/UploadImage/UploadImage.tsx
@@ -1,4 +1,4 @@
-import React, { useRef } from 'react';
+import React, { useState } from 'react';
import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField';
import Dialog from '@material-ui/core/Dialog';
@@ -6,39 +6,33 @@ import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
-import { User } from 'which-types';
-import { patch } from '../../requests';
interface PropTypes {
- displayD: boolean;
- setDisplayD: (d: boolean) => void;
- setUserInfo: (a: User) => void;
- setUser: (a: User) => void
+ isOpen: boolean;
+ setIsOpen: (value: boolean) => void;
+ callback: (url: string) => void;
}
-const UploadImage: React.FC<PropTypes> = ({
- displayD, setDisplayD, setUserInfo, setUser
-}) => {
- const urlRef = useRef<HTMLInputElement>(null);
+const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => {
+ const [url, setUrl] = useState('');
const handleClose = () => {
- setDisplayD(false);
+ setIsOpen(false);
};
- const updateAvatar = () => {
- const id = localStorage.getItem('userId');
- const newAvatar = urlRef.current?.value;
- patch(`/users/${id}`, { avatarUrl: newAvatar }).then(res => {
- setUserInfo(res.data);
- setUser(res.data);
- });
- setDisplayD(false);
+ const handleSubmit = () => {
+ callback(url || '');
+ setIsOpen(false);
+ };
+
+ const handleChange = (event:React.ChangeEvent<HTMLInputElement>) => {
+ setUrl(event.target.value);
};
return (
<div>
- <Dialog open={displayD} onClose={handleClose}>
- <DialogTitle id="form-dialog-title">Upload an Image</DialogTitle>
+ <Dialog open={isOpen} onClose={handleClose}>
+ <DialogTitle>Upload an Image</DialogTitle>
<DialogContent>
<DialogContentText>
Unfortunetly we do not support uploading images yet. Please provide a valid URL to your image.
@@ -51,14 +45,14 @@ const UploadImage: React.FC<PropTypes> = ({
type="text"
fullWidth
autoComplete="off"
- inputRef={urlRef}
+ onChange={handleChange}
/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Cancel
</Button>
- <Button onClick={updateAvatar} color="primary">
+ <Button onClick={handleSubmit} color="primary" disabled={!url.length}>
Submit
</Button>
</DialogActions>