fix(client/table-selector-provider): make data range config effective (#960)

* fix(client/table-selector-provider): make data range config effective

* fix(client/table-selector): fix when save data range config filter lose
This commit is contained in:
lyf-coder 2022-10-31 11:28:40 +08:00 committed by chenos
parent 9a81b1b8ee
commit b9910cdc1f
2 changed files with 70 additions and 39 deletions

View File

@ -9,7 +9,7 @@ import { useFormBlockContext } from './FormBlockProvider';
export const TableSelectorContext = createContext<any>({});
const InternalTableSelectorProvider = (props) => {
const { params, rowKey } = props;
const { params, rowKey, extraFilter } = props;
const field = useField();
const { resource, service } = useBlockRequestContext();
// if (service.loading) {
@ -22,6 +22,7 @@ const InternalTableSelectorProvider = (props) => {
service,
resource,
params,
extraFilter,
rowKey,
}}
>
@ -39,21 +40,23 @@ const useAssociationNames = (collection) => {
};
const recursiveParent = (schema: Schema, component) => {
return schema['x-component'] === component
? schema
: (schema.parent ? recursiveParent(schema.parent, component) : null);
}
return schema['x-component'] === component
? schema
: schema.parent
? recursiveParent(schema.parent, component)
: null;
};
export const TableSelectorProvider = (props) => {
const fieldSchema = useFieldSchema();
const ctx = useFormBlockContext()
const ctx = useFormBlockContext();
const { getCollectionJoinField, getCollectionFields } = useCollectionManager();
const record = useRecord();
const collectionFieldSchema = recursiveParent(fieldSchema, 'CollectionField');
// const value = ctx.form.query(collectionFieldSchema?.name).value();
const collectionField = getCollectionJoinField(collectionFieldSchema?.['x-collection-field']);
console.log('TableSelectorProvider', collectionFieldSchema, collectionField, record);
const params = { ...props.params };
const appends = useAssociationNames(props.collection);
@ -63,57 +66,74 @@ export const TableSelectorProvider = (props) => {
if (!Object.keys(params).includes('appends')) {
params['appends'] = appends;
}
let extraFilter;
if (collectionField) {
if (['oho', 'o2m'].includes(collectionField.interface)) {
if (record?.[collectionField.sourceKey]) {
params['filter'] = {
$or: [{
[collectionField.foreignKey]: {
$is: null,
}
}, {
[collectionField.foreignKey]: {
$eq: record?.[collectionField.sourceKey],
}
}]
}
extraFilter = {
$or: [
{
[collectionField.foreignKey]: {
$is: null,
},
},
{
[collectionField.foreignKey]: {
$eq: record?.[collectionField.sourceKey],
},
},
],
};
} else {
params['filter'] = {
extraFilter = {
[collectionField.foreignKey]: {
$is: null,
}
}
},
};
}
}
if (['obo'].includes(collectionField.interface)) {
const fields = getCollectionFields(collectionField.target);
const targetField = fields.find(f => f.foreignKey && f.foreignKey === collectionField.foreignKey);
const targetField = fields.find((f) => f.foreignKey && f.foreignKey === collectionField.foreignKey);
if (targetField) {
if (record?.[collectionField.foreignKey]) {
params['filter'] = {
$or: [{
[`${targetField.name}.${targetField.foreignKey}`]: {
$is: null,
}
}, {
[`${targetField.name}.${targetField.foreignKey}`]: {
$eq: record?.[collectionField.foreignKey],
}
}]
}
extraFilter = {
$or: [
{
[`${targetField.name}.${targetField.foreignKey}`]: {
$is: null,
},
},
{
[`${targetField.name}.${targetField.foreignKey}`]: {
$eq: record?.[collectionField.foreignKey],
},
},
],
};
} else {
params['filter'] = {
extraFilter = {
[`${targetField.name}.${targetField.foreignKey}`]: {
$is: null,
}
}
},
};
}
}
}
}
if (extraFilter) {
if (params?.filter) {
params['filter'] = {
$and: [extraFilter, params['filter']],
};
} else {
params['filter'] = extraFilter;
}
}
return (
<BlockProvider {...props} params={params}>
<InternalTableSelectorProvider {...props} params={params} />
<InternalTableSelectorProvider {...props} params={params} extraFilter={extraFilter} />
</BlockProvider>
);
};

View File

@ -8,6 +8,7 @@ import { useCollectionFilterOptions, useSortFields } from '../../../collection-m
import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings';
import { useSchemaTemplate } from '../../../schema-templates';
import { useDesignable } from '../../hooks';
import { cloneDeep } from 'lodash';
export const TableSelectorDesigner = () => {
const { name, title } = useCollection();
@ -15,7 +16,7 @@ export const TableSelectorDesigner = () => {
const fieldSchema = useFieldSchema();
const dataSource = useCollectionFilterOptions(name);
const sortFields = useSortFields(name);
const { service } = useTableSelectorContext();
const { service, extraFilter } = useTableSelectorContext();
const { t } = useTranslation();
const { dn } = useDesignable();
const defaultFilter = fieldSchema?.['x-decorator-props']?.params?.filter || {};
@ -57,7 +58,17 @@ export const TableSelectorDesigner = () => {
params.filter = filter;
field.decoratorProps.params = params;
fieldSchema['x-decorator-props']['params'] = params;
service.run({ ...service.params?.[0], filter, page: 1 });
let serviceFilter = cloneDeep(filter);
if (extraFilter) {
if (serviceFilter) {
serviceFilter = {
$and: [extraFilter, serviceFilter],
};
} else {
serviceFilter = extraFilter;
}
}
service.run({ ...service.params?.[0], filter: serviceFilter, page: 1 });
dn.emit('patch', {
schema: {
['x-uid']: fieldSchema['x-uid'],