fix(clearance): 避免任务期次切换显示旧空数据
This commit is contained in:
parent
dbeff6033c
commit
c0018fdb58
@ -846,6 +846,7 @@ export function ClearanceView({
|
||||
const [taskEditSaving, setTaskEditSaving] = useState(false)
|
||||
const [taskEditDrafts, setTaskEditDrafts] = useState<Record<string, TaskEditDraft>>({})
|
||||
const taskProgressRetryCountRef = useRef(0)
|
||||
const taskRequestSequenceRef = useRef(0)
|
||||
const handledProgressSyncRef = useRef<string | null>(null)
|
||||
|
||||
const [scores, setScores] = useState<ScoresData | null>(null)
|
||||
@ -983,9 +984,14 @@ export function ClearanceView({
|
||||
if (tab !== 'tasks' && tab !== 'pool') return
|
||||
if (tab === 'pool' && !pool?.periodKey) return
|
||||
const controller = new AbortController()
|
||||
const requestSequence = ++taskRequestSequenceRef.current
|
||||
const isCurrentRequest = () => (
|
||||
taskRequestSequenceRef.current === requestSequence && !controller.signal.aborted
|
||||
)
|
||||
let progressRetryTimer: number | null = null
|
||||
setTaskLoading(true)
|
||||
setTaskError('')
|
||||
if (tab === 'tasks') setTasks(null)
|
||||
const summaryPeriod = tab === 'pool'
|
||||
? pool?.periodKey ?? taskPeriod
|
||||
: taskPeriod || currentPeriodKey()
|
||||
@ -1000,6 +1006,7 @@ export function ClearanceView({
|
||||
{ signal: controller.signal, cache: 'no-store' },
|
||||
)
|
||||
.then((data) => {
|
||||
if (!isCurrentRequest()) return
|
||||
setTasks(data)
|
||||
if (!taskPeriod && data.periodKey) setTaskPeriod(data.periodKey)
|
||||
const progressTarget = clearanceProgressTargetDate(data.periodKey)
|
||||
@ -1026,6 +1033,7 @@ export function ClearanceView({
|
||||
},
|
||||
)
|
||||
.then((response) => {
|
||||
if (!isCurrentRequest()) return
|
||||
const syncFinishedAt = response.status?.lastFinishedAt ?? null
|
||||
const hasUnhandledFinishedSync = Boolean(
|
||||
syncFinishedAt && handledProgressSyncRef.current !== syncFinishedAt,
|
||||
@ -1058,7 +1066,7 @@ export function ClearanceView({
|
||||
`/api/supply-chain/clearance/tasks/progress-refresh?period=${encodeURIComponent(data.periodKey)}`,
|
||||
{ signal: controller.signal },
|
||||
)
|
||||
if (controller.signal.aborted) return
|
||||
if (!isCurrentRequest()) return
|
||||
if (!status.running && status.lastError) {
|
||||
setTaskError(
|
||||
`${status.lastError},当前显示截至 ${data.progress.through ?? '上次同步'} 的数据`,
|
||||
@ -1067,7 +1075,7 @@ export function ClearanceView({
|
||||
}
|
||||
setTaskRefreshKey((value) => value + 1)
|
||||
} catch (reason) {
|
||||
if ((reason as Error).name !== 'AbortError') {
|
||||
if ((reason as Error).name !== 'AbortError' && isCurrentRequest()) {
|
||||
setTaskError('无法获取发货同步状态,请手动刷新重试')
|
||||
}
|
||||
}
|
||||
@ -1075,16 +1083,16 @@ export function ClearanceView({
|
||||
})
|
||||
.catch((reason: Error) => {
|
||||
console.warn('[clearance] progress refresh queue failed', reason)
|
||||
if (!controller.signal.aborted) {
|
||||
if (isCurrentRequest()) {
|
||||
setTaskError('无法启动发货同步,请稍后手动刷新')
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch((reason: Error) => {
|
||||
if (reason.name !== 'AbortError') setTaskError(reason.message)
|
||||
if (reason.name !== 'AbortError' && isCurrentRequest()) setTaskError(reason.message)
|
||||
})
|
||||
.finally(() => {
|
||||
if (!controller.signal.aborted) setTaskLoading(false)
|
||||
if (isCurrentRequest()) setTaskLoading(false)
|
||||
})
|
||||
return () => {
|
||||
controller.abort()
|
||||
@ -2428,6 +2436,8 @@ export function ClearanceView({
|
||||
else {
|
||||
if (item.key === 'tasks' && pool?.periodKey) {
|
||||
setTaskPeriod(pool.periodKey)
|
||||
setTasks(null)
|
||||
setTaskLoading(true)
|
||||
setTaskPage(1)
|
||||
setSelectedTaskIds([])
|
||||
}
|
||||
@ -3013,6 +3023,8 @@ export function ClearanceView({
|
||||
onChange={(event) => {
|
||||
taskProgressRetryCountRef.current = 0
|
||||
setTaskPeriod(event.target.value)
|
||||
setTasks(null)
|
||||
setTaskLoading(true)
|
||||
setTaskPage(1)
|
||||
setSelectedTaskIds([])
|
||||
}}
|
||||
@ -3030,6 +3042,8 @@ export function ClearanceView({
|
||||
type="button"
|
||||
onClick={() => {
|
||||
taskProgressRetryCountRef.current = 0
|
||||
setTasks(null)
|
||||
setTaskLoading(true)
|
||||
setSelectedTaskIds([])
|
||||
setTaskRefreshKey((value) => value + 1)
|
||||
}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user