72-delete-avatar-and-cover (#255)

* allow to delete profile pic

* allow for removing banner
zio/stable
Ansh 2023-03-02 16:53:18 -08:00 committed by GitHub
parent aeb04a2ed0
commit e592e59f4e
4 changed files with 45 additions and 28 deletions

View File

@ -2,8 +2,8 @@ import {makeAutoObservable, runInAction} from 'mobx'
import {PickedMedia} from 'lib/media/picker' import {PickedMedia} from 'lib/media/picker'
import { import {
AppBskyActorGetProfile as GetProfile, AppBskyActorGetProfile as GetProfile,
AppBskyActorProfile as Profile,
AppBskySystemDeclRef, AppBskySystemDeclRef,
AppBskyActorUpdateProfile,
} from '@atproto/api' } from '@atproto/api'
type DeclRef = AppBskySystemDeclRef.Main type DeclRef = AppBskySystemDeclRef.Main
import {extractEntities} from 'lib/strings/rich-text-detection' import {extractEntities} from 'lib/strings/rich-text-detection'
@ -132,9 +132,9 @@ export class ProfileViewModel {
} }
async updateProfile( async updateProfile(
updates: Profile.Record, updates: AppBskyActorUpdateProfile.InputSchema,
newUserAvatar: PickedMedia | undefined, newUserAvatar: PickedMedia | undefined | null,
newUserBanner: PickedMedia | undefined, newUserBanner: PickedMedia | undefined | null,
) { ) {
if (newUserAvatar) { if (newUserAvatar) {
const res = await apilib.uploadBlob( const res = await apilib.uploadBlob(
@ -146,6 +146,8 @@ export class ProfileViewModel {
cid: res.data.cid, cid: res.data.cid,
mimeType: newUserAvatar.mime, mimeType: newUserAvatar.mime,
} }
} else {
updates.avatar = null
} }
if (newUserBanner) { if (newUserBanner) {
const res = await apilib.uploadBlob( const res = await apilib.uploadBlob(
@ -157,6 +159,8 @@ export class ProfileViewModel {
cid: res.data.cid, cid: res.data.cid,
mimeType: newUserBanner.mime, mimeType: newUserBanner.mime,
} }
} else {
updates.banner = null
} }
await this.rootStore.api.app.bsky.actor.updateProfile(updates) await this.rootStore.api.app.bsky.actor.updateProfile(updates)
await this.rootStore.me.load() await this.rootStore.me.load()

View File

@ -44,20 +44,30 @@ export function Component({
const [description, setDescription] = useState<string>( const [description, setDescription] = useState<string>(
profileView.description || '', profileView.description || '',
) )
const [userBanner, setUserBanner] = useState<string | undefined>( const [userBanner, setUserBanner] = useState<string | undefined | null>(
profileView.banner, profileView.banner,
) )
const [userAvatar, setUserAvatar] = useState<string | undefined>( const [userAvatar, setUserAvatar] = useState<string | undefined | null>(
profileView.avatar, profileView.avatar,
) )
const [newUserBanner, setNewUserBanner] = useState<PickedMedia | undefined>() const [newUserBanner, setNewUserBanner] = useState<
const [newUserAvatar, setNewUserAvatar] = useState<PickedMedia | undefined>() PickedMedia | undefined | null
>()
const [newUserAvatar, setNewUserAvatar] = useState<
PickedMedia | undefined | null
>()
const onPressCancel = () => { const onPressCancel = () => {
store.shell.closeModal() store.shell.closeModal()
} }
const onSelectNewAvatar = async (img: PickedMedia) => { const onSelectNewAvatar = async (img: PickedMedia | null) => {
track('EditProfile:AvatarSelected') track('EditProfile:AvatarSelected')
try { try {
// if img is null, user selected "remove avatar"
if (!img) {
setNewUserAvatar(null)
setUserAvatar(null)
return
}
const finalImg = await compressIfNeeded(img, 1000000) const finalImg = await compressIfNeeded(img, 1000000)
setNewUserAvatar({mediaType: 'photo', ...finalImg}) setNewUserAvatar({mediaType: 'photo', ...finalImg})
setUserAvatar(finalImg.path) setUserAvatar(finalImg.path)
@ -65,7 +75,12 @@ export function Component({
setError(cleanError(e)) setError(cleanError(e))
} }
} }
const onSelectNewBanner = async (img: PickedMedia) => { const onSelectNewBanner = async (img: PickedMedia | null) => {
if (!img) {
setNewUserBanner(null)
setUserBanner(null)
return
}
track('EditProfile:BannerSelected') track('EditProfile:BannerSelected')
try { try {
const finalImg = await compressIfNeeded(img, 1000000) const finalImg = await compressIfNeeded(img, 1000000)

View File

@ -31,7 +31,7 @@ export function UserAvatar({
handle: string handle: string
displayName: string | undefined displayName: string | undefined
avatar?: string | null avatar?: string | null
onSelectNewAvatar?: (img: PickedMedia) => void onSelectNewAvatar?: (img: PickedMedia | null) => void
}) { }) {
const store = useStores() const store = useStores()
const pal = usePalette('default') const pal = usePalette('default')
@ -97,14 +97,13 @@ export function UserAvatar({
) )
}, },
}, },
// TODO: Remove avatar https://github.com/bluesky-social/social-app/issues/122 {
// { label: 'Remove',
// label: 'Remove', icon: ['far', 'trash-can'] as IconProp,
// icon: ['far', 'trash-can'], onPress: async () => {
// onPress: () => { onSelectNewAvatar?.(null)
// // Remove avatar API call },
// }, },
// },
] ]
// onSelectNewAvatar is only passed as prop on the EditProfile component // onSelectNewAvatar is only passed as prop on the EditProfile component
return onSelectNewAvatar ? ( return onSelectNewAvatar ? (

View File

@ -25,7 +25,7 @@ export function UserBanner({
onSelectNewBanner, onSelectNewBanner,
}: { }: {
banner?: string | null banner?: string | null
onSelectNewBanner?: (img: PickedMedia) => void onSelectNewBanner?: (img: PickedMedia | null) => void
}) { }) {
const store = useStores() const store = useStores()
const pal = usePalette('default') const pal = usePalette('default')
@ -70,14 +70,13 @@ export function UserBanner({
) )
}, },
}, },
// TODO: Remove banner https://github.com/bluesky-social/social-app/issues/122 {
// { label: 'Remove',
// label: 'Remove', icon: ['far', 'trash-can'] as IconProp,
// icon: ['far', 'trash-can'], onPress: () => {
// onPress: () => { onSelectNewBanner?.(null)
// // Remove banner api call },
// }, },
// },
] ]
const renderSvg = () => ( const renderSvg = () => (