Hi everyone!
I have set up a page for a user to update their profile. Updating username, email, etc all updates and displays on my moralis server.
I have spent close to 100 hours and looked at other forum submissions and still havenβt figured out how to upload and display an image for profile pic, soβ¦ I am asking for any assistance.
Please review the attached code and help find a solution. This code is only the part a user uses to upload the file.
Your help is greatly appreciated.
import React from 'react';
import { useState } from 'react';
import { useMoralis, useMoralisFile } from "react-moralis";
function User() {
const { user } = useMoralis();
const { saveFile } = useMoralisFile();
const [photoFile, setPhotoFile] = useState();
const [photoFileName, setPhotoFileName] = useState();
const onChangePhoto = e => {
setPhotoFile(e.target.files[0]);
setPhotoFileName(e.target.files[0].name)
}
const onSubmitPhoto = e => {
const file = photoFile;
const name = photoFileName;
saveFile(name, file);
user.set('profilePic', file);
}
return (
<>
<div className="profile-pic-container">
<img className="profile-pic" src={user.attributes.profilePic} alt=""/>
</div>
<div className="profile-pic-update-container">
<form onSubmit={onSubmitPhoto}>
<div className="mb-3">
<label htmlFor="profilePhoto" className="form-label">Select a Profile Pic</label>
<input className="form-control" type="file" accept="image/*" multiple="false" id="profilePhoto" onChange={onChangePhoto} />
</div>
<input type="sumbit" value="Upload" className="btn btn-primary btn-block mt-1" onClick={onSubmitPhoto}/>
</form>
</div>
</>
)
}
export default User;