refactor(RecordV2): remove collectionName

This commit is contained in:
Zeke Zhang 2024-02-08 09:11:38 +08:00
parent 08790dc99f
commit e9e6d4a817
3 changed files with 9 additions and 42 deletions

View File

@ -1,7 +1,6 @@
import { useDeepCompareEffect, useUpdateEffect } from 'ahooks';
import React, { FC, createContext, useContext } from 'react';
import { isString } from 'lodash';
import { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
import { RecordProviderV2, RecordV2 } from '../record';
import { AllDataBlockPropsV2, useDataBlockPropsV2 } from './DataBlockProvider';
@ -110,11 +109,7 @@ export const BlockRequestProviderV2: FC = ({ children }) => {
<RecordProviderV2
isNew={action === undefined}
record={currentRequest.data?.data}
parentRecord={
parentRequest.data?.data &&
new RecordV2({ isNew: false, data: parentRequest.data?.data, collectionName: association.split('.')[0] })
}
collectionName={isString(collection) ? collection : collection?.name}
parentRecord={parentRequest.data?.data && new RecordV2({ isNew: false, data: parentRequest.data?.data })}
>
{children}
</RecordProviderV2>
@ -122,11 +117,7 @@ export const BlockRequestProviderV2: FC = ({ children }) => {
<RecordProviderV2
isNew={false}
record={null}
parentRecord={
parentRequest.data?.data &&
new RecordV2({ isNew: false, data: parentRequest.data?.data, collectionName: association.split('.')[0] })
}
collectionName={isString(collection) ? collection : collection?.name}
parentRecord={parentRequest.data?.data && new RecordV2({ isNew: false, data: parentRequest.data?.data })}
>
{children}
</RecordProviderV2>

View File

@ -2,26 +2,17 @@ export interface RecordOptionsV2<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
data?: DataType;
parentRecord?: RecordV2<ParentDataType>;
/**
* collection name
*/
collectionName?: string;
}
export class RecordV2<DataType = {}, ParentDataType = {}> {
public data?: DataType;
public parentRecord?: RecordV2<ParentDataType>;
public isNew?: boolean;
/**
* collection name
*/
public collectionName?: string;
constructor(options: RecordOptionsV2<DataType, ParentDataType>) {
const { data, parentRecord, isNew, collectionName } = options;
const { data, parentRecord, isNew } = options;
this.isNew = isNew;
this.data = data;
this.parentRecord = parentRecord;
this.collectionName = collectionName;
}
setData(data: DataType) {

View File

@ -9,31 +9,16 @@ export interface RecordProviderPropsV2<DataType = {}, ParentDataType = {}> {
isNew?: boolean;
record?: RecordV2<DataType, ParentDataType> | DataType;
parentRecord?: RecordV2<ParentDataType> | DataType;
/**
* collection name
*/
collectionName?: string;
/**
* collection name
*/
parentCollectionName?: string;
}
export const RecordProviderV2: FC<RecordProviderPropsV2> = ({
isNew,
record,
parentRecord,
children,
collectionName,
parentCollectionName,
}) => {
export const RecordProviderV2: FC<RecordProviderPropsV2> = ({ isNew, record, parentRecord, children }) => {
const parentRecordValue = useMemo(() => {
if (parentRecord) {
if (parentRecord instanceof RecordV2) return parentRecord;
return new RecordV2({ data: parentRecord, collectionName: parentCollectionName });
return new RecordV2({ data: parentRecord });
}
if (record instanceof RecordV2) return record.parentRecord;
}, [parentCollectionName, parentRecord, record]);
}, [parentRecord, record]);
const currentRecordValue = useMemo(() => {
let res: RecordV2;
@ -42,14 +27,14 @@ export const RecordProviderV2: FC<RecordProviderPropsV2> = ({
res = record;
res.isNew = record.isNew || isNew;
} else {
res = new RecordV2({ data: record, isNew, collectionName });
res = new RecordV2({ data: record, isNew });
}
} else {
res = new RecordV2({ isNew, collectionName });
res = new RecordV2({ isNew });
}
res.setParentRecord(parentRecordValue);
return res;
}, [record, parentRecordValue, isNew, collectionName]);
}, [record, parentRecordValue, isNew]);
return <RecordContextV2.Provider value={currentRecordValue}>{children}</RecordContextV2.Provider>;
};