Skip to content

Commit

Permalink
feat: change locale in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nyet-ty committed Apr 15, 2024
1 parent e7ab38f commit 1575984
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Settings" ADD COLUMN "locale" TEXT NOT NULL DEFAULT 'en';
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ model Settings {
flow Flow? @relation(fields: [flowId], references: [id])
flowId String?
activity Activity?
locale String @default("en")
createdAt DateTime @default(dbgenerated("timezone('utc'::text, now())")) @db.Timestamp()
updatedAt DateTime @default(dbgenerated("timezone('utc'::text, now())")) @updatedAt
Expand Down
8 changes: 8 additions & 0 deletions src/components/Page/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ export const Page: React.FC<PageProps> = ({ user, ssrTime, title = 'Untitled', c
setPreview(null);
}, [router.asPath, setPreview]);

useEffect(() => {
const { asPath, locale, push } = router;

if (userSettings?.locale && locale !== userSettings.locale) {
push(asPath, asPath, { locale: userSettings.locale });
}
}, [router, userSettings]);

return (
<pageContext.Provider value={{ user, theme, themeId: mapThemeOnId[theme], ssrTime }}>
<Head>
Expand Down
3 changes: 2 additions & 1 deletion src/components/PageFooter/PageFooter.i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"Docs": "Docs",
"Contact Taskany": "Contact Taskany",
"API": "API",
"About": "About"
"About": "About",
"Locale change title": "Ru"
}
3 changes: 2 additions & 1 deletion src/components/PageFooter/PageFooter.i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"Docs": "Документация",
"Contact Taskany": "Контакты",
"API": "API",
"About": "О нас"
"About": "О нас",
"Locale change title": "En"
}
32 changes: 31 additions & 1 deletion src/components/PageFooter/PageFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { FC } from 'react';
import { FC, useCallback } from 'react';
import { Footer, Link, FooterItem } from '@taskany/bricks';
import { useRouter } from 'next/router';

import { ModalEvent, dispatchModalEvent } from '../../utils/dispatchModal';
import { defaultLocale, languages } from '../../utils/getLang';
import { trpc } from '../../utils/trpcClient';
import { notifyPromise } from '../../utils/notifyPromise';

import s from './PageFooter.module.css';
import { tr } from './PageFooter.i18n';
Expand All @@ -14,6 +18,29 @@ export const PageFooter: FC = () => {
{ title: tr('API'), url: '/api' },
{ title: tr('About'), url: '/about' },
];

const router = useRouter();
const { locale } = router;

const updateSettingsMutation = trpc.user.updateSettings.useMutation();
const utils = trpc.useContext();
const onLocaleChange = useCallback(async () => {
const newLocale = locale === defaultLocale ? languages[1] : defaultLocale;

const promise = updateSettingsMutation.mutateAsync(
{
locale: newLocale,
},
{
onSuccess: () => {
utils.user.settings.invalidate();
},
},
);

await notifyPromise(promise, 'userSettingsUpdate');
}, [updateSettingsMutation, utils.user.settings, locale]);

return (
<Footer>
<FooterItem onClick={dispatchModalEvent(ModalEvent.FeedbackCreateModal)} className={s.FooterItem}>
Expand All @@ -25,6 +52,9 @@ export const PageFooter: FC = () => {
<FooterItem>{title}</FooterItem>
</Link>
))}
<Link inline onClick={onLocaleChange}>
<FooterItem>{tr('Locale change title')}</FooterItem>
</Link>
</Footer>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"title": "Taskany — {user} — Settings",
"You are hero": "",
"Beta features": "",
"Nickname already exists": "Nickname already exists"
"Nickname already exists": "Nickname already exists",
"Locale": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"title": "Taskany — {user} — Настройки",
"You are hero": "Ты наш герой",
"Beta features": "Участвовать в бета-тестировании",
"Nickname already exists": "Логин уже существует"
"Nickname already exists": "Логин уже существует",
"Locale": "Язык"
}
37 changes: 37 additions & 0 deletions src/components/UserSettingsPage/UserSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { UpdateUser, updateUserSchema } from '../../schema/user';
import { notifyPromise } from '../../utils/notifyPromise';
import { dispatchErrorNotification, dispatchSuccessNotification } from '../../utils/dispatchNotification';
import { userSettings, userSettingsLogoutButton } from '../../utils/domObjects';
import { languages } from '../../utils/getLang';

import s from './UserSettingsPage.module.css';
import { tr } from './UserSettingsPage.i18n';
Expand Down Expand Up @@ -119,6 +120,26 @@ export const UserSettingsPage = ({ user, ssrTime }: ExternalPageProps) => {
[updateSettingsMutation, utils.user.settings],
);

const onLocaleChange = useCallback(
async (locale?: string) => {
if (!locale) return;

const promise = updateSettingsMutation.mutateAsync(
{
locale,
},
{
onSuccess: () => {
utils.user.settings.invalidate();
},
},
);

await notifyPromise(promise, 'userSettingsUpdate');
},
[updateSettingsMutation, utils.user.settings],
);

const [betaUser, setBetaUser] = useState(settings?.data?.beta);

const onBetaUserChange = useCallback<ChangeEventHandler<HTMLInputElement>>(
Expand Down Expand Up @@ -246,6 +267,22 @@ export const UserSettingsPage = ({ user, ssrTime }: ExternalPageProps) => {
</Fieldset>
</Form>
</SettingsCard>
<SettingsCard>
<Form>
<Fieldset title={tr('Locale')}>
<FormRadio
label="Locale"
name="locale"
value={settings.data?.locale}
onChange={onLocaleChange}
>
{languages.map((language) => (
<FormRadioInput value={language} label={language} key={language} />
))}
</FormRadio>
</Fieldset>
</Form>
</SettingsCard>

<SettingsCard>
<Form>
Expand Down
1 change: 1 addition & 0 deletions src/schema/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const suggestionsUserSchema = z.object({
export const settingsUserSchema = z.object({
theme: z.string().optional(),
beta: z.boolean().optional(),
locale: z.string().optional(),
});
1 change: 1 addition & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ declare module 'next-auth' {
settings?: {
theme: 'light' | 'dark' | 'system';
beta: boolean;
locale: string;
};
};
}
Expand Down

0 comments on commit 1575984

Please sign in to comment.