From 0b9009180140669bda38fa561049b956fefe18f8 Mon Sep 17 00:00:00 2001 From: Geoffrey MERRAN Date: Mon, 6 Feb 2023 17:46:11 +0100 Subject: [PATCH] fix(user): user can remove profile picture --- src/components/User/Avatar.module.scss | 23 +++++++++++++++++ src/components/User/AvatarUpload.tsx | 35 +++++++++++++++++++------- src/containers/EditProfile.tsx | 12 ++++++--- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/components/User/Avatar.module.scss b/src/components/User/Avatar.module.scss index 702c36de4..2ee0c0c0e 100644 --- a/src/components/User/Avatar.module.scss +++ b/src/components/User/Avatar.module.scss @@ -1,5 +1,9 @@ @import "../../styles/Variables"; +.wrapper { + display: flex; + flex-direction: column; +} .container { border-radius: 999px; overflow: hidden; @@ -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; diff --git a/src/components/User/AvatarUpload.tsx b/src/components/User/AvatarUpload.tsx index 9ad474dea..25b89321e 100644 --- a/src/components/User/AvatarUpload.tsx +++ b/src/components/User/AvatarUpload.tsx @@ -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 } @@ -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( - () => (file ? URL.createObjectURL(file) : ipfsGatewayUrl(currentIpfs)), - [currentIpfs, file] - ) + const url = useMemo(() => { + if (file && file !== "ipfs") { + return URL.createObjectURL(file) + } + if (file === "ipfs") { + return ipfsGatewayUrl(currentIpfs) + } + return null + }, [currentIpfs, file]) const onFileChange = () => { if (inputFileRef.current) { @@ -42,8 +47,11 @@ export function AvatarUpload({ inputFileRef.current?.click() } + const handleRemovePicture = useCallback(() => { + onChange(null) + }, [onChange]) return ( - <> +
+ {url && ( + + )} - +
) } diff --git a/src/containers/EditProfile.tsx b/src/containers/EditProfile.tsx index f56c22cd3..decb15ea7 100644 --- a/src/containers/EditProfile.tsx +++ b/src/containers/EditProfile.tsx @@ -78,7 +78,7 @@ export function EditProfile() { } }, [safeData]) - const [avatarFile, setAvatarFile] = useState(null) + const [avatarFile, setAvatarFile] = useState("ipfs") const [data, setData] = useState({ name: user.name || "", description: user.description || "", @@ -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) @@ -200,7 +200,11 @@ export function EditProfile() {
- tzProfiles can take a while to synchronize, sometimes up to 1 day.
If you've updated your tzProfile recently but the changes are not reflected here, please wait a little bit. + tzProfiles can take a while to synchronize, + sometimes up to 1 day. +
+ If you've updated your tzProfile recently but the changes are not + reflected here, please wait a little bit.
)}