Browse Source

fix(admin-ui): Fix incompatible type def

Michael Bromley 6 years ago
parent
commit
a983de75fd

+ 11 - 6
admin-ui/src/app/catalog/components/collection-tree/array-to-tree.ts

@@ -1,4 +1,4 @@
-export type HasParent = { id: string; parent: { id: string } };
+export type HasParent = { id: string; parent?: { id: string } | null };
 export type TreeNode<T extends HasParent> = T & { children: Array<TreeNode<T>> };
 export type RootNode<T extends HasParent> = { id?: string; children: Array<TreeNode<T>> };
 
@@ -18,19 +18,24 @@ export function arrayToTree<T extends HasParent>(nodes: T[]): RootNode<T> {
     for (const id of nodes.map(n => n.id)) {
         if (mappedArr.hasOwnProperty(id)) {
             const mappedElem = mappedArr[id];
+            const parent = mappedElem.parent;
+            if (!parent) {
+                continue;
+            }
             // If the element is not at the root level, add it to its parent array of children.
-            const parentIsRoot = !mappedArr[mappedElem.parent.id];
+            const parentIsRoot = !mappedArr[parent.id];
             if (!parentIsRoot) {
-                if (mappedArr[mappedElem.parent.id]) {
-                    mappedArr[mappedElem.parent.id].children.push(mappedElem);
+                if (mappedArr[parent.id]) {
+                    mappedArr[parent.id].children.push(mappedElem);
                 } else {
-                    mappedArr[mappedElem.parent.id] = { children: [mappedElem] } as any;
+                    mappedArr[parent.id] = { children: [mappedElem] } as any;
                 }
             } else {
                 topLevelNodes.push(mappedElem);
             }
         }
     }
-    const rootId = topLevelNodes.length ? topLevelNodes[0].parent.id : undefined;
+    // tslint:disable-next-line:no-non-null-assertion
+    const rootId = topLevelNodes.length ? topLevelNodes[0].parent!.id : undefined;
     return { id: rootId, children: topLevelNodes };
 }

+ 18 - 11
admin-ui/src/app/catalog/components/collection-tree/collection-tree-node.component.ts

@@ -30,15 +30,16 @@ export class CollectionTreeNodeComponent implements OnInit {
         this.parentName = this.collectionTree.name || '<root>';
     }
 
-    getMoveListItems(category: Collection.Fragment): Array<{ path: string; id: string }> {
+    getMoveListItems(collection: Collection.Fragment): Array<{ path: string; id: string }> {
         const visit = (
             node: TreeNode<any>,
             parentPath: string[],
             output: Array<{ path: string; id: string }>,
         ) => {
-            if (node.id !== category.id) {
+            if (node.id !== collection.id) {
                 const path = parentPath.concat(node.name);
-                if (node.id !== category.parent.id) {
+                const parentId = collection.parent && collection.parent.id;
+                if (node.id !== parentId) {
                     output.push({ path: path.slice(1).join(' / ') || 'root', id: node.id });
                 }
                 node.children.forEach(child => visit(child, path, output));
@@ -48,27 +49,33 @@ export class CollectionTreeNodeComponent implements OnInit {
         return visit(this.root.collectionTree, [], []);
     }
 
-    move(category: Collection.Fragment, parentId: string) {
+    move(collection: Collection.Fragment, parentId: string) {
         this.root.onMove({
             index: 0,
             parentId,
-            collectionId: category.id,
+            collectionId: collection.id,
         });
     }
 
-    moveUp(category: Collection.Fragment, currentIndex: number) {
+    moveUp(collection: Collection.Fragment, currentIndex: number) {
+        if (!collection.parent) {
+            return;
+        }
         this.root.onMove({
             index: currentIndex - 1,
-            parentId: category.parent.id,
-            collectionId: category.id,
+            parentId: collection.parent.id,
+            collectionId: collection.id,
         });
     }
 
-    moveDown(category: Collection.Fragment, currentIndex: number) {
+    moveDown(collection: Collection.Fragment, currentIndex: number) {
+        if (!collection.parent) {
+            return;
+        }
         this.root.onMove({
             index: currentIndex + 1,
-            parentId: category.parent.id,
-            collectionId: category.id,
+            parentId: collection.parent.id,
+            collectionId: collection.id,
         });
     }