fix(sourceId): avoid error (#4077)

This commit is contained in:
Zeke Zhang 2024-04-17 21:34:05 +08:00 committed by GitHub
parent 946debc737
commit ce6011dbc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 84 additions and 14 deletions

View File

@ -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,
});
});
});

View File

@ -1,13 +1,13 @@
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 { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
import { useDataLoadingMode } from '../../modules/blocks/data-blocks/details-multi/setDataLoadingModeSettingsItem';
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);
BlockRequestContext.displayName = 'BlockRequestContext';
@ -43,6 +43,31 @@ function useCurrentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
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'>) {
const { sourceId, association, parentRecord } = options;
const api = useAPIClient();
@ -50,15 +75,8 @@ function useParentRequest<T>(options: Omit<AllDataBlockProps, 'type'>) {
const headers = useDataSourceHeaders(dataBlockProps.dataSource);
const sourceKey = useSourceKey(association);
return useRequest<T>(
async () => {
if (parentRecord) return Promise.resolve({ data: parentRecord });
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;
() => {
return requestParentRecordData({ sourceId, association, parentRecord, api, headers, sourceKey });
},
{
refreshDeps: [association, parentRecord, sourceId],