fix: association parent request (#3865)
* fix: association parent request * fix: bug
This commit is contained in:
parent
d0746b1155
commit
005b5a313d
@ -7,6 +7,7 @@ import { AllDataBlockProps, useDataBlockProps } from './DataBlockProvider';
|
|||||||
import { useDataBlockResource } from './DataBlockResourceProvider';
|
import { useDataBlockResource } from './DataBlockResourceProvider';
|
||||||
import { useDataSourceHeaders } from '../utils';
|
import { useDataSourceHeaders } from '../utils';
|
||||||
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
|
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
|
||||||
|
import { useCollection, useCollectionManager } from '../collection';
|
||||||
|
|
||||||
export const BlockRequestContext = createContext<UseRequestResult<any>>(null);
|
export const BlockRequestContext = createContext<UseRequestResult<any>>(null);
|
||||||
BlockRequestContext.displayName = 'BlockRequestContext';
|
BlockRequestContext.displayName = 'BlockRequestContext';
|
||||||
@ -55,6 +56,7 @@ function useCurrentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
|||||||
function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
||||||
const { sourceId, association, parentRecord } = options;
|
const { sourceId, association, parentRecord } = options;
|
||||||
const api = useAPIClient();
|
const api = useAPIClient();
|
||||||
|
const cm = useCollectionManager();
|
||||||
const dataBlockProps = useDataBlockProps();
|
const dataBlockProps = useDataBlockProps();
|
||||||
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
|
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
|
||||||
return useRequest<T>(
|
return useRequest<T>(
|
||||||
@ -63,8 +65,12 @@ function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
|||||||
if (!association) return Promise.resolve({ data: undefined });
|
if (!association) return Promise.resolve({ data: undefined });
|
||||||
// "association": "Collection.Field"
|
// "association": "Collection.Field"
|
||||||
const arr = association.split('.');
|
const arr = association.split('.');
|
||||||
// <collection>:get/<filterByTk>
|
const field = cm.getCollectionField(association);
|
||||||
const url = `${arr[0]}:get/${sourceId}`;
|
const isM2O = field.interface === 'm2o';
|
||||||
|
const filterTargetKey = cm.getCollection(arr[0]).getOption('filterTargetKey');
|
||||||
|
const filterKey = isM2O ? filterTargetKey : field.sourceKey;
|
||||||
|
// <collection>:get?filter[filterKey]=sourceId
|
||||||
|
const url = `${arr[0]}:get?filter[${filterKey}]=${sourceId}`;
|
||||||
const res = await api.request({ url, headers });
|
const res = await api.request({ url, headers });
|
||||||
return res.data;
|
return res.data;
|
||||||
},
|
},
|
||||||
@ -88,6 +94,7 @@ export const BlockRequestProvider: FC = ({ children }) => {
|
|||||||
requestOptions,
|
requestOptions,
|
||||||
requestService,
|
requestService,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const currentRequest = useCurrentRequest<{ data: any }>({
|
const currentRequest = useCurrentRequest<{ data: any }>({
|
||||||
action,
|
action,
|
||||||
sourceId,
|
sourceId,
|
||||||
|
Loading…
Reference in New Issue
Block a user