fix(filter-block): fix association fields not being filtered (#1758)

This commit is contained in:
black 2023-04-26 10:43:48 +08:00 committed by GitHub
parent 95e4e7e7b0
commit 00b3c8c257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 64 additions and 56 deletions

View File

@ -24,20 +24,13 @@ export function useRequest<P>(
// 缓存用途 // 缓存用途
const [state, setState] = useSetState({}); const [state, setState] = useSetState({});
const api = useContext(APIClientContext); const api = useContext(APIClientContext);
let tempOptions,tempService
if (typeof service === 'function') { if (typeof service === 'function') {
const result = useReq(service, { tempService = service
...options, }else {
onSuccess(...args) { tempService = async (params = {}) => {
options.onSuccess?.(...args);
if (options.uid) {
api.services[options.uid] = result;
}
},
});
return { ...result, state, setState };
}
const result = useReq(
async (params = {}) => {
const { resource } = service as ResourceActionOptions; const { resource } = service as ResourceActionOptions;
let args = cloneDeep(service); let args = cloneDeep(service);
if (resource) { if (resource) {
@ -48,16 +41,21 @@ export function useRequest<P>(
} }
const response = await api.request(args); const response = await api.request(args);
return response?.data; return response?.data;
}, }
{ }
tempOptions = {
...options, ...options,
onSuccess(...args) { onSuccess(...args) {
options.onSuccess?.(...args); // @ts-ignore
options.onSuccess?.(...args)
if (options.uid) { if (options.uid) {
api.services[options.uid] = result; api.services[options.uid] = result;
} }
}, },
}, }
);
return { ...result, state, setState }; const result:any = useReq(tempService, tempOptions)
return { ...result, state, setState }
} }

View File

@ -4,7 +4,7 @@ import parse from 'json-templates';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
import get from 'lodash/get'; import get from 'lodash/get';
import omit from 'lodash/omit'; import omit from 'lodash/omit';
import { ChangeEvent, useContext } from 'react'; import { ChangeEvent, useContext, useEffect } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { useReactToPrint } from 'react-to-print'; import { useReactToPrint } from 'react-to-print';
@ -891,9 +891,38 @@ export const useAssociationFilterBlockProps = () => {
const optionalFieldList = useOptionalFieldList(); const optionalFieldList = useOptionalFieldList();
const { getDataBlocks } = useFilterBlock(); const { getDataBlocks } = useFilterBlock();
const collectionFieldName = collectionField.name; const collectionFieldName = collectionField.name;
const field = useField();
let list, onSelected, handleSearchInput, params, run, data, valueKey, labelKey, filterKey; let list, onSelected, handleSearchInput, params, run, data, valueKey, labelKey, filterKey;
valueKey = collectionField?.targetKey || 'id';
labelKey = fieldSchema['x-component-props']?.fieldNames?.label || valueKey;
({ data, params, run } = useRequest(
{
resource: collectionField?.target,
action: 'list',
params: {
fields: [labelKey, valueKey],
pageSize: 200,
page: 1,
...field.componentProps?.params
},
},
{
// 由于 选项字段不需要触发当前请求,所以当前请求更改为手动触发
manual: true,
debounceWait: 300,
},
));
useEffect(() => {
// 由于 选项字段不需要触发当前请求,所以请求单独在 关系字段的时候触发
if (!isOptionalField(fieldSchema)) {
run()
}
},[labelKey, valueKey, JSON.stringify(field.componentProps?.params || {}), isOptionalField(fieldSchema)])
if (isOptionalField(fieldSchema)) { if (isOptionalField(fieldSchema)) {
const field = optionalFieldList.find((field) => field.name === fieldSchema.name); const field = optionalFieldList.find((field) => field.name === fieldSchema.name);
const operatorMap = { const operatorMap = {
@ -919,27 +948,8 @@ export const useAssociationFilterBlockProps = () => {
list = (_list as any[]).filter((item) => item.label.includes(value)); list = (_list as any[]).filter((item) => item.label.includes(value));
}; };
} else { } else {
valueKey = collectionField?.targetKey || 'id';
labelKey = fieldSchema['x-component-props']?.fieldNames?.label || valueKey;
({ data, params, run } = useRequest(
{
resource: collectionField.target,
action: 'list',
params: {
fields: [labelKey, valueKey],
pageSize: 200,
page: 1,
},
},
{
refreshDeps: [labelKey, valueKey],
debounceWait: 300,
},
));
filterKey = `${collectionFieldName}.${valueKey}.$in`; filterKey = `${collectionFieldName}.${valueKey}.$in`;
list = data?.data || []; list = data?.data || [];
handleSearchInput = (e: ChangeEvent<any>) => { handleSearchInput = (e: ChangeEvent<any>) => {
run({ run({
...params?.[0], ...params?.[0],

View File

@ -210,7 +210,7 @@ export default {
"Association fields filter": "Association fields filter", "Association fields filter": "Association fields filter",
"PK & FK fields": "PK & FK fields", "PK & FK fields": "PK & FK fields",
"Association fields": "Association fields", "Association fields": "Association fields",
"Optional fields": "Optional fields", "Choices fields": "Choices fields",
"System fields": "System fields", "System fields": "System fields",
"General fields": "General fields", "General fields": "General fields",
"Inherited fields": "Inherited fields", "Inherited fields": "Inherited fields",

View File

@ -577,7 +577,7 @@ export default {
"Enable SMS authentication": "SMS認証を有効にする", "Enable SMS authentication": "SMS認証を有効にする",
"Display association fields": "関連付けられたコレクションのフィールドを表示", "Display association fields": "関連付けられたコレクションのフィールドを表示",
"Set default value": "デフォルト値を設定", "Set default value": "デフォルト値を設定",
"Optional fields": "オプションフィールド", "Choices fields": "チョイスフィールド",
"Editable": "編集可能", "Editable": "編集可能",
"Readonly": "読み取り専用(編集不可)", "Readonly": "読み取り専用(編集不可)",
"Easy-reading": "読取り専用(読取りモード)", "Easy-reading": "読取り専用(読取りモード)",

View File

@ -170,7 +170,7 @@ export default {
"Association fields filter": "Filtro de campos de associação", "Association fields filter": "Filtro de campos de associação",
"PK & FK fields": "Campos PK & FK", "PK & FK fields": "Campos PK & FK",
"Association fields": "Campos de associação", "Association fields": "Campos de associação",
"Optional fields": "Campos opcionais", "Choices fields": "Campos de opções",
"System fields": "Campos do sistema", "System fields": "Campos do sistema",
"General fields": "Campos gerais", "General fields": "Campos gerais",
"Inherited fields": "Campos herdados", "Inherited fields": "Campos herdados",

View File

@ -224,7 +224,7 @@ export default {
"Association fields filter": "关系筛选", "Association fields filter": "关系筛选",
"PK & FK fields": "主外键字段", "PK & FK fields": "主外键字段",
"Association fields": "关系字段", "Association fields": "关系字段",
"Optional fields": "可选字段", "Choices fields": "选项字段",
"System fields": "系统字段", "System fields": "系统字段",
"General fields": "普通字段", "General fields": "普通字段",
"Inherited fields": "继承字段", "Inherited fields": "继承字段",

View File

@ -58,10 +58,10 @@ export const AssociationFilterFilterBlockInitializer = () => {
children, children,
}; };
// 选项字段 // 选项字段
const optionalFieldGroup: SchemaInitializerItemOptions = { const optionalFieldGroup: SchemaInitializerItemOptions = {
type: 'itemGroup', type: 'itemGroup',
title: t('Optional fields'), title: t('Choices fields'),
children: optionalChildren, children: optionalChildren,
}; };

View File

@ -161,7 +161,7 @@
"Association fields filter": "Association fields filter", "Association fields filter": "Association fields filter",
"PK & FK fields": "PK & FK fields", "PK & FK fields": "PK & FK fields",
"Association fields": "Association fields", "Association fields": "Association fields",
"Optional fields": "Optional fields", "Choices fields": "Choices fields",
"System fields": "System fields", "System fields": "System fields",
"General fields": "General fields", "General fields": "General fields",
"Inherited fields": "Inherited fields", "Inherited fields": "Inherited fields",

View File

@ -146,7 +146,7 @@
"Association fields filter": "Filtro de campos de associação", "Association fields filter": "Filtro de campos de associação",
"PK & FK fields": "Campos PK & FK", "PK & FK fields": "Campos PK & FK",
"Association fields": "Campos de associação", "Association fields": "Campos de associação",
"Optional fields": "Campos opcionais", "Choices fields": "Campos de opções",
"System fields": "Campos do sistema", "System fields": "Campos do sistema",
"General fields": "Campos gerais", "General fields": "Campos gerais",
"Inherited fields": "Campos herdados", "Inherited fields": "Campos herdados",

View File

@ -161,7 +161,7 @@
"Association fields filter": "关系筛选", "Association fields filter": "关系筛选",
"PK & FK fields": "主外键字段", "PK & FK fields": "主外键字段",
"Association fields": "关系字段", "Association fields": "关系字段",
"Optional fields": "可选字段", "Choices fields": "选项字段",
"System fields": "系统字段", "System fields": "系统字段",
"General fields": "普通字段", "General fields": "普通字段",
"Inherited fields": "继承字段", "Inherited fields": "继承字段",

View File

@ -6,7 +6,7 @@ export default {
'Create Collection': 'Create Collection', 'Create Collection': 'Create Collection',
'All Fields': 'All Fields', 'All Fields': 'All Fields',
'Association Fields': 'Association Fields', 'Association Fields': 'Association Fields',
'Optional fields': 'Optional fields', 'Choices fields': 'Choices fields',
'All relationships': 'All relationships', 'All relationships': 'All relationships',
'Entity relationship only': 'Entity relationship only', 'Entity relationship only': 'Entity relationship only',
'Inheritance relationship only': 'Inheritance relationship only', 'Inheritance relationship only': 'Inheritance relationship only',

View File

@ -6,7 +6,7 @@ export default {
'Create Collection': 'Criar Coleção', 'Create Collection': 'Criar Coleção',
'All Fields': 'Todos os Campos', 'All Fields': 'Todos os Campos',
'Association Fields': 'Campos de Associação', 'Association Fields': 'Campos de Associação',
'Optional fields': 'Campos Opcionais', 'Choices fields': 'Campos de opções',
'All relationships': 'Todos os relacionamentos', 'All relationships': 'Todos os relacionamentos',
'Entity relationship only': 'Somente relação de Entidade', 'Entity relationship only': 'Somente relação de Entidade',
'Inheritance relationship only': 'Somente relação de Herança', 'Inheritance relationship only': 'Somente relação de Herança',

View File

@ -6,7 +6,7 @@ export default {
'Create Collection': '创建数据表', 'Create Collection': '创建数据表',
'All Fields': '全部', 'All Fields': '全部',
'Association Fields': '关系字段', 'Association Fields': '关系字段',
'Optional fields': '可选字段', 'Choices fields': '选项字段',
'All relationships': '所有关系', 'All relationships': '所有关系',
'Entity relationship only': '实体关系', 'Entity relationship only': '实体关系',
'Inheritance relationship only': '继承关系', 'Inheritance relationship only': '继承关系',