a
This commit is contained in:
parent
1c1580b344
commit
c581174747
|
|
@ -4,7 +4,7 @@ import { api } from '../services/api'
|
||||||
import {
|
import {
|
||||||
setAccountId,
|
setAccountId,
|
||||||
setAccounts,
|
setAccounts,
|
||||||
selectFirstActiveAccount,
|
selectFirstAccount,
|
||||||
selectAccountId,
|
selectAccountId,
|
||||||
selectAccounts,
|
selectAccounts,
|
||||||
selectCurrentUser,
|
selectCurrentUser,
|
||||||
|
|
@ -38,9 +38,9 @@ const AccountSelector = ({ onChanged }) => {
|
||||||
user_id: item.user_id
|
user_id: item.user_id
|
||||||
}))
|
}))
|
||||||
dispatch(setAccounts(accountsList))
|
dispatch(setAccounts(accountsList))
|
||||||
// 自动选择第一个active账号
|
// 自动选择第一个账号(不管是否disabled)
|
||||||
if (accountsList.length > 0) {
|
if (accountsList.length > 0) {
|
||||||
dispatch(selectFirstActiveAccount())
|
dispatch(selectFirstAccount())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => dispatch(setAccounts([])))
|
.catch(() => dispatch(setAccounts([])))
|
||||||
|
|
@ -66,9 +66,9 @@ const AccountSelector = ({ onChanged }) => {
|
||||||
user_id: item.user_id
|
user_id: item.user_id
|
||||||
}))
|
}))
|
||||||
dispatch(setAccounts(accountsList))
|
dispatch(setAccounts(accountsList))
|
||||||
// 自动选择第一个active账号
|
// 自动选择第一个账号(不管是否disabled)
|
||||||
if (accountsList.length > 0) {
|
if (accountsList.length > 0) {
|
||||||
dispatch(selectFirstActiveAccount())
|
dispatch(selectFirstAccount())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => dispatch(setAccounts([])))
|
.catch(() => dispatch(setAccounts([])))
|
||||||
|
|
@ -115,28 +115,19 @@ const AccountSelector = ({ onChanged }) => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查当前选中的账号是否在新列表中且是 active 的
|
// 检查当前选中的账号是否在新列表中
|
||||||
const currentAccount = options.find((a) => a.id === accountId)
|
const currentAccount = options.find((a) => a.id === accountId)
|
||||||
if (currentAccount) {
|
if (currentAccount) {
|
||||||
// 如果当前账号是 disabled,需要切换到 active 账号
|
// 账号在新列表中,保持选中(不管是否disabled)
|
||||||
if (String(currentAccount?.status || 'active') === 'disabled') {
|
|
||||||
const firstActive = options.find((a) => String(a?.status || 'active') === 'active')
|
|
||||||
if (firstActive) {
|
|
||||||
dispatch(setAccountId(parseInt(String(firstActive.id || ''), 10)))
|
|
||||||
} else {
|
|
||||||
// 如果没有 active 账号,清空 accountId
|
|
||||||
dispatch(setAccountId(null))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果当前账号不在新列表中,选择第一个 active 账号
|
// 如果当前账号不在新列表中,选择第一个账号(不管是否disabled)
|
||||||
const firstActive = options.find((a) => String(a?.status || 'active') === 'active')
|
const firstAccount = options[0]
|
||||||
if (firstActive) {
|
if (firstAccount) {
|
||||||
dispatch(setAccountId(parseInt(String(firstActive.id || ''), 10)))
|
dispatch(setAccountId(parseInt(String(firstAccount.id || ''), 10)))
|
||||||
} else {
|
} else {
|
||||||
// 如果没有 active 账号,清空 accountId
|
// 如果没有账号,清空 accountId
|
||||||
dispatch(setAccountId(null))
|
dispatch(setAccountId(null))
|
||||||
}
|
}
|
||||||
}, [optionsKey, accountId, dispatch])
|
}, [optionsKey, accountId, dispatch])
|
||||||
|
|
@ -150,12 +141,7 @@ const AccountSelector = ({ onChanged }) => {
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const v = parseInt(e.target.value, 10)
|
const v = parseInt(e.target.value, 10)
|
||||||
if (Number.isFinite(v) && v > 0) {
|
if (Number.isFinite(v) && v > 0) {
|
||||||
// 检查选中的账号是否是 disabled
|
// 允许选择任何账号(包括disabled),由各个页面根据账号状态决定是否展示内容
|
||||||
const selectedAccount = options.find((a) => a.id === v)
|
|
||||||
if (selectedAccount && String(selectedAccount?.status || 'active') === 'disabled') {
|
|
||||||
// 如果选中的是 disabled 账号,不允许选择,保持当前值
|
|
||||||
return
|
|
||||||
}
|
|
||||||
dispatch(setAccountId(v))
|
dispatch(setAccountId(v))
|
||||||
} else {
|
} else {
|
||||||
dispatch(setAccountId(null))
|
dispatch(setAccountId(null))
|
||||||
|
|
@ -166,24 +152,11 @@ const AccountSelector = ({ onChanged }) => {
|
||||||
>
|
>
|
||||||
{options.length === 0 ? (
|
{options.length === 0 ? (
|
||||||
<option value="">{isAdmin && !effectiveUserId ? '请先选择用户' : '暂无账号'}</option>
|
<option value="">{isAdmin && !effectiveUserId ? '请先选择用户' : '暂无账号'}</option>
|
||||||
) : accountId === null ? (
|
|
||||||
<>
|
|
||||||
<option value="">请选择账号</option>
|
|
||||||
{options.map((a) => {
|
|
||||||
const isDisabled = String(a?.status || 'active') === 'disabled'
|
|
||||||
return (
|
|
||||||
<option key={a.id} value={a.id} disabled={isDisabled}>
|
|
||||||
#{a.id} {a.name || 'account'}
|
|
||||||
{isDisabled ? '(已禁用)' : ''}
|
|
||||||
</option>
|
|
||||||
)
|
|
||||||
})}
|
|
||||||
</>
|
|
||||||
) : (
|
) : (
|
||||||
options.map((a) => {
|
options.map((a) => {
|
||||||
const isDisabled = String(a?.status || 'active') === 'disabled'
|
const isDisabled = String(a?.status || 'active') === 'disabled'
|
||||||
return (
|
return (
|
||||||
<option key={a.id} value={a.id} disabled={isDisabled}>
|
<option key={a.id} value={a.id}>
|
||||||
#{a.id} {a.name || 'account'}
|
#{a.id} {a.name || 'account'}
|
||||||
{isDisabled ? '(已禁用)' : ''}
|
{isDisabled ? '(已禁用)' : ''}
|
||||||
</option>
|
</option>
|
||||||
|
|
|
||||||
|
|
@ -103,12 +103,11 @@ const appSlice = createSlice({
|
||||||
// 账号列表会在组件中异步加载,这里不自动切换accountId
|
// 账号列表会在组件中异步加载,这里不自动切换accountId
|
||||||
// 等待账号列表加载完成后再切换
|
// 等待账号列表加载完成后再切换
|
||||||
},
|
},
|
||||||
// 切换用户后,账号列表加载完成,自动选择第一个active账号
|
// 切换用户后,账号列表加载完成,自动选择第一个账号(不管是否disabled)
|
||||||
// 如果没有 active 账号,不改变当前 accountId(保持为 null 或之前的值)
|
selectFirstAccount: (state) => {
|
||||||
selectFirstActiveAccount: (state) => {
|
const firstAccount = state.accounts[0]
|
||||||
const firstActive = state.accounts.find((a) => String(a?.status || 'active') === 'active')
|
if (firstAccount) {
|
||||||
if (firstActive) {
|
const nextAccountId = parseInt(String(firstAccount.id || ''), 10)
|
||||||
const nextAccountId = parseInt(String(firstActive.id || ''), 10)
|
|
||||||
if (Number.isFinite(nextAccountId) && nextAccountId > 0) {
|
if (Number.isFinite(nextAccountId) && nextAccountId > 0) {
|
||||||
state.accountId = nextAccountId
|
state.accountId = nextAccountId
|
||||||
try {
|
try {
|
||||||
|
|
@ -124,7 +123,7 @@ const appSlice = createSlice({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 如果没有 active 账号,清空 accountId
|
// 如果没有账号,清空 accountId
|
||||||
state.accountId = null
|
state.accountId = null
|
||||||
try {
|
try {
|
||||||
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY)
|
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY)
|
||||||
|
|
@ -143,7 +142,7 @@ export const {
|
||||||
setAccounts,
|
setAccounts,
|
||||||
setUsers,
|
setUsers,
|
||||||
switchUser,
|
switchUser,
|
||||||
selectFirstActiveAccount,
|
selectFirstAccount,
|
||||||
} = appSlice.actions
|
} = appSlice.actions
|
||||||
|
|
||||||
// Selectors
|
// Selectors
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user