修复:目标保存后取消读取时自动重试并切换年度
This commit is contained in:
@@ -6,6 +6,7 @@ import { CompactManagementTables, QualityTable as Table } from './QualityTable';
|
||||
import { ApiError, api } from './api';
|
||||
import { METRIC_NAMES, matchingTarget, ruleObject as obj, ruleList, type RuleDefinition as Row } from './rulePresentation';
|
||||
import { readableSelectPopup } from './readableSelectPopup';
|
||||
import { useMaintenanceRefresh } from './pageCache';
|
||||
|
||||
const DIMENSIONS=['department','group','product','projectGroup'] as const;
|
||||
const dimensionName={department:'产业部',group:'业务分组',product:'产品',projectGroup:'项目组'};
|
||||
@@ -22,17 +23,36 @@ export function QualityTargetsPanel({onUnauthorized}:{onUnauthorized:(error:unkn
|
||||
const [year,setYear]=useState(new Date().getFullYear()),[metric,setMetric]=useState('COMPOSITE_RATE');
|
||||
const [form]=Form.useForm<TargetForm>();const scope=Form.useWatch('scope',form),inherit=Form.useWatch('inherit',form),department=Form.useWatch('department',form);
|
||||
const saveLock=useRef(false);
|
||||
const readGeneration=useRef(0),mounted=useRef(false);
|
||||
const selectedYear=Form.useWatch('year',form),selectedMetric=Form.useWatch('metric',form);
|
||||
const load=useCallback(async()=>{
|
||||
if(!mounted.current)return;
|
||||
const generation=++readGeneration.current;
|
||||
const current=()=>mounted.current&&generation===readGeneration.current;
|
||||
setLoading(true);setError('');
|
||||
try{
|
||||
const list=await api.targetVersions();const active=list.items.find(row=>String(row.status).toUpperCase()==='ACTIVE');
|
||||
if(!active){setVersion(null);setTargets([]);return;}
|
||||
const [detail,names]=await Promise.all([api.targetVersion(String(active.id)),api.referenceNames()]);
|
||||
setVersion(obj(detail.version||active));setTargets(ruleList(detail.targets));setReferences(names);
|
||||
}catch(e){if(!onUnauthorized(e))setError(e instanceof Error?e.message:'目标读取失败');}finally{setLoading(false);}
|
||||
for(let attempt=0;attempt<3;attempt++){
|
||||
try{
|
||||
const list=await api.targetVersions();if(!current())return;
|
||||
const active=list.items.find(row=>String(row.status).toUpperCase()==='ACTIVE');
|
||||
if(!active){setVersion(null);setTargets([]);return;}
|
||||
const [detail,names]=await Promise.all([api.targetVersion(String(active.id)),api.referenceNames()]);
|
||||
if(!current())return;
|
||||
setVersion(obj(detail.version||active));setTargets(ruleList(detail.targets));setReferences(names);return;
|
||||
}catch(e){
|
||||
if(!current())return;
|
||||
// 保存或跨页版本刷新会取消旧读取,只重读数据,绝不重复提交目标。
|
||||
if(e instanceof Error&&e.name==='AbortError'){
|
||||
if(attempt<2)continue;
|
||||
throw new Error('目标数据正在刷新,请稍后点击重试');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}catch(e){if(current()&&!onUnauthorized(e))setError(e instanceof Error?e.message:'目标读取失败');}finally{if(current())setLoading(false);}
|
||||
},[onUnauthorized]);
|
||||
useEffect(()=>{void load();},[load]);
|
||||
useEffect(()=>{mounted.current=true;void load();return()=>{mounted.current=false;readGeneration.current++;};},[load]);
|
||||
useMaintenanceRefresh(()=>void load());
|
||||
const annual=targets.filter(row=>String(row.periodType).toUpperCase()==='YEAR');
|
||||
const years=[...new Set([new Date().getFullYear(),...annual.map(row=>Number(row.year))])].sort((a,b)=>a-b);
|
||||
const overall=annual.find(row=>row.metric===metric&&row.year===year&&!Object.keys(dimensions(row.dimensions)).length);
|
||||
@@ -56,7 +76,7 @@ export function QualityTargetsPanel({onUnauthorized}:{onUnauthorized:(error:unkn
|
||||
};
|
||||
const persist=async(next:Row[])=>{
|
||||
if(!version||saveLock.current)return false;saveLock.current=true;setSaving(true);
|
||||
try{await api.updateCurrentTargets({targets:next.map(targetInput),expectedRowVersion:Number(version.rowVersion||0)});message.success('目标已更新');setEditing(null);await load();return true;}
|
||||
try{const saved=await api.updateCurrentTargets({targets:next.map(targetInput),expectedRowVersion:Number(version.rowVersion||0)});if(saved.version)setVersion(obj(saved.version));if(Array.isArray(saved.targets))setTargets(ruleList(saved.targets));message.success('目标已更新');setEditing(null);await load();return true;}
|
||||
catch(e){if(!onUnauthorized(e))message.error(e instanceof ApiError&&e.status===409?'目标已被其他人修改,请刷新后核对':e instanceof Error?e.message:'目标保存失败');return false;}
|
||||
finally{saveLock.current=false;setSaving(false);}
|
||||
};
|
||||
@@ -72,7 +92,7 @@ export function QualityTargetsPanel({onUnauthorized}:{onUnauthorized:(error:unkn
|
||||
next=[...next,{...prior,metric:f.metric,periodType:'YEAR',year:f.year,dimensions:d,value:Number(f.value)/100,unit:'RATE'}];
|
||||
}
|
||||
if(!next.some(row=>String(row.periodType).toUpperCase()==='YEAR'&&!Object.keys(dimensions(row.dimensions)).length)){message.error('至少保留一项总体年度目标');return;}
|
||||
await persist(next);
|
||||
if(await persist(next)){setYear(f.year);setMetric(f.metric);}
|
||||
}catch{ /* 表单保留在原位置显示字段错误。 */ }
|
||||
};
|
||||
const defaultTarget=matchingTarget(annual.filter(row=>row.metric===selectedMetric&&row.year===selectedYear&&(!editing||identity(row)!==identity(editing))),dimensions(form.getFieldsValue(true)));
|
||||
|
||||
Reference in New Issue
Block a user