From e5dbbfda86780521520c3a8527183ddc8460ead1 Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Sun, 14 Nov 2021 12:32:12 +0100
Subject: [PATCH] more readable dual_link_to component

---
 app/components/breadcrumb/index.hbs    | 17 ++-----
 app/components/commander/index.hbs     | 19 ++------
 app/components/dual_link_to.hbs        |  6 +++
 app/components/dual_link_to.js         | 61 ++++++++++++++++++++++++++
 app/templates/authenticated/viewer.hbs |  2 +-
 5 files changed, 76 insertions(+), 29 deletions(-)
 create mode 100644 app/components/dual_link_to.hbs
 create mode 100644 app/components/dual_link_to.js

diff --git a/app/components/breadcrumb/index.hbs b/app/components/breadcrumb/index.hbs
index f57949e..a12ba82 100644
--- a/app/components/breadcrumb/index.hbs
+++ b/app/components/breadcrumb/index.hbs
@@ -2,19 +2,10 @@
    <ol class="breadcrumb">
     {{#each this.nodes as |node|}}
       <li class="breadcrumb-item">
-        {{#if (is_equal @hint "left")}}
-          {{#if (is_equal node.nodeType "document") }}
-            <Document::LinkToLeft @node={{node}} />
-          {{else}}
-            <Folder::LinkToLeft
-              @node={{node}}
-              @extranode={{@extranode}} />
-          {{/if}}
-        {{else}}
-          <Folder::LinkToRight
-            @node={{@extranode}}
-            @extranode={{node}} />
-        {{/if}}
+        <DualLinkTo
+          @node={{node}}
+          @extranode={{@extranode}}
+          @hint={{@hint}} />
       </li>
     {{/each}}
   </ol>
diff --git a/app/components/commander/index.hbs b/app/components/commander/index.hbs
index 1683188..abde0d3 100644
--- a/app/components/commander/index.hbs
+++ b/app/components/commander/index.hbs
@@ -44,21 +44,10 @@
           @model={{node}}
           @selectedNodes={{this.selected_nodes}}
           @onCheckboxChange={{this.onCheckboxChange}} >
-          {{#if (is_equal @hint "left")}}
-            {{#if (is_equal node.nodeType "folder")}}
-              <Folder::LinkToLeft
-                @node={{node}}
-                @extranode={{@extranode}} />
-            {{else}}
-              <Document::LinkToLeft
-                @node={{node}}
-                @extranode={{@extranode}} />
-            {{/if}}
-          {{else}}
-            <Folder::LinkToRight
-              @node={{@extranode}}
-              @extranode={{node}} />
-          {{/if}}
+            <DualLinkTo
+              @node={{node}}
+              @extranode={{@extranode}}
+              @hint={{@hint}} />
         </NodeType>
       {{/let}}
     {{/each}}
diff --git a/app/components/dual_link_to.hbs b/app/components/dual_link_to.hbs
new file mode 100644
index 0000000..6e93ba5
--- /dev/null
+++ b/app/components/dual_link_to.hbs
@@ -0,0 +1,6 @@
+<LinkTo
+  @route={{this.route}}
+  @model={{this.model.id}}
+  @query={{this.query}}>
+    {{this.title}}
+</LinkTo>
diff --git a/app/components/dual_link_to.js b/app/components/dual_link_to.js
new file mode 100644
index 0000000..c4a1d25
--- /dev/null
+++ b/app/components/dual_link_to.js
@@ -0,0 +1,61 @@
+import Component from '@glimmer/component';
+
+
+export default class DualLinkToComponent extends Component {
+
+  get route() {
+    let node,
+      hint;
+
+    hint = this.args.hint;
+    node = this.args.node;
+    if (hint == 'left') {
+      if (node && node.get('nodeType') === 'document') {
+        return 'authenticated.document';
+      }
+
+      if (node && node.get('nodeType') === 'folder') {
+        return 'authenticated.nodes';
+      }
+    }
+
+    return 'authenticated.nodes';
+  }
+
+  get model() {
+    if (this.args.hint == 'left') {
+      return this.args.node;
+    }
+
+    return this.args.extranode;
+  }
+
+  get title() {
+    return this.args.node.get('title');
+  }
+
+  get query() {
+    let node,
+      extranode,
+      hint;
+
+    node = this.args.node;
+    extranode = this.args.extranode;
+    hint = this.args.hint;
+
+    if ((hint === 'left') && extranode) {
+      return {
+        'extranode_id': extranode.get('id')
+      };
+    }
+
+    if (hint === 'right' && node) {
+      return {
+        'extranode_id': node.get('id')
+      }
+    }
+
+    return {};
+  } // end of query
+
+} // end of DualLinkToComponent
diff --git a/app/templates/authenticated/viewer.hbs b/app/templates/authenticated/viewer.hbs
index ce1018e..51889d3 100644
--- a/app/templates/authenticated/viewer.hbs
+++ b/app/templates/authenticated/viewer.hbs
@@ -9,7 +9,7 @@
   {{#if this.dualpanel_mode}}
     <Commander
       @node={{this.extranode}}
-      @extranode={{this.mainnode}}
+      @extradoc={{@model}}
       @onPanelToggle={{this.onPanelToggle}}
       @dualPanelMode={{this.dualpanel_mode}}
       @hint="right" />
-- 
GitLab