15768: selections persist through sort/refresh, not filter Arvados-DCO-1.1-Signed...
authorLisa Knox <lisaknox83@gmail.com>
Wed, 26 Apr 2023 21:21:49 +0000 (17:21 -0400)
committerLisa Knox <lisaknox83@gmail.com>
Wed, 26 Apr 2023 21:21:49 +0000 (17:21 -0400)
src/components/data-table/data-table.tsx
src/validators/arrays-are-congruent.tsx [new file with mode: 0644]

index 68792e013dfa94bb7a5e37b0b1303c08c93aecc6..57753c057b313066f9b2b0ea90a24df328bab6a2 100644 (file)
@@ -13,8 +13,8 @@ import { countNodes, getTreeDirty } from 'models/tree';
 import { IconType, PendingIcon } from 'components/icon/icon';
 import { SvgIconProps } from '@material-ui/core/SvgIcon';
 import ArrowDownwardIcon from '@material-ui/icons/ArrowDownward';
-import { Checkbox } from '@material-ui/core';
 import { createTree } from 'models/tree';
+import arraysAreCongruent from 'validators/arrays-are-congruent';
 
 export type DataColumns<I, R> = Array<DataColumn<I, R>>;
 
@@ -97,12 +97,22 @@ export const DataTable = withStyles(styles)(
     class Component<T> extends React.Component<DataTableProps<T>> {
         state: DataTableState = {};
 
+        componentDidMount(): void {
+            this.initializeCheckedList(this.props.items);
+        }
+
         componentDidUpdate(prevProps: Readonly<DataTableProps<T>>) {
-            if (prevProps.items !== this.props.items) {
+            console.log(prevProps.items, this.props.items, this.state);
+            console.log(this.props.currentRoute);
+            if (!arraysAreCongruent(prevProps.items, this.props.items)) {
                 this.initializeCheckedList(this.props.items);
             }
         }
 
+        componentWillUnmount(): void {
+            console.log('UNMOUNT');
+        }
+
         checkBoxColumn: DataColumn<any, any> = {
             name: 'checkBoxColumn',
             selected: true,
diff --git a/src/validators/arrays-are-congruent.tsx b/src/validators/arrays-are-congruent.tsx
new file mode 100644 (file)
index 0000000..d945600
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export default function arraysAreCongruent<T>(arr1: T[], arr2: T[]): boolean {
+    if (!arr1.length || !arr2.length) return false;
+    if (arr1.length !== arr2.length) return false;
+
+    const sortedArr1 = [...arr1].sort();
+    const sortedArr2 = [...arr2].sort();
+
+    for (let i = 0; i < sortedArr1.length; i++) {
+        if (sortedArr1[i] !== sortedArr2[i]) return false;
+    }
+
+    return true;
+}