diff --git a/api/api/endpoints/account.py b/api/api/endpoints/account.py index f16d750..af68e4a 100644 --- a/api/api/endpoints/account.py +++ b/api/api/endpoints/account.py @@ -80,7 +80,7 @@ async def create_account( ) -@api_router.put("/{user_id}", dependencies=[Depends(bearer_schema)], response_model=User) +@api_router.put("/{user_id}", dependencies=[Depends(bearer_schema)], response_model=UserUpdate) async def update_account( user_id: int, user_update: UserUpdate, @@ -98,7 +98,7 @@ async def update_account( if update_values is None: return user - user_update_data = User.model_validate({**user.model_dump(), **update_values}) + user_update_data = UserUpdate.model_validate({**user.model_dump(), **update_values}) await update_user_by_id(connection, update_values, user) diff --git a/client/src/api/api.ts b/client/src/api/api.ts index aa4eb6d..714b26f 100644 --- a/client/src/api/api.ts +++ b/client/src/api/api.ts @@ -3,7 +3,7 @@ import axiosRetry from 'axios-retry'; import { Auth, Tokens } from '@/types/auth'; import { useAuthStore } from '@/store/authStore'; import { AuthService } from '@/services/authService'; -import { User, UserCreate } from '@/types/user'; +import { User, UserCreate, UserUpdate } from '@/types/user'; const baseURL = `${import.meta.env.VITE_APP_HTTP_PROTOCOL}://${ import.meta.env.VITE_APP_API_URL @@ -119,8 +119,12 @@ const api = { return response.data; }, + async updateUser(userId: number, user: UserUpdate): Promise { + const response = await base.put(`/account/${userId}`, user); + return response.data; + }, + // keyrings - async setPassword(userId: number, password: string): Promise {}, }; export default api; diff --git a/client/src/components/UserEdit.tsx b/client/src/components/UserEdit.tsx index 14b430a..4b89020 100644 --- a/client/src/components/UserEdit.tsx +++ b/client/src/components/UserEdit.tsx @@ -1,7 +1,8 @@ import { UserService } from '@/services/userService'; import { useUserSelector } from '@/store/userStore'; -import { User } from '@/types/user'; -import { Button, Form, Input, Select } from 'antd'; +import { User, UserUpdate } from '@/types/user'; +import { LoadingOutlined } from '@ant-design/icons'; +import { Button, Form, Input, Select, Spin } from 'antd'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; @@ -15,7 +16,18 @@ export default function UserEdit({ userId }: UserEditProps) { const currentUser = useUserSelector(); const [form] = Form.useForm(); const { t } = useTranslation(); - const [user, setUser] = useState(null); + const [user, setUser] = useState({ + id: 0, + name: '', + login: '', + email: '', + bindTenantId: '', + role: 'VIEWER', + meta: {}, + createdAt: '', + status: 'ACTIVE', + }); + const [loading, setLoading] = useState(false); useEffect(() => { async function getUser() { @@ -30,13 +42,30 @@ export default function UserEdit({ userId }: UserEditProps) { getUser(); }, []); + const onFinish = async (values: UserUpdate) => { + setLoading(true); + let updatedUser: Partial = {}; + + (Object.keys(values) as Array).forEach((key) => { + if (values[key] !== user[key]) { + updatedUser = { ...updatedUser, [key]: values[key] }; + } + }); + + if (Object.keys(updatedUser).length > 0) { + await UserService.updateUser(userId!, updatedUser); + } + + setLoading(false); + }; + return (
@@ -123,12 +152,21 @@ export default function UserEdit({ userId }: UserEditProps) { block style={{ color: '#000' }} > - save{' '} - {t('save')} + {loading ? ( + <> + } size="small">{' '} + {t('saving')} + + ) : ( + <> + save{' '} + {t('save')} + + )}
diff --git a/client/src/services/userService.ts b/client/src/services/userService.ts index c8a3172..0bdb99f 100644 --- a/client/src/services/userService.ts +++ b/client/src/services/userService.ts @@ -1,5 +1,5 @@ import api from '@/api/api'; -import { AllUserResponse, User, UserCreate } from '@/types/user'; +import { AllUserResponse, User, UserCreate, UserUpdate } from '@/types/user'; export class UserService { static async getProfile(): Promise { @@ -29,4 +29,10 @@ export class UserService { const createdUser = api.createUser(user); return createdUser; } + + static async updateUser(userId: number, user: UserUpdate): Promise { + console.log('updateUser'); + const updatedUser = api.updateUser(userId, user); + return updatedUser; + } }