mirror of
https://github.com/nocobase/nocobase
synced 2024-11-16 11:16:08 +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 { useDeepCompareEffect, useUpdateEffect } from 'ahooks';
|
||||||
import React, { FC, createContext, useContext } from 'react';
|
import React, { FC, createContext, useContext } from 'react';
|
||||||
|
|
||||||
import { isString } from 'lodash';
|
|
||||||
import { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
|
import { UseRequestResult, useAPIClient, useRequest } from '../../api-client';
|
||||||
import { RecordProviderV2, RecordV2 } from '../record';
|
import { RecordProviderV2, RecordV2 } from '../record';
|
||||||
import { AllDataBlockPropsV2, useDataBlockPropsV2 } from './DataBlockProvider';
|
import { AllDataBlockPropsV2, useDataBlockPropsV2 } from './DataBlockProvider';
|
||||||
@ -110,11 +109,7 @@ export const BlockRequestProviderV2: FC = ({ children }) => {
|
|||||||
<RecordProviderV2
|
<RecordProviderV2
|
||||||
isNew={action === undefined}
|
isNew={action === undefined}
|
||||||
record={currentRequest.data?.data}
|
record={currentRequest.data?.data}
|
||||||
parentRecord={
|
parentRecord={parentRequest.data?.data && new RecordV2({ isNew: false, data: parentRequest.data?.data })}
|
||||||
parentRequest.data?.data &&
|
|
||||||
new RecordV2({ isNew: false, data: parentRequest.data?.data, collectionName: association.split('.')[0] })
|
|
||||||
}
|
|
||||||
collectionName={isString(collection) ? collection : collection?.name}
|
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</RecordProviderV2>
|
</RecordProviderV2>
|
||||||
@ -122,11 +117,7 @@ export const BlockRequestProviderV2: FC = ({ children }) => {
|
|||||||
<RecordProviderV2
|
<RecordProviderV2
|
||||||
isNew={false}
|
isNew={false}
|
||||||
record={null}
|
record={null}
|
||||||
parentRecord={
|
parentRecord={parentRequest.data?.data && new RecordV2({ isNew: false, data: parentRequest.data?.data })}
|
||||||
parentRequest.data?.data &&
|
|
||||||
new RecordV2({ isNew: false, data: parentRequest.data?.data, collectionName: association.split('.')[0] })
|
|
||||||
}
|
|
||||||
collectionName={isString(collection) ? collection : collection?.name}
|
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</RecordProviderV2>
|
</RecordProviderV2>
|
||||||
|
@ -2,26 +2,17 @@ export interface RecordOptionsV2<DataType = {}, ParentDataType = {}> {
|
|||||||
isNew?: boolean;
|
isNew?: boolean;
|
||||||
data?: DataType;
|
data?: DataType;
|
||||||
parentRecord?: RecordV2<ParentDataType>;
|
parentRecord?: RecordV2<ParentDataType>;
|
||||||
/**
|
|
||||||
* 当前记录所属的 collection name
|
|
||||||
*/
|
|
||||||
collectionName?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RecordV2<DataType = {}, ParentDataType = {}> {
|
export class RecordV2<DataType = {}, ParentDataType = {}> {
|
||||||
public data?: DataType;
|
public data?: DataType;
|
||||||
public parentRecord?: RecordV2<ParentDataType>;
|
public parentRecord?: RecordV2<ParentDataType>;
|
||||||
public isNew?: boolean;
|
public isNew?: boolean;
|
||||||
/**
|
|
||||||
* 当前记录所属的 collection name
|
|
||||||
*/
|
|
||||||
public collectionName?: string;
|
|
||||||
constructor(options: RecordOptionsV2<DataType, ParentDataType>) {
|
constructor(options: RecordOptionsV2<DataType, ParentDataType>) {
|
||||||
const { data, parentRecord, isNew, collectionName } = options;
|
const { data, parentRecord, isNew } = options;
|
||||||
this.isNew = isNew;
|
this.isNew = isNew;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.parentRecord = parentRecord;
|
this.parentRecord = parentRecord;
|
||||||
this.collectionName = collectionName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setData(data: DataType) {
|
setData(data: DataType) {
|
||||||
|
@ -9,31 +9,16 @@ export interface RecordProviderPropsV2<DataType = {}, ParentDataType = {}> {
|
|||||||
isNew?: boolean;
|
isNew?: boolean;
|
||||||
record?: RecordV2<DataType, ParentDataType> | DataType;
|
record?: RecordV2<DataType, ParentDataType> | DataType;
|
||||||
parentRecord?: RecordV2<ParentDataType> | DataType;
|
parentRecord?: RecordV2<ParentDataType> | DataType;
|
||||||
/**
|
|
||||||
* 当前记录所属的 collection name
|
|
||||||
*/
|
|
||||||
collectionName?: string;
|
|
||||||
/**
|
|
||||||
* 当前记录所属的父级 collection name
|
|
||||||
*/
|
|
||||||
parentCollectionName?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RecordProviderV2: FC<RecordProviderPropsV2> = ({
|
export const RecordProviderV2: FC<RecordProviderPropsV2> = ({ isNew, record, parentRecord, children }) => {
|
||||||
isNew,
|
|
||||||
record,
|
|
||||||
parentRecord,
|
|
||||||
children,
|
|
||||||
collectionName,
|
|
||||||
parentCollectionName,
|
|
||||||
}) => {
|
|
||||||
const parentRecordValue = useMemo(() => {
|
const parentRecordValue = useMemo(() => {
|
||||||
if (parentRecord) {
|
if (parentRecord) {
|
||||||
if (parentRecord instanceof RecordV2) return 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;
|
if (record instanceof RecordV2) return record.parentRecord;
|
||||||
}, [parentCollectionName, parentRecord, record]);
|
}, [parentRecord, record]);
|
||||||
|
|
||||||
const currentRecordValue = useMemo(() => {
|
const currentRecordValue = useMemo(() => {
|
||||||
let res: RecordV2;
|
let res: RecordV2;
|
||||||
@ -42,14 +27,14 @@ export const RecordProviderV2: FC<RecordProviderPropsV2> = ({
|
|||||||
res = record;
|
res = record;
|
||||||
res.isNew = record.isNew || isNew;
|
res.isNew = record.isNew || isNew;
|
||||||
} else {
|
} else {
|
||||||
res = new RecordV2({ data: record, isNew, collectionName });
|
res = new RecordV2({ data: record, isNew });
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res = new RecordV2({ isNew, collectionName });
|
res = new RecordV2({ isNew });
|
||||||
}
|
}
|
||||||
res.setParentRecord(parentRecordValue);
|
res.setParentRecord(parentRecordValue);
|
||||||
return res;
|
return res;
|
||||||
}, [record, parentRecordValue, isNew, collectionName]);
|
}, [record, parentRecordValue, isNew]);
|
||||||
|
|
||||||
return <RecordContextV2.Provider value={currentRecordValue}>{children}</RecordContextV2.Provider>;
|
return <RecordContextV2.Provider value={currentRecordValue}>{children}</RecordContextV2.Provider>;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user