Skip to content

Commit

Permalink
Fix blank page on initial load
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Dec 6, 2023
1 parent 6815918 commit 9e346fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/components/User/Preferences/UserPreferenceProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { createContext, useContext, useEffect, useState } from 'react';
import { useSession } from 'next-auth/react';
import i18next from 'src/lib/i18n';
import { useGetUserQuery } from '../GetUser.generated';

Expand All @@ -19,20 +18,19 @@ interface Props {
children?: React.ReactNode;
}
export const UserPreferenceProvider: React.FC<Props> = ({ children }) => {
const { data: session } = useSession();
const { data: user, loading } = useGetUserQuery({ skip: !session });
const { data } = useGetUserQuery();

Check warning on line 21 in src/components/User/Preferences/UserPreferenceProvider.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/User/Preferences/UserPreferenceProvider.tsx#L21

Added line #L21 was not covered by tests
const [locale, setLocale] = useState('en-US');

useEffect(() => {
if (user) {
i18next.changeLanguage(user.user.preferences?.language ?? 'en');
setLocale(user.user.preferences?.locale ?? 'en-US');
if (data) {
i18next.changeLanguage(data.user.preferences?.language ?? 'en');
setLocale(data.user.preferences?.locale ?? 'en-US');
}
}, [user]);
}, [data]);

return (
<UserPreferenceContext.Provider value={{ locale }}>
{!loading && children}
{children}
</UserPreferenceContext.Provider>
);
};
7 changes: 4 additions & 3 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ const httpLink = new BatchHttpLink({
fetch,
});

const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
const clientErrorLink = onError(({ graphQLErrors, networkError }) => {
// Don't show sign out and display errors on the login page because the user won't be logged in
if (graphQLErrors && window.location.pathname !== '/login') {
graphQLErrors.map(({ message, extensions }) => {
if (extensions?.code === 'AUTHENTICATION_ERROR') {
signOut({ redirect: true, callbackUrl: 'signOut' }).then(() => {
Expand Down Expand Up @@ -101,7 +102,7 @@ if (process.browser && process.env.NODE_ENV === 'production') {
}

const client = new ApolloClient({
link: errorLink.concat(httpLink),
link: clientErrorLink.concat(httpLink),
cache,
assumeImmutableResults: true,
defaultOptions: {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ i18next
useSuspense: false,
},
detection: {
order: ['navigator', 'htmlTag'],
order: ['localStorage', 'navigator', 'htmlTag'],
},
backend: {
loadPath: '../../locales/{{lng}}/translation.json',
loadPath: '/locales/{{lng}}/translation.json',
},
});

Expand Down

0 comments on commit 9e346fb

Please sign in to comment.