fix: stack overflow

This commit is contained in:
sealday 2024-03-12 23:19:36 +08:00
parent 2f57f697b5
commit fa0d01fd47
4 changed files with 77 additions and 111 deletions

View File

@ -1,109 +1,40 @@
import _ from 'lodash';
import { Spin } from 'antd';
import React, { useEffect, useState } from 'react';
import { onFieldValueChange, onFieldInit, FormPath } from '@formily/core';
import { useField, useFieldSchema, useForm, useFormEffects } from '@formily/react';
import { useRequest } from '@nocobase/client';
import React from 'react';
import { observer, useField, useFieldSchema, useForm } from '@formily/react';
import {
CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD,
KEY_CUSTOM_COMPONENT_LABEL,
KEY_CUSTOM_COMPONENT_TYPE,
} from '@hera/plugin-core/client';
import { useFeeItems } from '../hooks';
import { useDeepCompareEffect } from 'ahooks';
export const RecordFeeScope = () => {
export const RecordFeeScope = observer(() => {
const form = useForm();
const [contractPlanId, setContractPlanId] = useState(-1);
const [feeProducts, setFeeProducts] = useState([]);
const [product, setProduct] = useState(null);
const fieldSchema = useFieldSchema();
const field = useField();
const path: any = field.path.entire;
const fieldPath = path?.replace(`.${fieldSchema.name}`, '');
const productPath = FormPath.parse('.product', fieldPath).toString();
const { loading } = useRequest<any>(
{
resource: 'contract_plan_lease_items',
action: 'list',
params: {
appends: ['fee_items', 'products'],
filter: {
$and: [
{
contract_plan_id: contractPlanId,
},
{
products: {
category_id: product?.category_id ?? -1,
},
},
],
},
pageSize: 9999,
},
},
{
refreshDeps: [contractPlanId, product?.id],
onSuccess(data) {
if (data?.data?.length > 0) {
const items = data.data as {
products: { category_id: Number }[];
fee_items: { fee_product_id: Number }[];
}[];
const result = items.reduce((acc, current) => {
acc.push(...current.fee_items.map((item) => ({ id: item.fee_product_id })));
return acc;
}, []);
setFeeProducts(result);
} else {
setFeeProducts([]);
}
},
},
);
useFormEffects(() => {
onFieldInit('contract_plan', () => {
if (form.values.contract_plan) {
setContractPlanId(form.values.contract_plan.id);
} else {
setContractPlanId(-1);
}
}),
onFieldValueChange('contract_plan', () => {
if (form.values.contract_plan) {
setContractPlanId(form.values.contract_plan.id);
} else {
setContractPlanId(-1);
}
});
});
useFormEffects(() => {
onFieldInit(productPath, () => {
if (form.getValuesIn(productPath)) {
setProduct(form.getValuesIn(productPath));
} else {
setProduct(null);
}
}),
onFieldValueChange(productPath, () => {
if (form.getValuesIn(productPath)) {
setProduct(form.getValuesIn(productPath));
} else {
setProduct(null);
}
});
});
useEffect(() => {
if (loading) {
return;
}
form.setValuesIn(fieldPath, feeProducts);
}, [loading, feeProducts, fieldPath]);
const item = form.getValuesIn(field.path.slice(0, -2).entire);
const { data, loading } = useFeeItems(item.product?.category_id, form.values.contract_plan?.id);
let result = [];
if (data?.data?.length > 0) {
const items = data.data as {
products: { category_id: Number }[];
fee_items: { fee_product_id: Number }[];
}[];
result = items.reduce((acc, current) => {
acc.push(...current.fee_items.map((item) => ({ id: item.fee_product_id })));
return acc;
}, []);
}
useDeepCompareEffect(() => {
form.setValuesIn(fieldPath, result);
}, [result, form, fieldPath]);
return loading ? <Spin /> : <></>;
};
});
RecordFeeScope.displayName = 'RecordFeeScope';
RecordFeeScope[KEY_CUSTOM_COMPONENT_TYPE] = CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD;

