fix: 修复审批列表选项卡切换时使用旧状态值的问题

- 修复 loadData 函数,添加 tab 参数直接传递当前选中的标签
- 修复文件中的重复代码问题
- 添加更详细的调试日志

问题原因:setActiveTab 是异步的,handleTabChange 中调用 loadData 时,
activeTab 还是旧值,导致请求参数错误。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-02-26 00:10:08 +08:00
parent ab62b9839e
commit 40384d7f26
+11 -24
View File
@@ -52,40 +52,25 @@ export default function ApprovalList() {
const [hasMore, setHasMore] = useState(true)
const [page, setPage] = useState(1)
// 加载数据
const loadData = async (pageNum: number = 1, isRefresh: boolean = false) => {
// 加载数据 - 接收 tab 参数避免使用旧的 activeTab
const loadData = async (pageNum: number = 1, isRefresh: boolean = false, tab?: string) => {
if (loading) return
console.log('🔍 Loading approval list:', { activeTab, pageNum, isRefresh })
// 使用传入的 tab 参数,如果没有则使用当前 activeTab
const currentTab = tab || activeTab
console.log('🔍 Loading approval list:', { currentTab, activeTab, tab, pageNum, isRefresh })
setLoading(true)
try {
const result = await approvalApi.getList({
type: activeTab,
type: currentTab,
page: pageNum,
page_size: 20,
})
console.log('📦 API response:', { type: activeTab, result })
console.log('📦 API response:', { type: currentTab, count: result?.list?.length, result })
const newList = result.list || []
if (isRefresh || pageNum === 1) {
setList(newList)
} else {
setList([...list, ...newList])
}
setHasMore(newList.length >= 20)
setPage(pageNum)
} catch (error: any) {
console.error('Failed to load approvals:', error)
} finally {
setLoading(false)
}
}
const newList = result.list || []
const newList = result?.list || []
if (isRefresh || pageNum === 1) {
setList(newList)
@@ -104,11 +89,13 @@ export default function ApprovalList() {
// 切换标签
const handleTabChange = (key: string) => {
console.log('🔄 Tab changing from', activeTab, 'to', key)
setActiveTab(key as 'my' | 'pending' | 'approved')
setList([])
setHasMore(true)
setPage(1)
loadData(1, true)
// 直接传入新的 tab 值,避免使用旧的 activeTab
loadData(1, true, key)
}
// 刷新