Fix subscribe logic (refactoring)

This commit is contained in:
ilya-bkv 2024-02-05 15:34:47 +03:00
parent 09b32b06cc
commit f2315411b2
4 changed files with 21 additions and 15 deletions

View File

@ -41,11 +41,7 @@ export const AuthorCard = (props: Props) => {
const [subscriptionFilter, setSubscriptionFilter] = createSignal<SubscriptionFilter>('all') const [subscriptionFilter, setSubscriptionFilter] = createSignal<SubscriptionFilter>('all')
const [isFollowed, setIsFollowed] = createSignal<boolean>() const [isFollowed, setIsFollowed] = createSignal<boolean>()
const isProfileOwner = createMemo(() => author()?.slug === props.author.slug) const isProfileOwner = createMemo(() => author()?.slug === props.author.slug)
const { subscriptions } = useFollowing() const { setFollowing, isOwnerSubscribed } = useFollowing()
const { setFollowing } = useFollowing()
const isOwnerSubscribed = (authorId: number) => {
return !!subscriptions?.authors.some((a) => a.id === authorId)
}
onMount(() => { onMount(() => {
setAuthorSubs(props.following) setAuthorSubs(props.following)

View File

@ -3,7 +3,7 @@
import type { Author, Shout, Topic } from '../../graphql/schema/core.gen' import type { Author, Shout, Topic } from '../../graphql/schema/core.gen'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import { createEffect, createSignal, For, Show } from 'solid-js' import { For, Show } from 'solid-js'
import { useFollowing } from '../../context/following' import { useFollowing } from '../../context/following'
import { useLocalize } from '../../context/localize' import { useLocalize } from '../../context/localize'
@ -30,12 +30,7 @@ type Props = {
export const Beside = (props: Props) => { export const Beside = (props: Props) => {
const { t } = useLocalize() const { t } = useLocalize()
const { subscriptions } = useFollowing() const { isOwnerSubscribed } = useFollowing()
const [subscriptionsAuthorsId, setSubscriptionsAuthorsId] = createSignal<number[] | undefined>()
createEffect(() => {
setSubscriptionsAuthorsId(subscriptions?.authors?.map((item) => item.id) || [])
})
return ( return (
<Show when={!!props.beside?.slug && props.values?.length > 0}> <Show when={!!props.beside?.slug && props.values?.length > 0}>
@ -94,8 +89,7 @@ export const Beside = (props: Props) => {
<AuthorBadge <AuthorBadge
author={value as Author} author={value as Author}
isFollowed={{ isFollowed={{
loaded: Boolean(subscriptionsAuthorsId()), value: isOwnerSubscribed(value.id),
value: subscriptionsAuthorsId().includes(value.id),
}} }}
/> />
</Show> </Show>

View File

@ -4,6 +4,7 @@ import { Meta } from '@solidjs/meta'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import { createEffect, createMemo, createSignal, For, Show } from 'solid-js' import { createEffect, createMemo, createSignal, For, Show } from 'solid-js'
import { useFollowing } from '../../context/following'
import { useLocalize } from '../../context/localize' import { useLocalize } from '../../context/localize'
import { useRouter } from '../../stores/router' import { useRouter } from '../../stores/router'
import { loadAuthors, setAuthorsSort, useAuthorsStore } from '../../stores/zine/authors' import { loadAuthors, setAuthorsSort, useAuthorsStore } from '../../stores/zine/authors'
@ -111,6 +112,8 @@ export const AllAuthorsView = (props: Props) => {
) )
}) })
const { isOwnerSubscribed } = useFollowing()
const sortedKeys = createMemo<string[]>(() => { const sortedKeys = createMemo<string[]>(() => {
const keys = Object.keys(byLetter()) const keys = Object.keys(byLetter())
keys.sort() keys.sort()
@ -229,7 +232,13 @@ export const AllAuthorsView = (props: Props) => {
{(author) => ( {(author) => (
<div class="row"> <div class="row">
<div class="col-lg-20 col-xl-18"> <div class="col-lg-20 col-xl-18">
<AuthorBadge author={author as Author} /> <AuthorBadge
author={author as Author}
isFollowed={{
loaded: Boolean(filteredAuthors()),
value: isOwnerSubscribed(author.id),
}}
/>
</div> </div>
</div> </div>
)} )}

View File

@ -20,6 +20,7 @@ interface FollowingContextType {
loadSubscriptions: () => void loadSubscriptions: () => void
follow: (what: FollowingEntity, slug: string) => Promise<void> follow: (what: FollowingEntity, slug: string) => Promise<void>
unfollow: (what: FollowingEntity, slug: string) => Promise<void> unfollow: (what: FollowingEntity, slug: string) => Promise<void>
isOwnerSubscribed: (userId: number) => boolean
} }
const FollowingContext = createContext<FollowingContextType>() const FollowingContext = createContext<FollowingContextType>()
@ -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 = { const value: FollowingContextType = {
loading, loading,
subscriptions, subscriptions,
setSubscriptions, setSubscriptions,
isOwnerSubscribed,
setFollowing, setFollowing,
loadSubscriptions: fetchData, loadSubscriptions: fetchData,
follow, follow,