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

fix(user): user can remove profile picture #564

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions src/components/User/Avatar.module.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@import "../../styles/Variables";

.wrapper {
display: flex;
flex-direction: column;
}
.container {
border-radius: 999px;
overflow: hidden;
Expand Down Expand Up @@ -70,7 +74,26 @@
}
}

.remove {
background: none;
border: none;
color: var(--color-black);
font-weight: bold;
cursor: pointer;
margin-top: 4px;
&:hover {
text-decoration: underline;
}
}
@media (max-width: $breakpoint-md) {
.remove {
max-width: 250px;
}
}
@media (max-width: $breakpoint-sm) {
.remove {
max-width: 162px;
}
.avatar-upload {
i {
height: 48px;
Expand Down
35 changes: 26 additions & 9 deletions src/components/User/AvatarUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import effect from "../../styles/Effects.module.scss"
import style from "./Avatar.module.scss"
import cs from "classnames"
import { ipfsGatewayUrl } from "../../services/Ipfs"
import { MouseEventHandler, useMemo, useRef } from "react"
import { MouseEventHandler, useCallback, useMemo, useRef } from "react"

interface Props {
currentIpfs?: string
file?: File | null
onChange: (file: File) => void
file?: File | "ipfs" | null
onChange: (file: File | null) => void
className?: string
}

Expand All @@ -23,10 +23,15 @@ export function AvatarUpload({
// - is there a file ? if so get a local url to display it
// - is there a current ipfs url to display url
// - url is empty
const url = useMemo<string | null>(
() => (file ? URL.createObjectURL(file) : ipfsGatewayUrl(currentIpfs)),
[currentIpfs, file]
)
const url = useMemo<string | null>(() => {
if (file && file !== "ipfs") {
return URL.createObjectURL(file)
}
if (file === "ipfs") {
return ipfsGatewayUrl(currentIpfs)
}
return null
}, [currentIpfs, file])

const onFileChange = () => {
if (inputFileRef.current) {
Expand All @@ -42,8 +47,11 @@ export function AvatarUpload({
inputFileRef.current?.click()
}

const handleRemovePicture = useCallback(() => {
onChange(null)
}, [onChange])
return (
<>
<div className={style.wrapper}>
<button
type="button"
className={cs(
Expand All @@ -63,6 +71,15 @@ export function AvatarUpload({
<span>upload {url ? "a new" : "an"} image</span>
{url && <i className="fa-solid fa-arrow-up-from-bracket" />}
</button>
{url && (
<button
type="button"
className={style.remove}
onClick={handleRemovePicture}
>
remove image
</button>
)}
<input
ref={inputFileRef}
type="file"
Expand All @@ -71,6 +88,6 @@ export function AvatarUpload({
accept="image/*"
onChange={onFileChange}
/>
</>
</div>
)
}
12 changes: 8 additions & 4 deletions src/containers/EditProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function EditProfile() {
}
}, [safeData])

const [avatarFile, setAvatarFile] = useState<File | null>(null)
const [avatarFile, setAvatarFile] = useState<File | "ipfs" | null>("ipfs")
const [data, setData] = useState({
name: user.name || "",
description: user.description || "",
Expand All @@ -103,9 +103,9 @@ export function EditProfile() {
validationSchema={Schema}
onSubmit={(values) => {
const f = new FormData()
if (avatarFile) {
if (avatarFile && avatarFile !== "ipfs") {
f.append("avatarFile", avatarFile)
} else if (user.avatarUri) {
} else if (avatarFile === "ipfs" && user.avatarUri) {
f.append("avatarIpfs", user.avatarUri)
}
f.append("description", values.description)
Expand Down Expand Up @@ -200,7 +200,11 @@ export function EditProfile() {
</div>
<Spacing size="3x-large" sm="x-large" />
<div className={style.warn}>
<strong>tzProfiles</strong> can take a while to synchronize, sometimes up to 1 day.<br/>If you've updated your tzProfile recently but the changes are not reflected here, please wait a little bit.
<strong>tzProfiles</strong> can take a while to synchronize,
sometimes up to 1 day.
<br />
If you've updated your tzProfile recently but the changes are not
reflected here, please wait a little bit.
</div>
</Form>
)}
Expand Down