From b3ced4c7e1a8d36ee1304dac2d78c2372bbeb42f Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Tue, 23 Jan 2024 11:09:29 +0800 Subject: [PATCH] fix: block resource --- .../src/application/collection/Collection.tsx | 4 ++++ .../data-block/DataBlockResourceProvider.tsx | 22 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/core/client/src/application/collection/Collection.tsx b/packages/core/client/src/application/collection/Collection.tsx index 4a97300196..1839736e08 100644 --- a/packages/core/client/src/application/collection/Collection.tsx +++ b/packages/core/client/src/application/collection/Collection.tsx @@ -93,6 +93,10 @@ export class CollectionV2 { return this.options?.fields || []; } + get sourceKey() { + return this.options.sourceKey; + } + get name() { return this.options.name; } diff --git a/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx b/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx index 7a5e03a605..22a907fbea 100644 --- a/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx +++ b/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx @@ -3,26 +3,40 @@ import { IResource } from '@nocobase/sdk'; import { useAPIClient } from '../../api-client'; import { useDataBlockPropsV2 } from './DataBlockProvider'; -import { DEFAULT_DATA_SOURCE_NAME } from '../collection'; +import { DEFAULT_DATA_SOURCE_NAME, useCollectionManagerV2 } from '../collection'; export const DataBlockResourceContextV2 = createContext(null); DataBlockResourceContextV2.displayName = 'DataBlockResourceContextV2'; export const DataBlockResourceProviderV2: FC<{ children?: ReactNode }> = ({ children }) => { const dataBlockProps = useDataBlockPropsV2(); - const { association, collection, dataSource, sourceId } = dataBlockProps; + const cm = useCollectionManagerV2(); + const { association, collection, dataSource, sourceId, parentRecord } = dataBlockProps; const api = useAPIClient(); const headers = useMemo(() => { if (dataSource && dataSource !== DEFAULT_DATA_SOURCE_NAME) { return { 'x-connection': dataSource }; } }, [dataSource]); + + const sourceIdValue = useMemo(() => { + if (sourceId) { + return sourceId; + } + if (association && parentRecord) { + const associationCollection = cm.getCollection(association); + if (associationCollection) { + return parentRecord.data[associationCollection.sourceKey || 'id']; + } + } + }, [sourceId, parentRecord]); + const resource = useMemo(() => { if (association) { - return api.resource(association, sourceId, headers); + return api.resource(association, sourceIdValue, headers); } return api.resource(collection, undefined, headers); - }, [api, association, collection, sourceId, headers]); + }, [api, association, collection, sourceIdValue, headers]); return {children}; };