From f2315411b229fde90eda8fee0f23add1dc1d3474 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Mon, 5 Feb 2024 15:34:47 +0300 Subject: [PATCH] Fix subscribe logic (refactoring) --- src/components/Author/AuthorCard/AuthorCard.tsx | 6 +----- src/components/Feed/Beside.tsx | 12 +++--------- src/components/Views/AllAuthors.tsx | 11 ++++++++++- src/context/following.tsx | 7 +++++++ 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/components/Author/AuthorCard/AuthorCard.tsx b/src/components/Author/AuthorCard/AuthorCard.tsx index 687e3094..4a8582dc 100644 --- a/src/components/Author/AuthorCard/AuthorCard.tsx +++ b/src/components/Author/AuthorCard/AuthorCard.tsx @@ -41,11 +41,7 @@ export const AuthorCard = (props: Props) => { const [subscriptionFilter, setSubscriptionFilter] = createSignal('all') const [isFollowed, setIsFollowed] = createSignal() const isProfileOwner = createMemo(() => author()?.slug === props.author.slug) - const { subscriptions } = useFollowing() - const { setFollowing } = useFollowing() - const isOwnerSubscribed = (authorId: number) => { - return !!subscriptions?.authors.some((a) => a.id === authorId) - } + const { setFollowing, isOwnerSubscribed } = useFollowing() onMount(() => { setAuthorSubs(props.following) diff --git a/src/components/Feed/Beside.tsx b/src/components/Feed/Beside.tsx index f11de91c..f4aa7945 100644 --- a/src/components/Feed/Beside.tsx +++ b/src/components/Feed/Beside.tsx @@ -3,7 +3,7 @@ import type { Author, Shout, Topic } from '../../graphql/schema/core.gen' import { clsx } from 'clsx' -import { createEffect, createSignal, For, Show } from 'solid-js' +import { For, Show } from 'solid-js' import { useFollowing } from '../../context/following' import { useLocalize } from '../../context/localize' @@ -30,12 +30,7 @@ type Props = { export const Beside = (props: Props) => { const { t } = useLocalize() - const { subscriptions } = useFollowing() - const [subscriptionsAuthorsId, setSubscriptionsAuthorsId] = createSignal() - - createEffect(() => { - setSubscriptionsAuthorsId(subscriptions?.authors?.map((item) => item.id) || []) - }) + const { isOwnerSubscribed } = useFollowing() return ( 0}> @@ -94,8 +89,7 @@ export const Beside = (props: Props) => { diff --git a/src/components/Views/AllAuthors.tsx b/src/components/Views/AllAuthors.tsx index 3b1ec9d4..c50964ed 100644 --- a/src/components/Views/AllAuthors.tsx +++ b/src/components/Views/AllAuthors.tsx @@ -4,6 +4,7 @@ import { Meta } from '@solidjs/meta' import { clsx } from 'clsx' import { createEffect, createMemo, createSignal, For, Show } from 'solid-js' +import { useFollowing } from '../../context/following' import { useLocalize } from '../../context/localize' import { useRouter } from '../../stores/router' import { loadAuthors, setAuthorsSort, useAuthorsStore } from '../../stores/zine/authors' @@ -111,6 +112,8 @@ export const AllAuthorsView = (props: Props) => { ) }) + const { isOwnerSubscribed } = useFollowing() + const sortedKeys = createMemo(() => { const keys = Object.keys(byLetter()) keys.sort() @@ -229,7 +232,13 @@ export const AllAuthorsView = (props: Props) => { {(author) => (
- +
)} diff --git a/src/context/following.tsx b/src/context/following.tsx index 9b87b140..26cb61f7 100644 --- a/src/context/following.tsx +++ b/src/context/following.tsx @@ -20,6 +20,7 @@ interface FollowingContextType { loadSubscriptions: () => void follow: (what: FollowingEntity, slug: string) => Promise unfollow: (what: FollowingEntity, slug: string) => Promise + isOwnerSubscribed: (userId: number) => boolean } const FollowingContext = createContext() @@ -108,10 +109,16 @@ export const FollowingProvider = (props: { children: JSX.Element }) => { } } + const isOwnerSubscribed = (userId: number) => { + if (!author()) return + return !!subscriptions?.authors?.some((authorEntity) => authorEntity.id === userId) + } + const value: FollowingContextType = { loading, subscriptions, setSubscriptions, + isOwnerSubscribed, setFollowing, loadSubscriptions: fetchData, follow,