From be3001d5d28746a6cb1c0300339d79a2cd7476b3 Mon Sep 17 00:00:00 2001
From: Moises Sacal <moisbo@gmail.com>
Date: Wed, 2 Oct 2019 17:51:12 +1000
Subject: [PATCH] added related object search eresearch/rdm#1292

---
 src/components/Router.js            | 22 ++++++++++++++++++++--
 src/components/SolrService.js       | 12 ++++++------
 src/components/views/ViewDoc.js     | 16 ++++++++++++++--
 src/components/views/ViewRelated.js |  7 +++++++
 src/components/views/ViewSubDoc.js  | 27 ++++++++++++++++-----------
 src/index.js                        |  3 ++-
 6 files changed, 65 insertions(+), 22 deletions(-)
 create mode 100644 src/components/views/ViewRelated.js

diff --git a/src/components/Router.js b/src/components/Router.js
index eed2ba8..fb337f9 100644
--- a/src/components/Router.js
+++ b/src/components/Router.js
@@ -15,22 +15,40 @@ const Router = async function (state) {
     const verb = match[1];
     const query = match[2];
     if (verb === '#view/') {
-      const {data, status} = await solrService.get({api: state.config.api}, query);
+      let {data, status} = await solrService.get({api: state.config.api}, query);
       if (status === 200) {
         state.main.doc = data;
+        //Just to avoid extra ajax calls but we can have multiple relationships here
+        if(state.main.doc.record_type_s || state.main.doc.record_type_s === 'Person') {
+          //Removing orcid.org to have better matches
+          state.main.doc.id = state.main.doc.id.replace("https://orcid.org/", "");
+          //state.main.doc.id = encodeURIComponent(state.main.doc.id);
+          //state.main.doc.id = encodeURIComponent(state.main.doc.id);
+          const res = await solrService.search({api: state.config.api}, {
+            start: 0,
+            page: 1,
+            searchParam: 'author_id%3A',
+            text: state.main.doc.id
+          });
+          if (status === 200) {
+            state.main.related = res.data.docs || [];
+          }
+        }
         app.innerHTML = [Header(state), Search(state), ViewDoc(state), Footer(state)].join('');
       } else {
         app.innerHTML = [Header(state), Search(state), ViewError(state), Footer(state)].join('');
       }
     }
     if (verb === '#search/') {
+      //TODO: make this better
       const splits = match[2].split('/');
       const start = splits[0] || state.main.start;
       const page = splits[1] || '';
-      const searchText = splits[2] || '';
+      let searchText = splits[2] || '';
       const {data, status} = await solrService.search({api: state.config.api}, {
         start: start,
         page: page,
+        searchParam: 'main_search%3A',
         text: searchText
       });
       if (status === 200) {
diff --git a/src/components/SolrService.js b/src/components/SolrService.js
index 66c0b00..42d5017 100644
--- a/src/components/SolrService.js
+++ b/src/components/SolrService.js
@@ -28,16 +28,16 @@ const SolrService = {
       return {data: {}, status: e.message};
     }
   },
-  search: async function (config, {start: start, page: page, text: text}) {
+  search: async function (config, {start: start, page: page, searchParam: searchParam, text: text}) {
     try {
-      let param = `select?q=main_search%3A`;
+      let param = `select?q=`;
       //Twice encoded, once for html one for solr
-      let data = encodeURIComponent(`${text}`);
-      data = encodeURIComponent(`${text}`);
+      //let data = encodeURIComponent(`${text}`);
+      //data = encodeURIComponent(`${text}`);
       if (text === '' || !text) {
-        data = '*';
+        text = '*';
       }
-      const req = await axios.get(`${config.api}/${param}${data}&start=${start}&page=${page}`);
+      const req = await axios.get(`${config.api}/${param}${searchParam}${text}&start=${start}&page=${page}`);
       if (req.data) {
         return {data: req.data['response'], status: req.status};
       } else {
diff --git a/src/components/views/ViewDoc.js b/src/components/views/ViewDoc.js
index ee503ae..fc9b3cb 100644
--- a/src/components/views/ViewDoc.js
+++ b/src/components/views/ViewDoc.js
@@ -1,6 +1,7 @@
 const $ = require("jquery");
 const ViewTable = require('./ViewTable');
 const ViewErrorElement = require('./ViewErrorElement');
+const ViewRelated = require('./ViewRelated');
 
 const ViewDoc = function (data) {
 
@@ -11,10 +12,21 @@ const ViewDoc = function (data) {
     const heading = $('<h1>').html(doc.name);
     const desc = $('<p>').html(doc.description);
     const date = $('<p>').html(doc.datePublished);
-
+    const related = $('<div>');
+    if (data.main.related.length > 0) {
+      const relatedInfo = $('<h3>').html('Related Objects');
+      related.append(relatedInfo);
+      const relatedUl = $('<ul>');
+      for (let rel of data.main.related) {
+        const relatedLi = $('<li>');
+        relatedLi.append(ViewRelated(rel));
+        relatedUl.append(relatedLi);
+      }
+      related.append(relatedUl);
+    }
     const fields = ["author"];
 
-    summary.append(heading).append(desc).append(date).append(ViewTable(doc, fields));
+    summary.append(heading).append(desc).append(date).append(ViewTable(doc, fields)).append(related);
     dummy.append(summary);
     return dummy.html();
   } else {
diff --git a/src/components/views/ViewRelated.js b/src/components/views/ViewRelated.js
new file mode 100644
index 0000000..4bafe7d
--- /dev/null
+++ b/src/components/views/ViewRelated.js
@@ -0,0 +1,7 @@
+const $ = require("jquery");
+
+const ViewRelated = function (data) {
+  return $(`<a href="#view/${data.id}">${data.name}</a>`);
+}
+
+module.exports = ViewRelated;
\ No newline at end of file
diff --git a/src/components/views/ViewSubDoc.js b/src/components/views/ViewSubDoc.js
index e79c436..372e369 100644
--- a/src/components/views/ViewSubDoc.js
+++ b/src/components/views/ViewSubDoc.js
@@ -4,19 +4,24 @@ const ViewSubDoc = function (ele) {
 
   try {
     if (Array.isArray(ele)) {
-      ele = ele[0];
       const dummy = $('<div>');
-      const sub = $('<div>');
-      const a = $('<a>');
-      ele = JSON.parse(ele);
-      const href = `/#view/${ele['@id']}`;
-      a.attr('href', href);
-      a.attr('title', ele['name']);
-      a.text(ele['name']);
-      a.addClass("link");
-      sub.append(a);
-      dummy.append(sub);
+      const subDocTitle = $('<div>');
+      subDocTitle.html('Author/s');
+      dummy.append(subDocTitle);
+      for (let el of ele) {
+        const div = $('<div>');
+        const a = $('<a>');
+        const sub = JSON.parse(el);
+        const href = `/#view/${sub['@id']}`;
+        a.attr('href', href);
+        a.attr('title', sub['name']);
+        a.text(sub['name']);
+        a.addClass("link");
+        div.append(a);
+        dummy.append(div);
+      }
       return dummy;
+
     } else {
       return ele;
     }
diff --git a/src/index.js b/src/index.js
index 87bd8c6..ddba3be 100644
--- a/src/index.js
+++ b/src/index.js
@@ -36,7 +36,8 @@ let state = {
     page: 1,
     numFound: 0,
     pageSize: 10,
-    searchText: ''
+    searchText: '',
+    related: []
   },
   config: config
 };
-- 
GitLab