mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 11:47:35 +00:00
refactor(RecordV2): remove collectionName
This commit is contained in:
parent
08790dc99f
commit
e9e6d4a817
@ -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>
|
||||
|
@ -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) {
|
||||
|
@ -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>;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user