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 _ from 'lodash';
import { Spin } from 'antd'; import { Spin } from 'antd';
import React, { useEffect, useState } from 'react'; import React from 'react';
import { onFieldValueChange, onFieldInit, FormPath } from '@formily/core'; import { observer, useField, useFieldSchema, useForm } from '@formily/react';
import { useField, useFieldSchema, useForm, useFormEffects } from '@formily/react';
import { useRequest } from '@nocobase/client';
import { import {
CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD, CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD,
KEY_CUSTOM_COMPONENT_LABEL, KEY_CUSTOM_COMPONENT_LABEL,
KEY_CUSTOM_COMPONENT_TYPE, KEY_CUSTOM_COMPONENT_TYPE,
} from '@hera/plugin-core/client'; } from '@hera/plugin-core/client';
import { useFeeItems } from '../hooks';
import { useDeepCompareEffect } from 'ahooks';
export const RecordFeeScope = () => { export const RecordFeeScope = observer(() => {
const form = useForm(); const form = useForm();
const [contractPlanId, setContractPlanId] = useState(-1);
const [feeProducts, setFeeProducts] = useState([]);
const [product, setProduct] = useState(null);
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const field = useField(); const field = useField();
const path: any = field.path.entire; const path: any = field.path.entire;
const fieldPath = path?.replace(`.${fieldSchema.name}`, ''); const fieldPath = path?.replace(`.${fieldSchema.name}`, '');
const productPath = FormPath.parse('.product', fieldPath).toString(); const item = form.getValuesIn(field.path.slice(0, -2).entire);
const { data, loading } = useFeeItems(item.product?.category_id, form.values.contract_plan?.id);
const { loading } = useRequest<any>( let result = [];
{ if (data?.data?.length > 0) {
resource: 'contract_plan_lease_items', const items = data.data as {
action: 'list', products: { category_id: Number }[];
params: { fee_items: { fee_product_id: Number }[];
appends: ['fee_items', 'products'], }[];
filter: { result = items.reduce((acc, current) => {
$and: [ acc.push(...current.fee_items.map((item) => ({ id: item.fee_product_id })));
{ return acc;
contract_plan_id: contractPlanId, }, []);
}, }
{ useDeepCompareEffect(() => {
products: { form.setValuesIn(fieldPath, result);
category_id: product?.category_id ?? -1, }, [result, form, fieldPath]);
},
},
],
},
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]);
return loading ? <Spin /> : <></>; return loading ? <Spin /> : <></>;
}; });
RecordFeeScope.displayName = 'RecordFeeScope'; RecordFeeScope.displayName = 'RecordFeeScope';
RecordFeeScope[KEY_CUSTOM_COMPONENT_TYPE] = CUSTOM_COMPONENT_TYPE_ASSOCIATED_FIELD; 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 inContractPlanId = form.getValuesIn('in_contract_plan')?.id;
const outContractPlanId = form.getValuesIn('out_contract_plan')?.id; const outContractPlanId = form.getValuesIn('out_contract_plan')?.id;
console.log('产品范围');
let required = { price: false, contract: false, inContract: false, outContract: false }; let required = { price: false, contract: false, inContract: false, outContract: false };
const { data, loading } = useCachedRequest<any>({ const { data, loading } = useCachedRequest<any>({
resource: 'product_category', resource: 'product_category',
@ -71,24 +70,24 @@ export const RecordProductScope = observer(() => {
default: default:
break; 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(() => { 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({ form.setValues({
product_scope: result, product_scope: result,
}); });
}, [required, data, priceProducts, contractProducts, inContractProducts, outContractProducts, form]); }, [result, form]);
return loading || leaseItemsLoading || inLeaseItemsLoading || outLeaseItemsLoading ? <Spin /> : <></>; return loading || leaseItemsLoading || inLeaseItemsLoading || outLeaseItemsLoading ? <Spin /> : <></>;
}); });

View File

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

View File

@ -29,3 +29,38 @@ export const useLeaseItems = (planId) => {
}, [planId]); }, [planId]);
return { data, loading }; 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 };
};