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 onValueChange({role: String(value)})} placeholder={t('base.please_select')} /> @@ -95,37 +106,37 @@ const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: U onValueChange({manual_payment: !!e.target.checked})}/> onValueChange({bill_page: !!e.target.checked})}/> onValueChange({apply_page: !!e.target.checked})}/> onValueChange({complete_button: !!e.target.checked})}/> onValueChange({confirm_button: !!e.target.checked})}/> onValueChange({apply_button: !!e.target.checked})}/> {user?.permissions?.role == 'root' && onValueChange({permission_edit: !!e.target.checked})}/> } @@ -161,15 +172,10 @@ const Permission = () => { const usernameOptionList = useMemo(() => (usernameList?usernameList.map(name => ({label: name, value: name})):[]), [usernameList]) const roleOptionList = useMemo(() => { - const list = [ - {label: 'ROOT', value: 'root'}, - {label: 'RO', value: 'ro'}, - {label: 'FO', value: 'fo'}, - ]; const userRole = user?.permissions?.role ?? 'staff'; - if(userRole == 'root') return list; - return list.filter(it => (it.value == userRole)); - }, []) + if(userRole == 'root') return RoleOptionList; + return RoleOptionList//.filter(it => (it.value == userRole)); + }, [user,state.allList]) const buildPermission = (it: UserPermission)=>{ it.permission_edit = !!it.permission_edit @@ -187,15 +193,15 @@ const Permission = () => { setState({loading: true}) getUserPermissionList().then(list => { setState({allList:[...list]}) - const userRole = user?.permissions?.role ?? 'staff'; + const userRole = (user?.permissions?.role ?? 'staff').toLowerCase(); list.forEach(it=>buildPermission(it)) if(userRole != 'root') { - list = list.filter(it => (it.role == userRole)); + list = list.filter(it => (it.role.toLowerCase() == userRole)); } setState({loading: false, list}) }) } - useEffect(loadUserPermissionList, []) + useEffect(loadUserPermissionList, [user]) // remove a user permission const removeItem = (index: number, id: number) => { @@ -244,17 +250,19 @@ const Permission = () => { } const process = value.id > 0 ? updateUserPermission : createUserPermission; try { - await process(value).then((newValue) => { + await process(value).then(() => { Toast.success(t('base.save_success')) - const newList = [...state.list]; - buildPermission(newValue) - newList[index] = { - ...newValue - } - setState({list: newList}) + //loadUserPermissionList(); + // const newList = [...state.list]; + // buildPermission(newValue) + // newList[index] = { + // ...newValue + // } + // setState({list: newList}) }) } catch (e) { Toast.error(t('base.save_failed') + `(${(e as Error).message})`) + throw e; } } } @@ -277,20 +285,22 @@ const Permission = () => { {user?.permissions?.role == 'root' && {t(`permission.title.permission`)}} {t('bill.title_operate')} - {state.list.map((it, index) => ( { - await handleChange(action, value, index) - }} - usernameOptionList={usernameOptionList} - roleOptionList={roleOptionList} - />))} - {state.list.length == 0 && - - } + + {state.list.map((it, index) => ( { + await handleChange(action, value, index) + }} + usernameOptionList={usernameOptionList} + roleOptionList={roleOptionList} + />))} + {state.list.length == 0 && + + } + {!state.loading && diff --git a/src/pages/bill/query.tsx b/src/pages/bill/query.tsx index 0f37e30..d2ff803 100644 --- a/src/pages/bill/query.tsx +++ b/src/pages/bill/query.tsx @@ -107,7 +107,6 @@ const BillQuery = () => { setShowBill(bill)} size={'small'} theme={'solid'} type={'primary'}>{t('base.qr-code')} - {AppMode == 'development' && 支付} >} { bill.status == BillStatus.PAID && <> diff --git a/src/routes/layout/dashboard-layout.tsx b/src/routes/layout/dashboard-layout.tsx index 1ce5a52..27277ff 100644 --- a/src/routes/layout/dashboard-layout.tsx +++ b/src/routes/layout/dashboard-layout.tsx @@ -74,7 +74,8 @@ const RoleSwitcher = ()=>{ - {user?.role?.toUpperCase()} + {/*{JSON.stringify(user)}*/} + {user?.permissions?.role.toUpperCase()} >) diff --git a/src/types/auth.d.ts b/src/types/auth.d.ts index da7c8be..9b1d6f2 100644 --- a/src/types/auth.d.ts +++ b/src/types/auth.d.ts @@ -44,6 +44,7 @@ declare type AuthContextType = { user?: UserProfile | null | undefined; logout: () => Promise; mockLogin: () => Promise; + refreshUserInfo: () => Promise; login: (code: string, state: string) => Promise; updateUser: (user: Partial) => Promise; };