diff --git a/src/pages/Home.js b/src/pages/Home.js index 38fca3a..7697910 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -84,6 +84,39 @@ const ThirdPartyConnections = ({user}) => { const Profile = () => { const [user] = useLoginContext(); + const [file, setFile] = useState(null); + const fileSelector = useRef(); + const [error, setError] = useState(null); + + // For some reason this has to be done for onDrop to work + const onDragOver = (event) => { + event.stopPropagation(); + event.preventDefault(); + }; + + const onDrop = (event) => { + event.preventDefault(); + const { dataTransfer } = event; + if (!dataTransfer.files.length) return; + + const [file] = dataTransfer.files; + fileSelector.value = file.name; + setFile(file); + }; + + const submit = async ({target: button}) => { + button.disabled = true; + if (!file) return; + + const body = new FormData(); + body.set('file', file, file.name); + // body.set('name', file.name); + + const response = await post('/api/user/avatar', body, { headers: null }); + if (!response.success) setError(response.message); + else fileSelector.current.value = null; + button.disabled = false; + }; return
Change Profile Picture