Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make profile picture editable #8

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/pages/u/[userId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,26 @@ const Profile = () => {
}
}, [user]);

const [selectedFilename, setSelectedFilename] = useState<any>("");
if (!user) {
return <Main> User not found </Main>;
}
const handleFileChange = (event: any) => {
const file = event.target.files[0];

if (file) {
const reader = new FileReader();
reader.onload = (e) => {
if (e.target) {
setSelectedFilename(e.target.result);
}
};
reader.readAsDataURL(file);
} else {
setSelectedFilename("");
}
};

return (
<Main>
<Image
Expand All @@ -90,13 +107,30 @@ const Profile = () => {
<div className="p-5 md:w-[70rem] mx-auto overflow-auto h-full z-10 font-libre">
<div className="grid md:grid-cols-2 grid-cols-1">
<div className="md:m-0 hidden md:flex md:flex-col md:gap-6">
<img
src={attributes?.profile?.photo ?? ""}
alt="pfp"
width={300}
height={300}
className="rounded-xl shadow-lg"
/>
{user.profile?.photo || selectedFilename ? (
<img
src={user.profile?.photo || selectedFilename}
alt="pfp"
width={300}
height={300}
className="rounded-full shadow-lg w-[300px] h-[300px]"
/>
) : (
<div className="form-control w-[300px] h-[300px] rounded-full w-full max-w-xs rounded-full shadow-lg">
<label
htmlFor="file-input"
className="cursor-pointer flex justify-center items-center w-full h-full"
>
Upload Photo
</label>
<input
type="file"
id="file-input"
className="hidden"
onChange={handleFileChange}
/>
</div>
)}
</div>
<div>
<div className="flex flex-row justify-between items-center">
Expand Down