fix: association parent request (#3865)

* fix: association parent request

* fix: bug
This commit is contained in:
jack zhang 2024-03-29 15:59:41 +08:00 committed by GitHub
parent d0746b1155
commit 005b5a313d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,7 @@ import { AllDataBlockProps, useDataBlockProps } from './DataBlockProvider';
import { useDataBlockResource } from './DataBlockResourceProvider';
import { useDataSourceHeaders } from '../utils';
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
import { useCollection, useCollectionManager } from '../collection';
export const BlockRequestContext = createContext<UseRequestResult<any>>(null);
BlockRequestContext.displayName = 'BlockRequestContext';
@ -55,6 +56,7 @@ function useCurrentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
const { sourceId, association, parentRecord } = options;
const api = useAPIClient();
const cm = useCollectionManager();
const dataBlockProps = useDataBlockProps();
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
return useRequest<T>(
@ -63,8 +65,12 @@ function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
if (!association) return Promise.resolve({ data: undefined });
// "association": "Collection.Field"
const arr = association.split('.');
// <collection>:get/<filterByTk>
const url = `${arr[0]}:get/${sourceId}`;
const field = cm.getCollectionField(association);
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 });
return res.data;
},
@ -88,6 +94,7 @@ export const BlockRequestProvider: FC = ({ children }) => {
requestOptions,
requestService,
} = props;
const currentRequest = useCurrentRequest<{ data: any }>({
action,
sourceId,