View File

@ -17,7 +17,6 @@ export const RecordProductScope = observer(() => {
const inContractPlanId = form.getValuesIn('in_contract_plan')?.id;
const outContractPlanId = form.getValuesIn('out_contract_plan')?.id;
console.log('产品范围');
let required = { price: false, contract: false, inContract: false, outContract: false };
const { data, loading } = useCachedRequest<any>({
resource: 'product_category',
@ -71,24 +70,24 @@ export const RecordProductScope = observer(() => {
default:
break;
}
let result = data?.data ?? [];
if (required.inContract && required.outContract) {
// 租赁直发单
const intersection = _.intersectionBy(inContractProducts, outContractProducts, 'id');
result = _.intersectionBy(result, intersection, 'id');
} else if (required.price && required.inContract) {
const intersection = _.intersectionBy(inContractProducts, priceProducts, 'id');
result = _.intersectionBy(result, intersection, 'id');
} else if (required.price) {
result = _.intersectionBy(result, priceProducts, 'id');
} else if (required.contract) {
result = _.intersectionBy(result, contractProducts, 'id');
}
useDeepCompareEffect(() => {
let result = data?.data ?? [];
if (required.inContract && required.outContract) {
// 租赁直发单
const intersection = _.intersectionBy(inContractProducts, outContractProducts, 'id');
result = _.intersectionBy(result, intersection, 'id');
} else if (required.price && required.inContract) {
const intersection = _.intersectionBy(inContractProducts, priceProducts, 'id');
result = _.intersectionBy(result, intersection, 'id');
} else if (required.price) {
result = _.intersectionBy(result, priceProducts, 'id');
} else if (required.contract) {
result = _.intersectionBy(result, contractProducts, 'id');
}
form.setValues({
product_scope: result,
});
}, [required, data, priceProducts, contractProducts, inContractProducts, outContractProducts, form]);
}, [result, form]);
return loading || leaseItemsLoading || inLeaseItemsLoading || outLeaseItemsLoading ? <Spin /> : <></>;
});

View File

@ -75,8 +75,9 @@ export const RecordSummary = observer((props): any => {
const outContractSummary = {};
// 报价小结
const quoteSummary = {};
form.values.items?.forEach((element) => {
if (!element.product || !element.count) return;
form.values.items?.forEach((item) => {
if (!item.product || !item.count) return;
const element = _.cloneDeep(item);
// 获取产品的分类数据信息
const productCategory = reqProduct.data.data?.find(
(product) => product.category_id === element.product?.category_id,
@ -142,11 +143,11 @@ export const RecordSummary = observer((props): any => {
];
const trans: any[] = resultItems
.map((item, index) => {
.map((item) => {
if (item.value.length) {
const data = {
label: item.label,
key: index,
key: item.label,
children: <Descriptions items={item.value} />,
};
return data;
@ -272,7 +273,7 @@ const transDescriptions = (data) => {
const values = data.map((item: any, index) => {
if (item.total) {
return {
key: index,
key: item.name,
label: item.name,
children: item.name === '总金额' ? formatCurrency(item.total, 2) : formatQuantity(item.total, 3) + item.unit,
};

View File

@ -29,3 +29,38 @@ export const useLeaseItems = (planId) => {
}, [planId]);
return { data, loading };
};
export const useFeeItems = (categoryId, planId) => {
const { data, loading, run } = useCachedRequest<any>(
{
resource: 'contract_plan_lease_items',
action: 'list',
params: {
appends: ['fee_items', 'products'],
filter: {
$and: [
{
contract_plan_id: planId,
},
{
products: {
category_id: categoryId ?? -1,
},
},
],
},
pageSize: 99999,
},
},
{
manual: true,
},
);
useEffect(() => {
if (planId && categoryId) {
run();
}
}, [planId, categoryId]);
return { data, loading };
};