mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 23:26:38 +00:00
fix: data-block dataSource
This commit is contained in:
parent
3da6bdc659
commit
819b38fe28
@ -10,8 +10,7 @@ import { useCollection, useCollectionManager } from '../collection-manager';
|
||||
import { useResourceActionContext } from '../collection-manager/ResourceActionProvider';
|
||||
import { useRecord } from '../record-provider';
|
||||
import { SchemaComponentOptions, useDesignable } from '../schema-component';
|
||||
import { useApp } from '../application';
|
||||
import { useDataSourceName } from '../block-provider/BlockProvider';
|
||||
import { useApp, useCollectionDataSourceName } from '../application';
|
||||
|
||||
export const ACLContext = createContext<any>({});
|
||||
|
||||
@ -93,7 +92,7 @@ export const ACLActionParamsContext = createContext<any>({});
|
||||
|
||||
export const useACLRolesCheck = () => {
|
||||
const ctx = useContext(ACLContext);
|
||||
const dataSourceName = useDataSourceName();
|
||||
const dataSourceName = useCollectionDataSourceName();
|
||||
const { dataSources: dataSourcesAcl } = ctx?.data?.meta || {};
|
||||
const data = { ...ctx?.data?.data, ...omit(dataSourcesAcl?.[dataSourceName], 'snippets') };
|
||||
const getActionAlias = (actionPath: string) => {
|
||||
|
@ -60,7 +60,7 @@ interface DataSource {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
type DataSourceName = string;
|
||||
type DataSourceNameType = string;
|
||||
|
||||
export interface CollectionManagerOptionsV2 {
|
||||
collections?: CollectionOptionsV2[];
|
||||
@ -76,11 +76,11 @@ type ReloadCallback = (collections: CollectionOptionsV2[]) => void;
|
||||
|
||||
export class CollectionManagerV2 {
|
||||
public app: Application;
|
||||
protected collections: Record<string, Record<DataSourceName, CollectionV2>> = {};
|
||||
protected collections: Record<string, Record<DataSourceNameType, CollectionV2>> = {};
|
||||
protected collectionTemplateInstances: Record<string, CollectionTemplateBase> = {};
|
||||
protected fieldInterfaceInstances: Record<string, CollectionFieldInterfaceBase> = {};
|
||||
protected collectionMixins: CollectionMixinConstructor[] = [];
|
||||
protected dataSourceMap: Record<DataSourceName, Omit<DataSource, 'collections'>> = {};
|
||||
protected dataSourceMap: Record<DataSourceNameType, Omit<DataSource, 'collections'>> = {};
|
||||
protected collectionFieldGroups: Record<string, { label: string; order?: number }> = {};
|
||||
protected mainDataSourceFn: MainDataSOurceFn;
|
||||
protected thirdDataSourceFn: ThirdDataResourceFn;
|
||||
|
@ -0,0 +1,15 @@
|
||||
import React, { FC, ReactNode, createContext, useContext } from 'react';
|
||||
|
||||
export const CollectionDataSourceName = createContext<string>(undefined);
|
||||
CollectionDataSourceName.displayName = 'CollectionDataSourceName';
|
||||
|
||||
export const CollectionDataSourceProvider: FC<{ dataSource: string; children: ReactNode }> = ({
|
||||
dataSource,
|
||||
children,
|
||||
}) => {
|
||||
return <CollectionDataSourceName.Provider value={dataSource}>{children}</CollectionDataSourceName.Provider>;
|
||||
};
|
||||
|
||||
export const useCollectionDataSourceName = () => {
|
||||
return useContext(CollectionDataSourceName);
|
||||
};
|
@ -6,10 +6,12 @@ import { withDynamicSchemaProps } from '../hoc';
|
||||
import { DataBlockResourceProviderV2 } from './DataBlockResourceProvider';
|
||||
import { AssociationProviderV2, CollectionProviderV2, RecordV2 } from '../collection';
|
||||
import { UseRequestOptions, UseRequestService } from '../../api-client';
|
||||
import { CollectionDataSourceProvider } from './CollectionDataSourceProvider';
|
||||
|
||||
export interface AllDataBlockProps {
|
||||
collection: string;
|
||||
association: string;
|
||||
dataSource?: string;
|
||||
sourceId: string | number;
|
||||
filterByTk: string | number;
|
||||
record: RecordV2;
|
||||
@ -21,26 +23,39 @@ export interface AllDataBlockProps {
|
||||
[index: string]: any;
|
||||
}
|
||||
|
||||
type CollectionCreate = Pick<AllDataBlockProps, 'collection'>;
|
||||
type CollectionCreate = Pick<AllDataBlockProps, 'collection' | 'dataSource'>;
|
||||
|
||||
interface CollectionGet
|
||||
extends Pick<AllDataBlockProps, 'collection' | 'filterByTk' | 'params' | 'requestService' | 'requestOptions'> {
|
||||
extends Pick<
|
||||
AllDataBlockProps,
|
||||
'collection' | 'dataSource' | 'filterByTk' | 'params' | 'requestService' | 'requestOptions'
|
||||
> {
|
||||
action: 'get';
|
||||
}
|
||||
|
||||
interface CollectionList
|
||||
extends Pick<AllDataBlockProps, 'collection' | 'params' | 'requestService' | 'requestOptions'> {
|
||||
extends Pick<AllDataBlockProps, 'collection' | 'dataSource' | 'params' | 'requestService' | 'requestOptions'> {
|
||||
action: 'list';
|
||||
}
|
||||
|
||||
type CollectionRecord = Pick<AllDataBlockProps, 'collection' | 'record' | 'requestService' | 'requestOptions'>;
|
||||
type CollectionRecord = Pick<
|
||||
AllDataBlockProps,
|
||||
'collection' | 'dataSource' | 'record' | 'requestService' | 'requestOptions'
|
||||
>;
|
||||
|
||||
type AssociationCreate = Pick<AllDataBlockProps, 'association' | 'sourceId' | 'parentRecord'>;
|
||||
type AssociationCreate = Pick<AllDataBlockProps, 'association' | 'dataSource' | 'sourceId' | 'parentRecord'>;
|
||||
|
||||
interface AssociationGet
|
||||
extends Pick<
|
||||
AllDataBlockProps,
|
||||
'association' | 'sourceId' | 'parentRecord' | 'filterByTk' | 'params' | 'requestService' | 'requestOptions'
|
||||
| 'association'
|
||||
| 'dataSource'
|
||||
| 'sourceId'
|
||||
| 'parentRecord'
|
||||
| 'filterByTk'
|
||||
| 'params'
|
||||
| 'requestService'
|
||||
| 'requestOptions'
|
||||
> {
|
||||
action: 'get';
|
||||
}
|
||||
@ -48,14 +63,14 @@ interface AssociationGet
|
||||
interface AssociationList
|
||||
extends Pick<
|
||||
AllDataBlockProps,
|
||||
'association' | 'sourceId' | 'parentRecord' | 'params' | 'requestService' | 'requestOptions'
|
||||
'association' | 'dataSource' | 'sourceId' | 'parentRecord' | 'params' | 'requestService' | 'requestOptions'
|
||||
> {
|
||||
action: 'list';
|
||||
}
|
||||
|
||||
type AssociationRecord = Pick<
|
||||
AllDataBlockProps,
|
||||
'association' | 'record' | 'parentRecord' | 'requestService' | 'requestOptions'
|
||||
'association' | 'dataSource' | 'record' | 'parentRecord' | 'requestService' | 'requestOptions'
|
||||
>;
|
||||
|
||||
type AllDataBlockType = {
|
||||
@ -73,7 +88,7 @@ export type DataBlockProviderProps = AllDataBlockType[keyof AllDataBlockType];
|
||||
|
||||
export type UseDataBlockProps<T extends keyof AllDataBlockType> = (
|
||||
props: DataBlockProviderProps & { [index: string]: any },
|
||||
) => Omit<AllDataBlockType[T], 'association' | 'collection' | 'action'> & { [index: string]: any };
|
||||
) => Omit<AllDataBlockType[T], 'association' | 'collection' | 'dataSource' | 'action'> & { [index: string]: any };
|
||||
|
||||
export interface DataBlockContextValue<T extends {} = {}> {
|
||||
props: AllDataBlockProps & T;
|
||||
@ -85,7 +100,7 @@ DataBlockContextV2.displayName = 'DataBlockContextV2';
|
||||
|
||||
export const DataBlockProviderV2: FC<DataBlockProviderProps & { children?: ReactNode }> = withDynamicSchemaProps(
|
||||
(props) => {
|
||||
const { collection, association, children, ...resets } = props as Partial<AllDataBlockProps>;
|
||||
const { collection, association, dataSource, children, ...resets } = props as Partial<AllDataBlockProps>;
|
||||
const AssociationOrCollection = useMemo(() => {
|
||||
if (association) {
|
||||
return {
|
||||
@ -105,14 +120,16 @@ export const DataBlockProviderV2: FC<DataBlockProviderProps & { children?: React
|
||||
<DataBlockContextV2.Provider
|
||||
value={{
|
||||
dn,
|
||||
props: { ...resets, collection, association } as AllDataBlockProps,
|
||||
props: { ...resets, collection, association, dataSource } as AllDataBlockProps,
|
||||
}}
|
||||
>
|
||||
<AssociationOrCollection.Component name={AssociationOrCollection.name}>
|
||||
<DataBlockResourceProviderV2>
|
||||
<BlockRequestProviderV2>{children}</BlockRequestProviderV2>
|
||||
</DataBlockResourceProviderV2>
|
||||
</AssociationOrCollection.Component>
|
||||
<CollectionDataSourceProvider dataSource={dataSource}>
|
||||
<AssociationOrCollection.Component name={AssociationOrCollection.name}>
|
||||
<DataBlockResourceProviderV2>
|
||||
<BlockRequestProviderV2>{children}</BlockRequestProviderV2>
|
||||
</DataBlockResourceProviderV2>
|
||||
</AssociationOrCollection.Component>
|
||||
</CollectionDataSourceProvider>
|
||||
</DataBlockContextV2.Provider>
|
||||
);
|
||||
},
|
||||
|
@ -3,20 +3,26 @@ import { IResource } from '@nocobase/sdk';
|
||||
|
||||
import { useAPIClient } from '../../api-client';
|
||||
import { useDataBlockPropsV2 } from './DataBlockProvider';
|
||||
import { DEFAULT_DATA_SOURCE_NAME } from '../collection';
|
||||
|
||||
export const DataBlockResourceContextV2 = createContext<IResource>(null);
|
||||
DataBlockResourceContextV2.displayName = 'DataBlockResourceContextV2';
|
||||
|
||||
export const DataBlockResourceProviderV2: FC<{ children?: ReactNode }> = ({ children }) => {
|
||||
const dataBlockProps = useDataBlockPropsV2();
|
||||
const { association, collection, sourceId } = dataBlockProps;
|
||||
const { association, collection, dataSource, sourceId } = dataBlockProps;
|
||||
const api = useAPIClient();
|
||||
const headers = useMemo(() => {
|
||||
if (dataSource && dataSource !== DEFAULT_DATA_SOURCE_NAME) {
|
||||
return { 'x-connection': dataSource };
|
||||
}
|
||||
}, [dataSource]);
|
||||
const resource = useMemo(() => {
|
||||
if (association) {
|
||||
return api.resource(association, sourceId);
|
||||
return api.resource(association, sourceId, headers);
|
||||
}
|
||||
return api.resource(collection);
|
||||
}, [api, association, collection, sourceId]);
|
||||
return api.resource(collection, undefined, headers);
|
||||
}, [api, association, collection, sourceId, headers]);
|
||||
return <DataBlockResourceContextV2.Provider value={resource}>{children}</DataBlockResourceContextV2.Provider>;
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from './DataBlockResourceProvider';
|
||||
export * from './DataBlockRequestProvider';
|
||||
export * from './DataBlockProvider';
|
||||
export * from './CollectionDataSourceProvider';
|
||||
|
@ -23,6 +23,7 @@ import { useRecordIndex } from '../record-provider';
|
||||
import { SharedFilterProvider } from './SharedFilterProvider';
|
||||
import { useTemplateBlockContext } from './TemplateBlockProvider';
|
||||
import { useAssociationNames } from './hooks';
|
||||
import { CollectionDataSourceProvider } from '../application/data-block';
|
||||
|
||||
export const BlockResourceContext = createContext(null);
|
||||
export const BlockAssociationContext = createContext(null);
|
||||
@ -303,11 +304,6 @@ export const useBlockContext = () => {
|
||||
return useContext(BlockContext);
|
||||
};
|
||||
|
||||
export const DataSourceName = createContext<string>(undefined);
|
||||
export const useDataSourceName = () => {
|
||||
return useContext(DataSourceName);
|
||||
};
|
||||
|
||||
export const BlockProvider = (props: {
|
||||
name: string;
|
||||
resource: any;
|
||||
@ -331,7 +327,7 @@ export const BlockProvider = (props: {
|
||||
|
||||
return (
|
||||
<BlockContext.Provider value={blockValue}>
|
||||
<DataSourceName.Provider value={dataSource}>
|
||||
<CollectionDataSourceProvider dataSource={dataSource}>
|
||||
<MaybeCollectionProvider collection={collection}>
|
||||
<BlockAssociationContext.Provider value={association}>
|
||||
<BlockResourceContext.Provider value={resource}>
|
||||
@ -345,7 +341,7 @@ export const BlockProvider = (props: {
|
||||
</BlockResourceContext.Provider>
|
||||
</BlockAssociationContext.Provider>
|
||||
</MaybeCollectionProvider>
|
||||
</DataSourceName.Provider>
|
||||
</CollectionDataSourceProvider>
|
||||
</BlockContext.Provider>
|
||||
);
|
||||
};
|
||||
|
@ -4,7 +4,7 @@ import { CollectionOptions } from './types';
|
||||
import React from 'react';
|
||||
import { DeletedPlaceholder } from '../application/collection/DeletedPlaceholder';
|
||||
import { CollectionExtendsProvider } from './CollectionManagerProvider';
|
||||
import { useDataSourceName } from '../block-provider/BlockProvider';
|
||||
import { useCollectionDataSourceName } from '../application/data-block';
|
||||
|
||||
function getCollectionName(name?: string | CollectionOptions): string {
|
||||
if (!name) return undefined;
|
||||
@ -20,7 +20,7 @@ export const CollectionProvider: FC<{
|
||||
dataSource?: string;
|
||||
}> = ({ children, allowNull, name, dataSource, collection }) => {
|
||||
const collectionName = getCollectionName(name || collection);
|
||||
const dataSourceName = useDataSourceName();
|
||||
const dataSourceName = useCollectionDataSourceName();
|
||||
const dataSourceValue = dataSource || dataSourceName || undefined;
|
||||
const cm = useCollectionManagerV2();
|
||||
const hasCollection = cm.getCollection(collectionName, { dataSource: dataSourceValue });
|
||||
|
@ -6,11 +6,11 @@ import { CollectionFieldOptions, CollectionOptions } from '../types';
|
||||
import { useCollectionManagerV2 } from '../../application';
|
||||
import { InheritanceCollectionMixin } from '../mixins/InheritanceCollectionMixin';
|
||||
import { uid } from '@formily/shared';
|
||||
import { useDataSourceName } from '../../block-provider/BlockProvider';
|
||||
import { useCollectionDataSourceName } from '../../application/data-block';
|
||||
|
||||
export const useCollectionManager = (dataSourceName?: string) => {
|
||||
const cm = useCollectionManagerV2();
|
||||
const blockDataSourceName = useDataSourceName();
|
||||
const blockDataSourceName = useCollectionDataSourceName();
|
||||
const dataSourceNameValue = dataSourceName || blockDataSourceName || undefined;
|
||||
const [random, setRandom] = useState(uid());
|
||||
const { refresh } = useSchemaComponentContext();
|
||||
|
@ -4,7 +4,7 @@ import {
|
||||
BlockRequestContext,
|
||||
CollectionProvider,
|
||||
DEFAULT_DATA_SOURCE_NAME,
|
||||
DataSourceName,
|
||||
CollectionDataSourceProvider,
|
||||
FormActiveFieldsProvider,
|
||||
FormBlockContext,
|
||||
FormV2,
|
||||
@ -75,7 +75,7 @@ export function FormBlockProvider(props) {
|
||||
}, [field, form, params, service, updateAssociationValues]);
|
||||
|
||||
return !userJob.status || values ? (
|
||||
<DataSourceName.Provider value={dataSource}>
|
||||
<CollectionDataSourceProvider dataSource={dataSource}>
|
||||
<CollectionProvider collection={props.collection}>
|
||||
<RecordProvider record={values} parent={false}>
|
||||
<FormActiveFieldsProvider name="form">
|
||||
@ -92,6 +92,6 @@ export function FormBlockProvider(props) {
|
||||
</FormActiveFieldsProvider>
|
||||
</RecordProvider>
|
||||
</CollectionProvider>
|
||||
</DataSourceName.Provider>
|
||||
</CollectionDataSourceProvider>
|
||||
) : null;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user