Skip to content

Commit

Permalink
Add check before refetching and remove saving to local storage in pre…
Browse files Browse the repository at this point in the history
…ferences
  • Loading branch information
caleballdrin committed Oct 21, 2024
1 parent 9ce5b2b commit b5c1e7d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/components/Constants/UseApiConstants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export const useApiConstants = ():
});

const userSavedLanguage = data?.user.preferences?.locale;
const [localeStorageLanguage, setLocaleStorageLanguage] = useLocalStorage(
`user-language`,
const [localStorageLanguage, setLocalStorageLanguage] = useLocalStorage(
`constants-language`,
'',
);
useEffect(() => {
// if the language in locale storage is different than the saved language, the user may have changed languages in a different browser. If so, refetch the constants.
if (localeStorageLanguage !== userSavedLanguage) {
setLocaleStorageLanguage(userSavedLanguage || '');
// if the language in local storage is different than the saved language, that may mean the cached constants are in the previous language. If so, refetch the constants.

if (userSavedLanguage && localStorageLanguage !== userSavedLanguage) {
setLocalStorageLanguage(userSavedLanguage || '');
refetch();
}
}, [userSavedLanguage]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { FieldWrapper } from 'src/components/Shared/Forms/FieldWrapper';
import { FormWrapper } from 'src/components/Shared/Forms/FormWrapper';
import { Preference } from 'src/graphql/types.generated';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { useLocalStorage } from 'src/hooks/useLocalStorage';
import { formatLanguage, languages } from 'src/lib/data/languages';
import { useUpdatePersonalPreferencesMutation } from '../UpdatePersonalPreferences.generated';

Expand All @@ -34,7 +33,6 @@ export const LanguageAccordion: React.FC<LanguageAccordionProps> = ({
const { appName } = useGetAppSettings();
const { enqueueSnackbar } = useSnackbar();
const [updatePersonalPreferences] = useUpdatePersonalPreferencesMutation();
const [_, setLocaleStorageLanguage] = useLocalStorage(`user-language`, '');

const label = t('Language');

Expand All @@ -57,7 +55,6 @@ export const LanguageAccordion: React.FC<LanguageAccordionProps> = ({
enqueueSnackbar(t('Saved successfully.'), {
variant: 'success',
});
setLocaleStorageLanguage(attributes.locale || '');
},
onError: () => {
enqueueSnackbar(t('Saving failed.'), {
Expand Down

0 comments on commit b5c1e7d

Please sign in to comment.