Skip to content

Commit

Permalink
If no defaultLocale found for user, use the locale from the session o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
vasharma05 committed Aug 28, 2023
1 parent da40164 commit b5ed3b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import { useTranslation } from "react-i18next";
export interface ChangeLocaleProps {
allowedLocales: Array<string>;
user: LoggedInUser;
locale: string;
postUserProperties: PostUserProperties;
postSessionLocale: PostSessionLocale;
}

const ChangeLocaleWrapper: React.FC<
Pick<ChangeLocaleProps, "allowedLocales" | "user">
Pick<ChangeLocaleProps, "allowedLocales" | "user" | "locale">
> = (props) => {
const isOnline = useConnectivity();
const [postUserProperties, postSessionLocale] = useMemo(
Expand All @@ -43,30 +44,40 @@ const ChangeLocaleWrapper: React.FC<
// exported for tests
export const ChangeLocale: React.FC<ChangeLocaleProps> = ({
allowedLocales,
locale,
user,
postUserProperties,
postSessionLocale,
}) => {
const { t } = useTranslation();
const [userProps, setUserProps] = useState(user.userProperties);
const [selectedLocale, setSelectedLocale] = useState(
user?.userProperties?.defaultLocale ?? locale
);
const options = allowedLocales?.map((locale) => (
<SelectItem text={locale} value={locale} key={locale} />
));

useEffect(() => {
if (user.userProperties.defaultLocale !== userProps.defaultLocale) {
if (user.userProperties.defaultLocale !== selectedLocale) {
const ac = new AbortController();
postUserProperties(user.uuid, userProps, ac);
postSessionLocale(userProps.defaultLocale, ac);
postUserProperties(
user.uuid,
{
...(user.userProperties ?? {}),
defaultLocale: selectedLocale,
},
ac
);
postSessionLocale(selectedLocale, ac);
return () => ac.abort();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [userProps, postUserProperties, postSessionLocale]);
}, [selectedLocale, postUserProperties, postSessionLocale]);

const onChange = useCallback(
(event: React.ChangeEvent<HTMLSelectElement>) =>
setUserProps({ ...userProps, defaultLocale: event.target.value }),
[userProps, setUserProps]
setSelectedLocale(event.target.value),
[setSelectedLocale]
);

const onClick = useCallback(
Expand All @@ -83,7 +94,7 @@ export const ChangeLocale: React.FC<ChangeLocaleProps> = ({
labelText={t("selectLocale", "Select locale")}
onChange={onChange}
onClick={onClick}
value={userProps.defaultLocale}
value={selectedLocale}
>
{options}
</Select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe(`<ChangeLocale />`, () => {

render(
<ChangeLocale
locale={"fr"}
allowedLocales={allowedLocales}
user={user}
postUserProperties={postUserPropertiesMock}
Expand Down

0 comments on commit b5ed3b4

Please sign in to comment.