From e7e7a28cfe97b8aae7eb73439c772186bee51436 Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Mon, 25 Dec 2023 16:11:51 +0800 Subject: [PATCH] fix: bug --- .../core/collection/collection-manager.md | 8 +++--- .../collection/AssociationProvider.tsx | 25 +++++++++++++++++-- .../collection/CollectionManager.tsx | 13 ++++++---- .../collection/CollectionManagerProvider.tsx | 14 +++++++++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/packages/core/client/docs/zh-CN/core/collection/collection-manager.md b/packages/core/client/docs/zh-CN/core/collection/collection-manager.md index b4e07154e3..738c4fbbc1 100644 --- a/packages/core/client/docs/zh-CN/core/collection/collection-manager.md +++ b/packages/core/client/docs/zh-CN/core/collection/collection-manager.md @@ -34,8 +34,8 @@ class CollectionManagerV2 { getCollections(ns?: string, predicate?: (collection: CollectionV2) => boolean): CollectionV2[] async getCollection(path: string, options?: GetCollectionOptions): Promise async getCollectionName(path: string, options?: GetCollectionOptions): Promise; - removeCollection(path: string, options?: GetCollectionOptions): void; - getCollectionField(path: string, options?: GetCollectionOptions): CollectionFieldOptions | undefined; + async removeCollection(path: string, options?: GetCollectionOptions): void; + async getCollectionField(path: string, options?: GetCollectionOptions): Promise; addCollectionFieldInterfaces(interfaces:CollectionFieldInterfaceV2[] | CollectionFieldInterfaceOptions[]): void; getCollectionFieldInterfaces(): CollectionFieldInterfaceV2[] @@ -461,7 +461,7 @@ collectionManager.getCollectionName('users.profileId'); // 'profiles' ```tsx | pure class CollectionManagerV2 { - removeCollection(path: string, options?: GetCollectionOptions): void; + async removeCollection(path: string, options?: GetCollectionOptions): void; } ``` @@ -479,7 +479,7 @@ collectionManager.removeCollection('users'); ```tsx | pure class CollectionManagerV2 { - getCollectionField(path: string, options?: GetCollectionOptions): CollectionFieldOptions | undefined; + async getCollectionField(path: string, options?: GetCollectionOptions): CollectionFieldOptions | undefined; } ``` diff --git a/packages/core/client/src/application/collection/AssociationProvider.tsx b/packages/core/client/src/application/collection/AssociationProvider.tsx index 284fb6b6ed..e83d65ce5a 100644 --- a/packages/core/client/src/application/collection/AssociationProvider.tsx +++ b/packages/core/client/src/application/collection/AssociationProvider.tsx @@ -1,8 +1,10 @@ -import React, { FC, ReactNode } from 'react'; +import React, { FC, ReactNode, useEffect, useState } from 'react'; import { CollectionProviderV2 } from './CollectionProvider'; import { CollectionFieldProviderV2 } from './CollectionFieldProvider'; import { useCollectionManagerV2 } from './CollectionManagerProvider'; +import { Spin } from 'antd'; +import { CollectionV2 } from './Collection'; export interface AssociationProviderProps { ns?: string; @@ -14,11 +16,30 @@ export const AssociationProviderV2: FC = (props) => { const { name, ns, children } = props; const collectionManager = useCollectionManagerV2(); + const [loading, setLoading] = useState(false); + const [collectionName, setCollectionName] = useState(); + + useEffect(() => { + const load = async () => { + setLoading(true); + try { + const res = await collectionManager.getCollectionName(name, { ns }); + setCollectionName(res); + setLoading(false); + } catch (error) { + console.error(error); + setLoading(false); + } + }; + load(); + }, [collectionManager, name, ns]); + + if (loading) return ; return ( - + {children} diff --git a/packages/core/client/src/application/collection/CollectionManager.tsx b/packages/core/client/src/application/collection/CollectionManager.tsx index dae866de6b..ae1a66dc43 100644 --- a/packages/core/client/src/application/collection/CollectionManager.tsx +++ b/packages/core/client/src/application/collection/CollectionManager.tsx @@ -99,7 +99,7 @@ export class CollectionManagerV2 { this.checkNamespace(ns); if (path.split('.').length > 1) { // 获取到关联字段 - const associationField = this.getCollectionField(path); + const associationField = await this.getCollectionField(path); return this.getCollection(associationField.target, { ns }); } @@ -109,12 +109,12 @@ export class CollectionManagerV2 { const res = await this.getCollection(path, options); return res?.name; } - removeCollection(path: string, options: GetCollectionOptions = {}) { + async removeCollection(path: string, options: GetCollectionOptions = {}) { const { ns = DEFAULT_COLLECTION_NAMESPACE_NAME } = options; this.checkNamespace(ns); if (path.split('.').length > 1) { // 获取到关联字段 - const associationField = this.getCollectionField(path); + const associationField = await this.getCollectionField(path); return this.removeCollection(associationField.target, { ns }); } @@ -127,7 +127,10 @@ export class CollectionManagerV2 { * getCollection('users.username'); // 获取 users 表的 username 字段 * getCollection('a.b.c'); // 获取 a 表的 b 字段的关联表,然后 b.target 表对应的 c 字段 */ - getCollectionField(path: string, options: GetCollectionOptions = {}): CollectionFieldOptions | undefined { + async getCollectionField( + path: string, + options: GetCollectionOptions = {}, + ): Promise { const arr = path.split('.'); if (arr.length < 2) { throw new Error(`[@nocobase/client]: CollectionManager.getCollectionField() path "${path}" is invalid`); @@ -135,7 +138,7 @@ export class CollectionManagerV2 { const [collectionName, fieldName, ...otherFieldNames] = path.split('.'); const { ns = DEFAULT_COLLECTION_NAMESPACE_NAME } = options || {}; this.checkNamespace(ns); - const collection = this.getCollection(collectionName, { ns }); + const collection = await this.getCollection(collectionName, { ns }); if (!collection) { return; } diff --git a/packages/core/client/src/application/collection/CollectionManagerProvider.tsx b/packages/core/client/src/application/collection/CollectionManagerProvider.tsx index 3378bd62ad..c8faa2eb4a 100644 --- a/packages/core/client/src/application/collection/CollectionManagerProvider.tsx +++ b/packages/core/client/src/application/collection/CollectionManagerProvider.tsx @@ -1,6 +1,7 @@ -import React, { FC, ReactNode, createContext, useContext, useMemo } from 'react'; +import React, { FC, ReactNode, createContext, useContext, useEffect, useMemo, useState } from 'react'; import type { CollectionManagerV2, GetCollectionOptions } from './CollectionManager'; import type { CollectionV2 } from './Collection'; +import { CollectionFieldOptions } from '../../collection-manager'; export const CollectionManagerContextV2 = createContext(null); CollectionManagerContextV2.displayName = 'CollectionManagerContextV2'; @@ -32,6 +33,15 @@ export const useCollectionsV2 = (ns?: string, predicate?: (collection: Collectio export const useCollectionFieldByPathV2 = (path: string, options?: GetCollectionOptions) => { const collectionManager = useCollectionManagerV2(); - const field = useMemo(() => collectionManager.getCollectionField(path, options), [collectionManager, path, options]); + const [field, setField] = useState(); + useEffect(() => { + async function load() { + const field = await collectionManager.getCollectionField(path, options); + setField(field); + } + + load(); + }, [collectionManager, path, options]); + return field; };