Skip to content

Commit

Permalink
fix(condo): DOMA-11031 add new logic for removing unnecessary requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tolmachev21 committed Jan 31, 2025
1 parent 58275fa commit 8f301d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ export const GlobalAppsContainer: React.FC = () => {
}, [organizationId])

useEffect(() => {
if (!isGlobalAppsFetched.current && !loading && !isNull(user)) {
if (!isGlobalAppsFetched.current && !loading && !isNull(user) && !isLoading) {
refetch()
isGlobalAppsFetched.current = true
}
}, [user, loading])
}, [user, loading, isLoading])

// Global miniapps allowed only for authenticated employees
if (!user || !organizationId) {
Expand Down
28 changes: 22 additions & 6 deletions packages/next/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,17 @@ const _withAuthLegacy: WithAuthLegacyType = ({ ssr = false, ...opts } = {}) => P
const AuthProvider: React.FC = ({ children }) => {
const apolloClient = useApolloClient()

const { data, loading: userLoading, refetch } = useQuery(USER_QUERY, {
notifyOnNetworkStatusChange: true,
})
const [isAuthLoading, setAuthLoading] = useState<boolean>(false)

const { data, loading: userLoading, refetch } = useQuery(USER_QUERY)

const user = useMemo(() => {
if (!userLoading) {
setAuthLoading(false)
return get(data, 'authenticatedUser') || null
}
}, [data, userLoading])

const user = useMemo(() => get(data, 'authenticatedUser') || null, [data])

const refetchAuth = useCallback(async () => {
await refetch()
Expand All @@ -282,15 +288,17 @@ const AuthProvider: React.FC = ({ children }) => {
if (item) {
await apolloClient.clearStore()
}
setAuthLoading(false)
},
onError: (error) => {
console.error(error)
setAuthLoading(false)
},
})

const [signOutMutation, { loading: signOutLoading }] = useMutation(SIGNOUT_MUTATION, {
refetchQueries: [USER_QUERY],
onCompleted: async () => {
await refetch()
removeCookieEmployeeId()
await apolloClient.cache.reset()
apolloClient.cache.writeQuery({
Expand All @@ -299,16 +307,24 @@ const AuthProvider: React.FC = ({ children }) => {
authenticatedUser: null,
},
})
setAuthLoading(false)
},
onError: (error) => {
console.error(error)
setAuthLoading(false)
},
})

useEffect(() => {
if (userLoading || signOutLoading || signInLoading) {
setAuthLoading(true)
}
}, [userLoading, signOutLoading, signInLoading])

return (
<AuthContext.Provider
value={{
isLoading: userLoading || signOutLoading || signInLoading,
isLoading: userLoading || signOutLoading || signInLoading || isAuthLoading,
isAuthenticated: !!user,
user,
refetch: refetchAuth,
Expand Down

0 comments on commit 8f301d5

Please sign in to comment.