From 0488441fbbf87ed2ccd1cff2df7380c41e72a1ef Mon Sep 17 00:00:00 2001 From: lleohao <12764126+lleohao@users.noreply.github.com> Date: Wed, 11 Sep 2024 05:12:06 +0000 Subject: [PATCH] feat: save language to locale column --- .../features/user/components/account-languate.tsx | 6 ++---- apps/client/src/features/user/types/user.types.ts | 13 ++++++------- apps/client/src/features/user/user-provider.tsx | 2 +- apps/server/src/core/user/dto/update-user.dto.ts | 2 +- apps/server/src/core/user/user.service.ts | 15 ++++----------- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/apps/client/src/features/user/components/account-languate.tsx b/apps/client/src/features/user/components/account-languate.tsx index e498d0f8..2ac61337 100644 --- a/apps/client/src/features/user/components/account-languate.tsx +++ b/apps/client/src/features/user/components/account-languate.tsx @@ -1,7 +1,6 @@ import { Group, Text, Select } from "@mantine/core"; import { useTranslation } from "react-i18next"; import { updateUser } from "../services/user-service"; -import { IUser } from "../types/user.types"; import { useAtom } from "jotai"; import { userAtom } from "../atoms/current-user-atom"; import { useState } from "react"; @@ -26,11 +25,11 @@ function LanguageSwitcher() { const { t, i18n } = useTranslation(); const [user, setUser] = useAtom(userAtom); const [language, setLanguage] = useState( - user.settings?.preferences?.language || "en-US", + user?.locale === "en" ? "en-US" : user.locale, ); const handleChange = async (value: string) => { - const updatedUser = await updateUser({ language: value }); + const updatedUser = await updateUser({ locale: value }); setLanguage(value); setUser(updatedUser); @@ -52,4 +51,3 @@ function LanguageSwitcher() { /> ); } - diff --git a/apps/client/src/features/user/types/user.types.ts b/apps/client/src/features/user/types/user.types.ts index e73ec381..e9bf1220 100644 --- a/apps/client/src/features/user/types/user.types.ts +++ b/apps/client/src/features/user/types/user.types.ts @@ -1,11 +1,6 @@ import { IWorkspace } from "@/features/workspace/types/workspace.types"; -type IUserPreferences = { - fullPageWidth: boolean; - language: string; -}; - -export interface IUser extends IUserPreferences { +export interface IUser { id: string; name: string; email: string; @@ -16,12 +11,14 @@ export interface IUser extends IUserPreferences { invitedById: string; lastLoginAt: string; lastActiveAt: Date; + locale: string; createdAt: Date; updatedAt: Date; role: string; workspaceId: string; deactivatedAt: Date; deletedAt: Date; + fullPageWidth: boolean; // used for update } export interface ICurrentUser { @@ -30,5 +27,7 @@ export interface ICurrentUser { } export interface IUserSettings { - preferences: IUserPreferences + preferences: { + fullPageWidth: boolean; + }; } diff --git a/apps/client/src/features/user/user-provider.tsx b/apps/client/src/features/user/user-provider.tsx index 58e7ac3f..c5711863 100644 --- a/apps/client/src/features/user/user-provider.tsx +++ b/apps/client/src/features/user/user-provider.tsx @@ -13,7 +13,7 @@ export function UserProvider({ children }: React.PropsWithChildren) { if (data && data.user && data.workspace) { setCurrentUser(data); i18n.changeLanguage( - data.user?.settings?.preferences?.language || "en-US", + data.user.locale === "en" ? "en-US" : data.user.locale, ); } }, [data, isLoading]); diff --git a/apps/server/src/core/user/dto/update-user.dto.ts b/apps/server/src/core/user/dto/update-user.dto.ts index e93eb9fa..ff3201c5 100644 --- a/apps/server/src/core/user/dto/update-user.dto.ts +++ b/apps/server/src/core/user/dto/update-user.dto.ts @@ -15,5 +15,5 @@ export class UpdateUserDto extends PartialType( @IsOptional() @IsString() - language: string; + locale: string; } diff --git a/apps/server/src/core/user/user.service.ts b/apps/server/src/core/user/user.service.ts index 428004b0..7909b548 100644 --- a/apps/server/src/core/user/user.service.ts +++ b/apps/server/src/core/user/user.service.ts @@ -33,13 +33,6 @@ export class UserService { ); } - if (typeof updateUserDto.language !== 'undefined') { - return this.updateUserLanguagePreference( - userId, - updateUserDto.language, - ); - } - if (updateUserDto.name) { user.name = updateUserDto.name; } @@ -55,6 +48,10 @@ export class UserService { user.avatarUrl = updateUserDto.avatarUrl; } + if (updateUserDto.locale) { + user.locale = updateUserDto.locale; + } + await this.userRepo.updateUser(updateUserDto, userId, workspaceId); return user; } @@ -66,8 +63,4 @@ export class UserService { fullPageWidth, ); } - - async updateUserLanguagePreference(userId: string, language: string) { - return this.userRepo.updatePreference(userId, 'language', language); - } }