From 00b3c8c257f2c77c25bba51ff6e5daf6e4b54a86 Mon Sep 17 00:00:00 2001 From: black Date: Wed, 26 Apr 2023 10:43:48 +0800 Subject: [PATCH] fix(filter-block): fix association fields not being filtered (#1758) --- .../client/src/api-client/hooks/useRequest.ts | 46 ++++++++--------- .../client/src/block-provider/hooks/index.ts | 50 +++++++++++-------- packages/core/client/src/locale/en_US.ts | 2 +- packages/core/client/src/locale/ja_JP.ts | 2 +- packages/core/client/src/locale/pt_BR.ts | 2 +- packages/core/client/src/locale/zh_CN.ts | 2 +- ...sociationFilter.FilterBlockInitializer.tsx | 4 +- packages/plugins/client/src/locale/en-US.json | 2 +- packages/plugins/client/src/locale/pt-BR.json | 2 +- packages/plugins/client/src/locale/zh-CN.json | 2 +- .../src/client/locale/en-US.ts | 2 +- .../src/client/locale/pt-BR.ts | 2 +- .../src/client/locale/zh-CN.ts | 2 +- 13 files changed, 64 insertions(+), 56 deletions(-) diff --git a/packages/core/client/src/api-client/hooks/useRequest.ts b/packages/core/client/src/api-client/hooks/useRequest.ts index 93c3c5f0d..fdb2af604 100644 --- a/packages/core/client/src/api-client/hooks/useRequest.ts +++ b/packages/core/client/src/api-client/hooks/useRequest.ts @@ -24,20 +24,13 @@ export function useRequest

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

( } const response = await api.request(args); return response?.data; + } + } + + tempOptions = { + ...options, + onSuccess(...args) { + // @ts-ignore + options.onSuccess?.(...args) + if (options.uid) { + api.services[options.uid] = result; + } }, - { - ...options, - onSuccess(...args) { - options.onSuccess?.(...args); - if (options.uid) { - api.services[options.uid] = result; - } - }, - }, - ); - return { ...result, state, setState }; + } + + const result:any = useReq(tempService, tempOptions) + + return { ...result, state, setState } } diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 7f2c78c11..839770eaa 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -4,7 +4,7 @@ import parse from 'json-templates'; import { cloneDeep } from 'lodash'; import get from 'lodash/get'; import omit from 'lodash/omit'; -import { ChangeEvent, useContext } from 'react'; +import { ChangeEvent, useContext, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router-dom'; import { useReactToPrint } from 'react-to-print'; @@ -891,9 +891,38 @@ export const useAssociationFilterBlockProps = () => { const optionalFieldList = useOptionalFieldList(); const { getDataBlocks } = useFilterBlock(); const collectionFieldName = collectionField.name; + const field = useField(); 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)) { const field = optionalFieldList.find((field) => field.name === fieldSchema.name); const operatorMap = { @@ -919,27 +948,8 @@ export const useAssociationFilterBlockProps = () => { list = (_list as any[]).filter((item) => item.label.includes(value)); }; } 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`; - list = data?.data || []; - handleSearchInput = (e: ChangeEvent) => { run({ ...params?.[0], diff --git a/packages/core/client/src/locale/en_US.ts b/packages/core/client/src/locale/en_US.ts index 5e67b9037..04afd2004 100644 --- a/packages/core/client/src/locale/en_US.ts +++ b/packages/core/client/src/locale/en_US.ts @@ -210,7 +210,7 @@ export default { "Association fields filter": "Association fields filter", "PK & FK fields": "PK & FK fields", "Association fields": "Association fields", - "Optional fields": "Optional fields", + "Choices fields": "Choices fields", "System fields": "System fields", "General fields": "General fields", "Inherited fields": "Inherited fields", diff --git a/packages/core/client/src/locale/ja_JP.ts b/packages/core/client/src/locale/ja_JP.ts index 4feea6761..ae0ce03cf 100644 --- a/packages/core/client/src/locale/ja_JP.ts +++ b/packages/core/client/src/locale/ja_JP.ts @@ -577,7 +577,7 @@ export default { "Enable SMS authentication": "SMS認証を有効にする", "Display association fields": "関連付けられたコレクションのフィールドを表示", "Set default value": "デフォルト値を設定", - "Optional fields": "オプションフィールド", + "Choices fields": "チョイスフィールド", "Editable": "編集可能", "Readonly": "読み取り専用(編集不可)", "Easy-reading": "読取り専用(読取りモード)", diff --git a/packages/core/client/src/locale/pt_BR.ts b/packages/core/client/src/locale/pt_BR.ts index 25640f5d8..690572cbe 100644 --- a/packages/core/client/src/locale/pt_BR.ts +++ b/packages/core/client/src/locale/pt_BR.ts @@ -170,7 +170,7 @@ export default { "Association fields filter": "Filtro de campos de associação", "PK & FK fields": "Campos PK & FK", "Association fields": "Campos de associação", - "Optional fields": "Campos opcionais", + "Choices fields": "Campos de opções", "System fields": "Campos do sistema", "General fields": "Campos gerais", "Inherited fields": "Campos herdados", diff --git a/packages/core/client/src/locale/zh_CN.ts b/packages/core/client/src/locale/zh_CN.ts index a0e93e003..d259b8ac8 100644 --- a/packages/core/client/src/locale/zh_CN.ts +++ b/packages/core/client/src/locale/zh_CN.ts @@ -224,7 +224,7 @@ export default { "Association fields filter": "关系筛选", "PK & FK fields": "主外键字段", "Association fields": "关系字段", - "Optional fields": "可选字段", + "Choices fields": "选项字段", "System fields": "系统字段", "General fields": "普通字段", "Inherited fields": "继承字段", diff --git a/packages/core/client/src/schema-component/antd/association-filter/AssociationFilter.FilterBlockInitializer.tsx b/packages/core/client/src/schema-component/antd/association-filter/AssociationFilter.FilterBlockInitializer.tsx index 7dc4a4242..66254d749 100644 --- a/packages/core/client/src/schema-component/antd/association-filter/AssociationFilter.FilterBlockInitializer.tsx +++ b/packages/core/client/src/schema-component/antd/association-filter/AssociationFilter.FilterBlockInitializer.tsx @@ -58,10 +58,10 @@ export const AssociationFilterFilterBlockInitializer = () => { children, }; - // 可选项字段 + // 选项字段 const optionalFieldGroup: SchemaInitializerItemOptions = { type: 'itemGroup', - title: t('Optional fields'), + title: t('Choices fields'), children: optionalChildren, }; diff --git a/packages/plugins/client/src/locale/en-US.json b/packages/plugins/client/src/locale/en-US.json index c498f84fb..4b2cba83c 100644 --- a/packages/plugins/client/src/locale/en-US.json +++ b/packages/plugins/client/src/locale/en-US.json @@ -161,7 +161,7 @@ "Association fields filter": "Association fields filter", "PK & FK fields": "PK & FK fields", "Association fields": "Association fields", - "Optional fields": "Optional fields", + "Choices fields": "Choices fields", "System fields": "System fields", "General fields": "General fields", "Inherited fields": "Inherited fields", diff --git a/packages/plugins/client/src/locale/pt-BR.json b/packages/plugins/client/src/locale/pt-BR.json index cb88534bb..6175a4bb7 100644 --- a/packages/plugins/client/src/locale/pt-BR.json +++ b/packages/plugins/client/src/locale/pt-BR.json @@ -146,7 +146,7 @@ "Association fields filter": "Filtro de campos de associação", "PK & FK fields": "Campos PK & FK", "Association fields": "Campos de associação", - "Optional fields": "Campos opcionais", + "Choices fields": "Campos de opções", "System fields": "Campos do sistema", "General fields": "Campos gerais", "Inherited fields": "Campos herdados", diff --git a/packages/plugins/client/src/locale/zh-CN.json b/packages/plugins/client/src/locale/zh-CN.json index 0f8e08c27..d4ddcb9ef 100644 --- a/packages/plugins/client/src/locale/zh-CN.json +++ b/packages/plugins/client/src/locale/zh-CN.json @@ -161,7 +161,7 @@ "Association fields filter": "关系筛选", "PK & FK fields": "主外键字段", "Association fields": "关系字段", - "Optional fields": "可选字段", + "Choices fields": "选项字段", "System fields": "系统字段", "General fields": "普通字段", "Inherited fields": "继承字段", diff --git a/packages/plugins/graph-collection-manager/src/client/locale/en-US.ts b/packages/plugins/graph-collection-manager/src/client/locale/en-US.ts index f3c5aee81..3ae29bdc1 100644 --- a/packages/plugins/graph-collection-manager/src/client/locale/en-US.ts +++ b/packages/plugins/graph-collection-manager/src/client/locale/en-US.ts @@ -6,7 +6,7 @@ export default { 'Create Collection': 'Create Collection', 'All Fields': 'All Fields', 'Association Fields': 'Association Fields', - 'Optional fields': 'Optional fields', + 'Choices fields': 'Choices fields', 'All relationships': 'All relationships', 'Entity relationship only': 'Entity relationship only', 'Inheritance relationship only': 'Inheritance relationship only', diff --git a/packages/plugins/graph-collection-manager/src/client/locale/pt-BR.ts b/packages/plugins/graph-collection-manager/src/client/locale/pt-BR.ts index 6bf332d4b..d99f5d53a 100644 --- a/packages/plugins/graph-collection-manager/src/client/locale/pt-BR.ts +++ b/packages/plugins/graph-collection-manager/src/client/locale/pt-BR.ts @@ -6,7 +6,7 @@ export default { 'Create Collection': 'Criar Coleção', 'All Fields': 'Todos os Campos', 'Association Fields': 'Campos de Associação', - 'Optional fields': 'Campos Opcionais', + 'Choices fields': 'Campos de opções', 'All relationships': 'Todos os relacionamentos', 'Entity relationship only': 'Somente relação de Entidade', 'Inheritance relationship only': 'Somente relação de Herança', diff --git a/packages/plugins/graph-collection-manager/src/client/locale/zh-CN.ts b/packages/plugins/graph-collection-manager/src/client/locale/zh-CN.ts index fdc27d634..9a5ff24eb 100644 --- a/packages/plugins/graph-collection-manager/src/client/locale/zh-CN.ts +++ b/packages/plugins/graph-collection-manager/src/client/locale/zh-CN.ts @@ -6,7 +6,7 @@ export default { 'Create Collection': '创建数据表', 'All Fields': '全部', 'Association Fields': '关系字段', - 'Optional fields': '可选字段', + 'Choices fields': '选项字段', 'All relationships': '所有关系', 'Entity relationship only': '实体关系', 'Inheritance relationship only': '继承关系',