From c67bcc5d0726a2c371f193ab0a9ef97297e6145d Mon Sep 17 00:00:00 2001 From: callmeyan Date: Tue, 10 Sep 2024 11:43:25 +0800 Subject: [PATCH] fixed: update user role change --- src/contexts/auth/index.tsx | 13 +++- src/pages/auth/permission.tsx | 100 ++++++++++++++----------- src/pages/bill/query.tsx | 1 - src/routes/layout/dashboard-layout.tsx | 3 +- src/types/auth.d.ts | 1 + 5 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/contexts/auth/index.tsx b/src/contexts/auth/index.tsx index 581bbab..3742125 100644 --- a/src/contexts/auth/index.tsx +++ b/src/contexts/auth/index.tsx @@ -52,6 +52,16 @@ export const AuthProvider = ({children}: { children: React.ReactNode }) => { const [state, dispatch] = useReducer(authReducer, initialState); // MOCK INIT DATA + const refreshUserInfo = async ()=>{ + const user = await getUserInfo(); + dispatch({ + action: 'refresh', + payload: { + isLoggedIn: !!user, + user: getInitUserData(user) + } + }) + } const init = async () => { const token = getAuthToken(); if (!token) { @@ -141,7 +151,8 @@ export const AuthProvider = ({children}: { children: React.ReactNode }) => { return ({children}) } export default AuthContext \ No newline at end of file diff --git a/src/pages/auth/permission.tsx b/src/pages/auth/permission.tsx index 9cb2016..68c51f7 100644 --- a/src/pages/auth/permission.tsx +++ b/src/pages/auth/permission.tsx @@ -1,5 +1,5 @@ import {useSetState} from "ahooks"; -import {Button, Checkbox, Empty, Popconfirm, Select, Space, Toast} from "@douyinfe/semi-ui"; +import {Button, Checkbox, Empty, Popconfirm, Select, Space, Spin, Toast} from "@douyinfe/semi-ui"; import {useTranslation} from "react-i18next"; import {useEffect, useMemo} from "react"; @@ -25,6 +25,12 @@ import useAuth from "@/hooks/useAuth.ts"; // 'permission' // ] +const RoleOptionList = [ + {label: 'ROOT', value: 'root'}, + {label: 'RO', value: 'ro'}, + {label: 'FO', value: 'fo'}, +]; + type UserPermissionItemProps = { it: UserPermission; onChange: (action: 'remove' | 'saved' | 'modify', value: UserPermission) => Promise; @@ -34,13 +40,15 @@ type UserPermissionItemProps = { const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: UserPermissionItemProps) => { const {t} = useTranslation() - const {user} = useAuth() + const {user,refreshUserInfo} = useAuth() const [state,setState] = useSetState({ - loading: false + loading: false, + currentRole: it.role }) const onValueChange = (value: { [key: string]: string | boolean }) => { + onChange('modify', { ...it, ...value @@ -63,8 +71,11 @@ const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: U return; } setState({loading: true}) - onChange('saved', it).finally(()=>{ + onChange('saved', it).then(()=>{ + if(self) refreshUserInfo().then(()=>console.log('self refresh')); + }).finally(()=>{ setState({loading: false}) + }) } @@ -77,7 +88,7 @@ const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: U filter optionList={usernameOptionList} placeholder={t('base.please_select')} - defaultValue={it.username} + value={it.username} onChange={(value) => onValueChange({username: String(value)})} />} @@ -87,7 +98,7 @@ const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: U