Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPDX-8351 Add UseLocalizedConstants() hook #1145

Merged
merged 14 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/components/Constants/LoadConstants.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ query LoadConstants {
# }
pledgeCurrency {
id
key
codeSymbolString
name
code
value
symbol
}
pledgeFrequency {
Expand Down Expand Up @@ -83,4 +85,11 @@ query LoadConstants {
tasks
}
}
user {
id
preferences {
id
locale
}
}
}
1 change: 1 addition & 0 deletions src/components/Constants/LoadConstantsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const LoadConstantsMock = (): MockedResponse => {
};

export const loadConstantsMockData: LoadConstantsQuery = {
user: { id: '123', preferences: { id: '123', locale: 'en' } },
constant: {
status: [
{
Expand Down
17 changes: 16 additions & 1 deletion src/components/Constants/UseApiConstants.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useEffect } from 'react';
import { useLocalStorage } from 'src/hooks/useLocalStorage';
import {
LoadConstantsQuery,
useLoadConstantsQuery,
Expand All @@ -7,9 +9,22 @@ import {
export const useApiConstants = ():
| LoadConstantsQuery['constant']
| undefined => {
const { data } = useLoadConstantsQuery({
const { data, refetch } = useLoadConstantsQuery({
fetchPolicy: 'cache-first',
});

const userSavedLanguage = data?.user.preferences?.locale;
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
const [localeStorageLanguage, setLocaleStorageLanguage] = useLocalStorage(
`user-language`,
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
'',
);
useEffect(() => {
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
// 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.
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
if (localeStorageLanguage !== userSavedLanguage) {
setLocaleStorageLanguage(userSavedLanguage || '');
refetch();
}
}, [userSavedLanguage]);

return data?.constant;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -33,6 +34,7 @@ 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 @@ -55,6 +57,7 @@ export const LanguageAccordion: React.FC<LanguageAccordionProps> = ({
enqueueSnackbar(t('Saved successfully.'), {
variant: 'success',
});
setLocaleStorageLanguage(attributes.locale || '');
caleballdrin marked this conversation as resolved.
Show resolved Hide resolved
},
onError: () => {
enqueueSnackbar(t('Saving failed.'), {
Expand Down
Loading