Skip to content

Commit

Permalink
(feat) O3-2817: Fix some issues in the change language modal (#917)
Browse files Browse the repository at this point in the history
* (feat) O3-2817: Fix issues in the change language modal

* Fixup annotations
  • Loading branch information
denniskigen authored Feb 6, 2024
1 parent 97feb47 commit ca0c93a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ export function ChangeLanguageLink() {
const { t } = useTranslation();
const session = useSession();

const launchChangeModal = useCallback(() => {
showModal('change-language-modal');
}, []);
const launchChangeLanguageModal = useCallback(() => showModal('change-language-modal'), []);

const languageNames = new Intl.DisplayNames([session?.locale], { type: 'language' });

return (
<SwitcherItem className={styles.panelItemContainer} aria-label="Change language">
<SwitcherItem className={styles.panelItemContainer} aria-label={t('changeLanguage', 'Change language')}>
<div>
<Language size={20} />
<p>{languageNames.of(session?.locale)}</p>
</div>
<Button kind="ghost" onClick={launchChangeModal}>
<Button kind="ghost" onClick={launchChangeLanguageModal}>
{t('change', 'Change')}
</Button>
</SwitcherItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
.languageOptionsContainer {
display: flex;
flex-direction: column;
margin-left: 2em;
max-height: 28rem;
}

Expand All @@ -14,3 +13,13 @@
align-items: center;
@extend .bodyShort01;
}

.submitButton {
:global(.cds--inline-loading) {
min-height: 1rem !important;
}

:global(.cds--inline-loading__text) {
font-size: unset !important;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import React, { useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, ModalBody, ModalFooter, ModalHeader, RadioButton, RadioButtonGroup } from '@carbon/react';
import {
Button,
InlineLoading,
ModalBody,
ModalFooter,
ModalHeader,
RadioButton,
RadioButtonGroup,
} from '@carbon/react';
import { useConnectivity, useSession } from '@openmrs/esm-framework';
import { postUserPropertiesOffline, postUserPropertiesOnline } from './change-language.resource';
import styles from './change-language-modal.scss';
Expand All @@ -11,53 +19,58 @@ interface ChangeLanguageModalProps {

export default function ChangeLanguageModal({ close }: ChangeLanguageModalProps) {
const { t } = useTranslation();
const isOnline = useConnectivity();
const session = useSession();
const user = session?.user;
const allowedLocales = session?.allowedLocales ?? [];
const [selectedLocale, setSelectedLocale] = useState(session?.locale);
const isOnline = useConnectivity();
const [isChangingLanguage, setIsChangingLanguage] = useState(false);

const handleSubmit = useCallback(() => {
setIsChangingLanguage(true);

const onSubmit = useCallback(() => {
const postUserProperties = isOnline ? postUserPropertiesOnline : postUserPropertiesOffline;
if (selectedLocale !== session?.locale) {

if (selectedLocale && selectedLocale !== session?.locale) {
const ac = new AbortController();
postUserProperties(
user.uuid,
{
...(user.userProperties ?? {}),
defaultLocale: selectedLocale,
defaultLocale: selectedLocale.replace(/-/gi, '_'),
},
ac,
);
}
}, [isOnline, user.userProperties, user.uuid, selectedLocale]);

const languageNames = useMemo(
() => Object.fromEntries(allowedLocales.map((l) => [l, new Intl.DisplayNames([l], { type: 'language' }).of(l)])),
() =>
Object.fromEntries(
allowedLocales.map((locale) => [locale, new Intl.DisplayNames([locale], { type: 'language' }).of(locale)]),
),
[allowedLocales],
);

return (
<>
<ModalHeader closeModal={close} title={t('changeLanguage', 'Change Language')} />
<ModalHeader closeModal={close} title={t('changeLanguage', 'Change language')} />
<ModalBody>
<div className={styles.languageOptionsContainer}>
<RadioButtonGroup
valueSelected={selectedLocale}
orientation="vertical"
name="Login locations"
onChange={(l) => {
setSelectedLocale(l.toString());
}}
name="Language options"
onChange={(locale) => setSelectedLocale(locale.toString())}
>
{allowedLocales.map((l, i) => (
{allowedLocales.map((locale, i) => (
<RadioButton
className={styles.languageRadioButton}
key={`locale-option-${l}`}
id={`locale-option-${l}`}
name={l}
labelText={languageNames[l]}
value={l}
key={`locale-option-${locale}-${i}`}
id={`locale-option-${locale}-${i}`}
name={locale}
labelText={languageNames[locale]}
value={locale}
/>
))}
</RadioButtonGroup>
Expand All @@ -67,8 +80,17 @@ export default function ChangeLanguageModal({ close }: ChangeLanguageModalProps)
<Button kind="secondary" onClick={close}>
{t('cancel', 'Cancel')}
</Button>
<Button type="submit" onClick={onSubmit}>
{t('apply', 'Apply')}
<Button
className={styles.submitButton}
disabled={isChangingLanguage || selectedLocale === session?.locale}
type="submit"
onClick={handleSubmit}
>
{isChangingLanguage ? (
<InlineLoading description={t('changingLanguage', 'Changing language') + '...'} />
) : (
<span>{t('change', 'Change')}</span>
)}
</Button>
</ModalFooter>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type PostUserProperties = (

export async function postUserPropertiesOnline(
userUuid: string,
userProperties: any,
userProperties: Record<string, string>,
abortController: AbortController,
): Promise<void> {
await openmrsFetch(`/ws/rest/v1/user/${userUuid}`, {
Expand All @@ -25,7 +25,7 @@ export async function postUserPropertiesOnline(

export async function postUserPropertiesOffline(
userUuid: string,
userProperties: Record<string, unknown>,
userProperties: Record<string, string>,
): Promise<void> {
await queueSynchronizationItemFor(userUuid, userPropertyChange, userProperties, {
displayName: 'User Language Change',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ChangeLanguageModal from './change-language.modal';
import { render, screen } from '@testing-library/react';
import { useSession } from '@openmrs/esm-framework';
import ChangeLanguageModal from './change-language.modal';

const mockUser: any = {
const mockUser = {
uuid: 'uuid',
userProperties: {
defaultLocale: 'fr',
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(`Change Language Modal`, () => {
render(<ChangeLanguageModal close={jest.fn()} />);
expect(screen.getByRole('radio', { name: /français/ })).toBeChecked();
await user.click(screen.getByRole('radio', { name: /english/i }));
await user.click(screen.getByRole('button', { name: /apply/i }));
await user.click(screen.getByRole('button', { name: /change/i }));
expect(mockPostUserPropertiesOnline).toHaveBeenCalledWith(
mockUser.uuid,
{ defaultLocale: 'en' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apply": "Apply",
"cancel": "Cancel",
"change": "Change",
"changeLanguage": "Change Language",
"changeLanguage": "Change language",
"notifications": "Notifications",
"userMenuTooltip": "User"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apply": "Apply",
"cancel": "Cancel",
"change": "تغيير",
"changeLanguage": "Change Language",
"changeLanguage": "Change language",
"notifications": "الإشعارات",
"userMenuTooltip": "User"
}
4 changes: 2 additions & 2 deletions packages/apps/esm-primary-navigation-app/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"apply": "Apply",
"cancel": "Cancel",
"change": "Change",
"changeLanguage": "Change Language",
"changeLanguage": "Change language",
"changingLanguage": "Changing language",
"notifications": "Notifications",
"userMenuTooltip": "User"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apply": "אישור",
"cancel": "Cancel",
"change": "שינוי",
"changeLanguage": "Change Language",
"changeLanguage": "Change language",
"notifications": "התראות",
"userMenuTooltip": "User"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apply": "បញ្ជាក់",
"cancel": "Cancel",
"change": "ផ្លាស់ប្តូរ",
"changeLanguage": "Change Language",
"changeLanguage": "Change language",
"notifications": "ការជូនដំណឹង",
"userMenuTooltip": "User"
}

0 comments on commit ca0c93a

Please sign in to comment.