Skip to content

Commit d09be9d

Browse files
refactor: remove unsafe method in document table COMPASS-6137 (#3970)
refactor: refactor usage of UNSAFE_ method in document-table COMPASS-6137
1 parent 87c7678 commit d09be9d

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

packages/compass-crud/src/components/table-view/document-table-view.tsx

+10-27
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export type GridContext = {
9292
class DocumentTableView extends React.Component<DocumentTableViewProps> {
9393
AGGrid: React.ReactElement;
9494
collection: string;
95-
hadronDocs: Document[];
9695
topLevel: boolean;
9796
// eslint-disable-next-line @typescript-eslint/ban-types
9897
unsubscribeGridStore?: Function;
@@ -153,7 +152,6 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
153152
};
154153

155154
this.collection = mongodbns(props.ns).collection;
156-
this.hadronDocs = [];
157155
this.topLevel = true;
158156

159157
this.AGGrid = React.createElement(AgGridReact, sharedGridProperties);
@@ -171,10 +169,6 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
171169
this.gridApi?.destroy?.();
172170
}
173171

174-
UNSAFE_componentWillReceiveProps(nextProps: DocumentTableViewProps) {
175-
this.hadronDocs = nextProps.docs;
176-
}
177-
178172
componentDidUpdate(prevProps: DocumentTableViewProps) {
179173
this.handleBreadcrumbChange();
180174

@@ -204,7 +198,6 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
204198
this.gridApi = params.api;
205199
this.columnApi = params.columnApi;
206200

207-
this.hadronDocs = this.props.docs;
208201
this.handleBreadcrumbChange();
209202
}
210203

@@ -330,13 +323,12 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
330323
rowNumber: rowNumber,
331324
};
332325

333-
/* Update this.hadronDocs */
334-
for (let i = 0; i < this.hadronDocs.length; i++) {
326+
for (let i = 0; i < this.props.docs.length; i++) {
335327
if (
336-
this.hadronDocs[i].getStringId() ===
328+
this.props.docs[i].getStringId() ===
337329
newData.hadronDocument.getStringId()
338330
) {
339-
this.hadronDocs[i] = newData.hadronDocument;
331+
this.props.docs[i] = newData.hadronDocument;
340332
break;
341333
}
342334
}
@@ -561,9 +553,6 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
561553
rowNumber: lineNumber,
562554
};
563555

564-
/* Update this.hadronDocs */
565-
this.hadronDocs.splice(index, 0, data.hadronDocument);
566-
567556
if (this.topLevel) {
568557
/* Update row numbers */
569558
this.updateRowNumbers(lineNumber, true);
@@ -613,24 +602,20 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
613602
if (params.path.length === 0) {
614603
this.topLevel = true;
615604

616-
const headers = this.createColumnHeaders(this.hadronDocs, [], []);
605+
const headers = this.createColumnHeaders([], []);
617606

618607
(this.gridApi as any).gridOptionsWrapper.gridOptions.context.path = [];
619608
this.gridApi.setColumnDefs(headers);
620609
this.gridApi.setRowData(
621-
this.createRowData(this.hadronDocs, this.props.start)
610+
this.createRowData(this.props.docs, this.props.start)
622611
);
623612
} else if (
624613
params.types[params.types.length - 1] === 'Object' ||
625614
params.types[params.types.length - 1] === 'Array'
626615
) {
627616
this.topLevel = false;
628617

629-
const headers = this.createColumnHeaders(
630-
this.hadronDocs,
631-
params.path,
632-
params.types
633-
);
618+
const headers = this.createColumnHeaders(params.path, params.types);
634619
headers.push(this.createObjectIdHeader());
635620

636621
if (headers.length <= 3) {
@@ -644,7 +629,7 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
644629

645630
(this.gridApi as any).gridOptionsWrapper.gridOptions.context.path =
646631
params.path;
647-
this.gridApi.setRowData(this.createRowData(this.hadronDocs, 1));
632+
this.gridApi.setRowData(this.createRowData(this.props.docs, 1));
648633
this.gridApi.setColumnDefs(headers);
649634
}
650635
this.gridApi.refreshCells({ force: true });
@@ -869,15 +854,13 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
869854
* Third, get the displayed type for the headers of each of the field columns.
870855
* Last, add the document level actions column that is pinned to the right.
871856
*
872-
* @param {Array} hadronDocs - The list of HadronDocuments.
873857
* @param {Array} path - The list of path segments. Empty when top-level.
874858
* @param {Array} types - The list of types. If element is not of the correct
875859
* type, then don't render it.
876860
*
877861
* @returns {object} the ColHeaders, which is a list of colDefs.
878862
*/
879863
createColumnHeaders = (
880-
hadronDocs: Document[],
881864
path: (string | number)[],
882865
types: TableHeaderType[]
883866
): ColDef[] => {
@@ -902,9 +885,9 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
902885
};
903886

904887
/* Make column definitions + track type for header components */
905-
for (let i = 0; i < hadronDocs.length; i++) {
888+
for (let i = 0; i < this.props.docs.length; i++) {
906889
/* Get the top-level element/document in the view */
907-
let topLevel: Document | Element = hadronDocs[i];
890+
let topLevel: Document | Element = this.props.docs[i];
908891

909892
if (path.length > 0) {
910893
topLevel = topLevel.getChild(path)!;
@@ -926,7 +909,7 @@ class DocumentTableView extends React.Component<DocumentTableViewProps> {
926909
if (!(key in headerTypes)) {
927910
headerTypes[key] = {};
928911
}
929-
headerTypes[key][String(hadronDocs[i].getStringId())] = type;
912+
headerTypes[key][String(this.props.docs[i].getStringId())] = type;
930913
}
931914
}
932915

0 commit comments

Comments
 (0)