优化客户端请求时候操作的页面不更新
This commit is contained in:
@@ -249,6 +249,28 @@ export default function RuleNew() {
|
||||
setInstanceKey(`new_${Date.now()}`);
|
||||
}, []);
|
||||
|
||||
|
||||
/**
|
||||
* 从API响应中提取数据
|
||||
* @param responseData - API响应数据
|
||||
* @returns 提取的数据或null
|
||||
*/
|
||||
function extractApiData<T>(responseData: unknown): T | null {
|
||||
if (!responseData) return null;
|
||||
|
||||
// 格式1: { code: number, msg: string, data: T }
|
||||
if (typeof responseData === 'object' && responseData !== null &&
|
||||
'code' in responseData &&
|
||||
'data' in responseData &&
|
||||
(responseData as { data: unknown }).data) {
|
||||
return (responseData as { data: T }).data;
|
||||
}
|
||||
|
||||
// 格式2: 直接是数据对象
|
||||
return responseData as T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取评查点数据
|
||||
* 编辑模式下从API获取指定ID的评查点数据
|
||||
@@ -266,17 +288,17 @@ export default function RuleNew() {
|
||||
};
|
||||
const response = await postgrestGet('evaluation_points', postgrestParams);
|
||||
|
||||
if (response.data && Array.isArray(response.data) && response.data[0]) {
|
||||
if (response.data) {
|
||||
// 使用extractApiData从响应中提取数据
|
||||
const evaluationPoints = extractApiData<EvaluationPoint[]>(response.data);
|
||||
|
||||
if (response.data.length > 0) {
|
||||
if (evaluationPoints && Array.isArray(evaluationPoints) && evaluationPoints.length > 0) {
|
||||
try {
|
||||
// 使用JSON序列化和反序列化来进行深拷贝,避免浏览器差异
|
||||
const originalData = response.data[0];
|
||||
const originalData = evaluationPoints[0];
|
||||
const jsonString = JSON.stringify(originalData);
|
||||
const data = JSON.parse(jsonString);
|
||||
|
||||
// console.log("数据已经过深拷贝处理,避免浏览器兼容性问题");
|
||||
|
||||
// 设置表单数据
|
||||
setFormData(data);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user