From 09b32b06ccdfe518b5d986089a6412555fdb1c52 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Mon, 5 Feb 2024 14:11:46 +0300 Subject: [PATCH 1/2] Fix subscribe logic --- .../Author/AuthorBadge/AuthorBadge.tsx | 1 - .../Author/AuthorCard/AuthorCard.tsx | 53 +++++++++++-------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/components/Author/AuthorBadge/AuthorBadge.tsx b/src/components/Author/AuthorBadge/AuthorBadge.tsx index 24e885e7..63c299ea 100644 --- a/src/components/Author/AuthorBadge/AuthorBadge.tsx +++ b/src/components/Author/AuthorBadge/AuthorBadge.tsx @@ -80,7 +80,6 @@ export const AuthorBadge = (props: Props) => { () => { setIsFollowed(props.isFollowed.value) }, - { defer: true }, ), ) diff --git a/src/components/Author/AuthorCard/AuthorCard.tsx b/src/components/Author/AuthorCard/AuthorCard.tsx index d7cc72e6..687e3094 100644 --- a/src/components/Author/AuthorCard/AuthorCard.tsx +++ b/src/components/Author/AuthorCard/AuthorCard.tsx @@ -2,7 +2,7 @@ import type { Author, Community } from '../../../graphql/schema/core.gen' import { openPage, redirectPage } from '@nanostores/router' import { clsx } from 'clsx' -import { createEffect, createMemo, createSignal, For, on, onMount, Show } from 'solid-js' +import { createEffect, createMemo, createSignal, For, onMount, Show } from 'solid-js' import { useFollowing } from '../../../context/following' import { useLocalize } from '../../../context/localize' @@ -41,33 +41,30 @@ export const AuthorCard = (props: Props) => { const [subscriptionFilter, setSubscriptionFilter] = createSignal('all') const [isFollowed, setIsFollowed] = createSignal() const isProfileOwner = createMemo(() => author()?.slug === props.author.slug) - const isSubscribed = () => props.followers?.some((entity) => entity.id === author()?.id) - createEffect( - on( - () => props.followers, - () => { - setIsFollowed(isSubscribed()) - }, - { defer: true }, - ), - ) - + const { subscriptions } = useFollowing() const { setFollowing } = useFollowing() + const isOwnerSubscribed = (authorId: number) => { + return !!subscriptions?.authors.some((a) => a.id === authorId) + } + + onMount(() => { + setAuthorSubs(props.following) + }) + + createEffect(() => { + setIsFollowed(isOwnerSubscribed(props.author?.id)) + }) const name = createMemo(() => { if (lang() !== 'ru' && isCyrillic(props.author.name)) { if (props.author.name === 'Дискурс') { return 'Discours' } - return translit(props.author.name) } - return props.author.name }) - onMount(() => setAuthorSubs(props.following)) - // TODO: reimplement AuthorCard const { changeSearchParams } = useRouter() const initChat = () => { @@ -103,7 +100,7 @@ export const AuthorCard = (props: Props) => { } const followButtonText = createMemo(() => { - if (isFollowed()) { + if (isOwnerSubscribed(props.author?.id)) { return ( <> {t('Following')} @@ -111,7 +108,6 @@ export const AuthorCard = (props: Props) => { ) } - return t('Follow') }) @@ -206,12 +202,11 @@ export const AuthorCard = (props: Props) => { - - +