Skip to content

Commit

Permalink
📁 Fix account follow logic (Joystream#6015)
Browse files Browse the repository at this point in the history
* Get follow data from account

* Adjust the logic
  • Loading branch information
WRadoslaw committed Mar 17, 2024
1 parent 27d0fc8 commit 4f511d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
10 changes: 10 additions & 0 deletions packages/atlas/src/api/hooks/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
useUnfollowChannelMutation,
} from '@/api/queries/__generated__/channels.generated'
import { useSegmentAnalytics } from '@/hooks/useSegmentAnalytics'
import { useAuth } from '@/providers/auth/auth.hooks'

export const useBasicChannel = (
id: string,
Expand Down Expand Up @@ -143,6 +144,7 @@ export const useMostPaidChannels = (): { channels: PayeeChannel[] | undefined; l

export const useFollowChannel = (opts?: MutationHookOptions<FollowChannelMutation>) => {
const [followChannel, rest] = useFollowChannelMutation()
const { refetchCurrentUser } = useAuth()
const { trackChannelFollow } = useSegmentAnalytics()
return {
followChannel: (id: string) => {
Expand All @@ -152,6 +154,9 @@ export const useFollowChannel = (opts?: MutationHookOptions<FollowChannelMutatio
variables: {
channelId: id,
},
onCompleted: () => {
refetchCurrentUser().catch(() => undefined)
},
update: (cache, mutationResult) => {
cache.modify({
id: cache.identify({
Expand All @@ -171,13 +176,18 @@ export const useFollowChannel = (opts?: MutationHookOptions<FollowChannelMutatio

export const useUnfollowChannel = (opts?: MutationHookOptions<UnfollowChannelMutation>) => {
const [unfollowChannel, rest] = useUnfollowChannelMutation()
const { refetchCurrentUser } = useAuth()

return {
unfollowChannel: (id: string) =>
unfollowChannel({
...opts,
variables: {
channelId: id,
},
onCompleted: () => {
refetchCurrentUser().catch(() => undefined)
},
update: (cache, mutationResult) => {
cache.modify({
id: cache.identify({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/atlas/src/api/queries/accounts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ query GetCurrentAccount {
isEmailConfirmed
joystreamAccount
membershipId
followedChannels {
channelId
timestamp
}
}
}

Expand Down
22 changes: 4 additions & 18 deletions packages/atlas/src/hooks/useHandleFollowChannel.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import { useCallback } from 'react'

import { useFollowChannel, useUnfollowChannel } from '@/api/hooks/channel'
import { useGetChannelFollowsQuery } from '@/api/queries/__generated__/accounts.generated'
import { useAuth } from '@/providers/auth/auth.hooks'
import { useConfirmationModal } from '@/providers/confirmationModal'
import { useUser } from '@/providers/user/user.hooks'
import { SentryLogger } from '@/utils/logs'

export const useHandleFollowChannel = (channelId?: string, channelTitle?: string | null) => {
const [openUnfollowDialog, closeUnfollowDialog] = useConfirmationModal()
const { followChannel } = useFollowChannel()
const { accountId } = useUser()
const { currentUser } = useAuth()
const { unfollowChannel } = useUnfollowChannel()
const { data } = useGetChannelFollowsQuery({
variables: {
where: {
channelId_eq: channelId,
user: {
account: {
id_eq: accountId,
},
},
},
limit: 1,
},
skip: !accountId || !channelTitle || !channelId,
})
const follow = !!data?.channelFollows.length

const follow = currentUser?.followedChannels.some((followage) => followage.channelId === channelId)

const toggleFollowing = useCallback(async () => {
if (!channelId || !channelTitle) {
Expand Down
19 changes: 17 additions & 2 deletions packages/atlas/src/providers/auth/auth.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,34 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {

const isWalletUser = useMemo(() => encodedSeed === null && !!currentUser, [currentUser, encodedSeed])

const refetchCurrentUser = useCallback(async () => {
const res = await refetch()
setCurrentUser(res.data.accountData)
return res
}, [refetch])

const contextValue: AuthContextValue = useMemo(
() => ({
handleLogin,
isAuthenticating,
loggedAddress,
refetchCurrentUser: refetch,
refetchCurrentUser,
currentUser,
isWalletUser,
handleLogout,
encodedSeed,
isLoggedIn: isAuthenticating ? undefined : !!currentUser,
}),
[currentUser, encodedSeed, handleLogin, handleLogout, isAuthenticating, isWalletUser, loggedAddress, refetch]
[
currentUser,
encodedSeed,
handleLogin,
handleLogout,
isAuthenticating,
isWalletUser,
loggedAddress,
refetchCurrentUser,
]
)

return <AuthContext.Provider value={contextValue}>{children}</AuthContext.Provider>
Expand Down

0 comments on commit 4f511d2

Please sign in to comment.