Skip to content

Commit

Permalink
Refetch queries when the token changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesan committed Nov 23, 2023
1 parent d1b290a commit ad4409d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const SettingsNotificationsTab: FC = () => {
activeMemberExistBackendLoading,
activeMemberExistBackendRefetch,
backendClient,
activeMemberSettings,
authToken,
setMemberSettings,
} = useNotificationSettings()
const { showModal } = useModal()
Expand Down Expand Up @@ -79,13 +79,12 @@ export const SettingsNotificationsTab: FC = () => {
receiveEmailNotifications: data.me?.receiveEmails ?? true,
})
},
skip: !activeMemberSettings?.accessToken,
skip: !authToken,
})

const isRegistered = activeMemberExistBackendData?.memberExist ?? false

const isUnauthorized =
(!!activeMember && !activeMemberSettings?.accessToken) || meError?.message.includes('Unauthorized')
const isUnauthorized = (!!activeMember && !authToken) || meError?.message.includes('Unauthorized')

const [sendUpdateMemberMutation, { error: mutationError }] = useUpdateBackendMemberMutation({
client: backendClient,
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/app/providers/backend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type BackendContextValue = {
backendClient?: ApolloClient<any>
notificationsSettingsMap?: MemberNotificationsRecord
setMemberSettings: (memberId: string, settings: Partial<MemberNotificationSettingsData>) => void
authToken?: string
}

export const BackendContext = createContext<BackendContextValue>({ setMemberSettings: () => null })
10 changes: 9 additions & 1 deletion packages/ui/src/app/providers/backend/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, makeVar } from '@apollo/client'
import { ApolloClient, ApolloLink, HttpLink, InMemoryCache, makeVar, useReactiveVar } from '@apollo/client'
import React, { ReactNode, useCallback, useEffect, useState } from 'react'

import { useLocalStorage } from '@/common/hooks/useLocalStorage'
Expand Down Expand Up @@ -48,6 +48,13 @@ export const BackendProvider = (props: { children: ReactNode }) => {
backendAuthTokenVar(activeMemberSettings.accessToken)
}, [backendClient, activeMemberSettings?.accessToken])

const authToken = useReactiveVar(backendAuthTokenVar)

// Refetch backend queries after Apollo client auth token changes (warning: the changes are async).
useEffect(() => {
backendClient?.refetchQueries({ include: 'active' })
}, [authToken])

const setMemberSettings = useCallback(
(memberId: string, settings: Partial<MemberNotificationSettingsData>) => {
setNotificationsSettingsMap((prev) => ({
Expand All @@ -71,6 +78,7 @@ export const BackendProvider = (props: { children: ReactNode }) => {
backendClient,
notificationsSettingsMap,
setMemberSettings,
authToken: authToken ?? undefined,
}}
{...props}
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/memberships/hooks/useNotificationSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useNotificationSettings = () => {
const { active: activeMember } = useMyMemberships()
const backendContext = useContext(BackendContext)
if (!backendContext) throw new Error('Missing backend context')
const { backendClient, notificationsSettingsMap, setMemberSettings } = backendContext
const { backendClient, notificationsSettingsMap, setMemberSettings, authToken } = backendContext
const memberExistsQueryEnabled = !!backendClient && !!activeMember?.id
const { data, error, loading, refetch } = useGetBackendMemberExistsQuery({
client: backendClient,
Expand All @@ -28,5 +28,6 @@ export const useNotificationSettings = () => {
activeMemberExistBackendRefetch: refetch,
setMemberSettings,
backendClient,
authToken,
}
}

0 comments on commit ad4409d

Please sign in to comment.