mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 01:45:53 +00:00
fix: block resource
This commit is contained in:
parent
819b38fe28
commit
b3ced4c7e1
@ -93,6 +93,10 @@ export class CollectionV2 {
|
||||
return this.options?.fields || [];
|
||||
}
|
||||
|
||||
get sourceKey() {
|
||||
return this.options.sourceKey;
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this.options.name;
|
||||
}
|
||||
|
@ -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<IResource>(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 <DataBlockResourceContextV2.Provider value={resource}>{children}</DataBlockResourceContextV2.Provider>;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user