From 3a235fd85b6d8f601708eb57c9bfe1649249349a Mon Sep 17 00:00:00 2001 From: "haitao(lj)" Date: Fri, 4 Nov 2022 12:03:42 +0800 Subject: [PATCH] feat(interface-language): done admin language setting --- ui/src/pages/Admin/Interface/index.tsx | 29 ++++++++------------------ ui/src/services/admin/settings.ts | 5 +++++ ui/src/stores/interface.ts | 3 ++- ui/src/stores/userInfo.ts | 10 ++++++--- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/ui/src/pages/Admin/Interface/index.tsx b/ui/src/pages/Admin/Interface/index.tsx index 8a477bdb..d42b6058 100644 --- a/ui/src/pages/Admin/Interface/index.tsx +++ b/ui/src/pages/Admin/Interface/index.tsx @@ -12,18 +12,19 @@ import { interfaceStore } from '@/stores'; import { UploadImg } from '@/components'; import { TIMEZONES, DEFAULT_TIMEZONE } from '@/common/constants'; import { - getLanguageOptions, + getAdminLanguageOptions, uploadAvatar, updateInterfaceSetting, useInterfaceSetting, useThemeOptions, } from '@/services'; +import { setupAppLanguage } from '@/utils/localize'; const Interface: FC = () => { const { t } = useTranslation('translation', { keyPrefix: 'admin.interface', }); - const { update: interfaceStoreUpdate } = interfaceStore(); + const storeInterface = interfaceStore.getState().interface; const { data: themes } = useThemeOptions(); const Toast = useToast(); const [langs, setLangs] = useState(); @@ -31,17 +32,17 @@ const Interface: FC = () => { const [formData, setFormData] = useState({ logo: { - value: setting?.logo || '', + value: setting?.logo || storeInterface.logo, isInvalid: false, errorMsg: '', }, theme: { - value: setting?.theme || '', + value: setting?.theme || storeInterface.theme, isInvalid: false, errorMsg: '', }, language: { - value: setting?.language || '', + value: setting?.language || storeInterface.language, isInvalid: false, errorMsg: '', }, @@ -52,19 +53,8 @@ const Interface: FC = () => { }, }); const getLangs = async () => { - const res: LangsType[] = await getLanguageOptions(); + const res: LangsType[] = await getAdminLanguageOptions(); setLangs(res); - if (!formData.language.value) { - // set default theme value - setFormData({ - ...formData, - language: { - value: res[0].value, - isInvalid: false, - errorMsg: '', - }, - }); - } }; // set default theme value if (!formData.theme.value && Array.isArray(themes) && themes.length) { @@ -122,7 +112,8 @@ const Interface: FC = () => { msg: t('update', { keyPrefix: 'toast' }), variant: 'success', }); - interfaceStoreUpdate(reqParams); + interfaceStore.getState().update(reqParams); + setupAppLanguage(); }) .catch((err) => { if (err.isError && err.key) { @@ -172,8 +163,6 @@ const Interface: FC = () => { useEffect(() => { getLangs(); }, []); - - console.log('formData', formData); return ( <>

{t('page_title')}

diff --git a/ui/src/services/admin/settings.ts b/ui/src/services/admin/settings.ts index 5f370260..6bae3576 100644 --- a/ui/src/services/admin/settings.ts +++ b/ui/src/services/admin/settings.ts @@ -83,3 +83,8 @@ export const useDashBoard = () => { error, }; }; + +export const getAdminLanguageOptions = () => { + const apiUrl = `/answer/admin/api/language/options`; + return request.get(apiUrl); +}; diff --git a/ui/src/stores/interface.ts b/ui/src/stores/interface.ts index eddd1db8..e92e0f8a 100644 --- a/ui/src/stores/interface.ts +++ b/ui/src/stores/interface.ts @@ -1,6 +1,7 @@ import create from 'zustand'; import { AdminSettingsInterface } from '@/common/interface'; +import { DEFAULT_LANG } from '@/common/constants'; interface InterfaceType { interface: AdminSettingsInterface; @@ -11,7 +12,7 @@ const interfaceSetting = create((set) => ({ interface: { logo: '', theme: '', - language: '', + language: DEFAULT_LANG, time_zone: '', }, update: (params) => diff --git a/ui/src/stores/userInfo.ts b/ui/src/stores/userInfo.ts index cf1aa860..e82a592b 100644 --- a/ui/src/stores/userInfo.ts +++ b/ui/src/stores/userInfo.ts @@ -25,17 +25,21 @@ const initUser: UserInfoRes = { website: '', status: '', mail_status: 1, - language: '', + language: 'Default', }; const loggedUserInfoStore = create((set) => ({ user: initUser, - update: (params) => + update: (params) => { + if (!params.language) { + params.language = 'Default'; + } set(() => { Storage.set(LOGGED_TOKEN_STORAGE_KEY, params.access_token); Storage.set(LOGGED_USER_STORAGE_KEY, params); return { user: params }; - }), + }); + }, clear: () => set(() => { Storage.remove(LOGGED_TOKEN_STORAGE_KEY);