mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
fix(sourceId): avoid error (#4077)
This commit is contained in:
parent
946debc737
commit
ce6011dbc1
@ -0,0 +1,52 @@
|
|||||||
|
import { requestParentRecordData } from '../../data-block/DataBlockRequestProvider';
|
||||||
|
|
||||||
|
describe('requestParentRecordData', () => {
|
||||||
|
it('should return parent record data if parentRecord is provided', async () => {
|
||||||
|
const parentRecord = { id: 1, name: 'John Doe' };
|
||||||
|
const result = await requestParentRecordData({ parentRecord });
|
||||||
|
expect(result).toEqual({ data: parentRecord });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if association, sourceKey, or sourceId is missing', async () => {
|
||||||
|
const result = await requestParentRecordData({});
|
||||||
|
expect(result).toEqual({ data: undefined });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if association is missing', async () => {
|
||||||
|
const sourceId = 1;
|
||||||
|
const sourceKey = 'filterKey';
|
||||||
|
const result = await requestParentRecordData({ sourceId, sourceKey });
|
||||||
|
expect(result).toEqual({ data: undefined });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if sourceKey is missing', async () => {
|
||||||
|
const association = 'Collection.Field';
|
||||||
|
const sourceId = 1;
|
||||||
|
const result = await requestParentRecordData({ association, sourceId });
|
||||||
|
expect(result).toEqual({ data: undefined });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return undefined if sourceId is missing', async () => {
|
||||||
|
const association = 'Collection.Field';
|
||||||
|
const sourceKey = 'filterKey';
|
||||||
|
const result = await requestParentRecordData({ association, sourceKey });
|
||||||
|
expect(result).toEqual({ data: undefined });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should make a request to the correct URL if association is provided', async () => {
|
||||||
|
const association = 'Collection.Field';
|
||||||
|
const sourceId = 1;
|
||||||
|
const sourceKey = 'filterKey';
|
||||||
|
const api = {
|
||||||
|
request: vi.fn().mockResolvedValue({ data: { id: 1, name: 'John Doe' } }),
|
||||||
|
};
|
||||||
|
const headers = { Authorization: 'Bearer token' };
|
||||||
|
|
||||||
|
await requestParentRecordData({ association, sourceId, sourceKey, api, headers });
|
||||||
|
|
||||||
|
expect(api.request).toHaveBeenCalledWith({
|
||||||
|
url: 'Collection:get?filter[filterKey]=1',
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,13 +1,13 @@
|
|||||||
import React, { FC, createContext, useContext, useMemo } from 'react';
|
import React, { FC, createContext, useContext, useMemo } from 'react';
|
||||||
|
|
||||||
import { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
|
|
||||||
import { CollectionRecordProvider, CollectionRecord } from '../collection-record';
|
|
||||||
import { AllDataBlockProps, useDataBlockProps } from './DataBlockProvider';
|
|
||||||
import { useDataBlockResource } from './DataBlockResourceProvider';
|
|
||||||
import { useDataSourceHeaders } from '../utils';
|
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
|
||||||
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
|
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
|
||||||
import { useSourceKey } from '../../modules/blocks/useSourceKey';
|
import { useSourceKey } from '../../modules/blocks/useSourceKey';
|
||||||
|
import { CollectionRecord, CollectionRecordProvider } from '../collection-record';
|
||||||
|
import { useDataSourceHeaders } from '../utils';
|
||||||
|
import { AllDataBlockProps, useDataBlockProps } from './DataBlockProvider';
|
||||||
|
import { useDataBlockResource } from './DataBlockResourceProvider';
|
||||||
|
|
||||||
export const BlockRequestContext = createContext<UseRequestResult<any>>(null);
|
export const BlockRequestContext = createContext<UseRequestResult<any>>(null);
|
||||||
BlockRequestContext.displayName = 'BlockRequestContext';
|
BlockRequestContext.displayName = 'BlockRequestContext';
|
||||||
@ -43,6 +43,31 @@ function useCurrentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function requestParentRecordData({
|
||||||
|
sourceId,
|
||||||
|
association,
|
||||||
|
parentRecord,
|
||||||
|
api,
|
||||||
|
headers,
|
||||||
|
sourceKey,
|
||||||
|
}: {
|
||||||
|
sourceId?: number;
|
||||||
|
association?: string;
|
||||||
|
parentRecord?: any;
|
||||||
|
api?: any;
|
||||||
|
headers?: any;
|
||||||
|
sourceKey?: string;
|
||||||
|
}) {
|
||||||
|
if (parentRecord) return Promise.resolve({ data: parentRecord });
|
||||||
|
if (!association || !sourceKey || !sourceId) return Promise.resolve({ data: undefined });
|
||||||
|
// "association": "Collection.Field"
|
||||||
|
const arr = association.split('.');
|
||||||
|
// <collection>:get?filter[filterKey]=sourceId
|
||||||
|
const url = `${arr[0]}:get?filter[${sourceKey}]=${sourceId}`;
|
||||||
|
const res = await api.request({ url, headers });
|
||||||
|
return res.data;
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
||||||
@ -50,15 +75,8 @@ function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
|
|||||||
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
|
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
|
||||||
const sourceKey = useSourceKey(association);
|
const sourceKey = useSourceKey(association);
|
||||||
return useRequest<T>(
|
return useRequest<T>(
|
||||||
async () => {
|
() => {
|
||||||
if (parentRecord) return Promise.resolve({ data: parentRecord });
|
return requestParentRecordData({ sourceId, association, parentRecord, api, headers, sourceKey });
|
||||||
if (!association || !sourceKey) return Promise.resolve({ data: undefined });
|
|
||||||
// "association": "Collection.Field"
|
|
||||||
const arr = association.split('.');
|
|
||||||
// <collection>:get?filter[filterKey]=sourceId
|
|
||||||
const url = `${arr[0]}:get?filter[${sourceKey}]=${sourceId}`;
|
|
||||||
const res = await api.request({ url, headers });
|
|
||||||
return res.data;
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
refreshDeps: [association, parentRecord, sourceId],
|
refreshDeps: [association, parentRecord, sourceId],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user