From 428b9ff5277c8c2303bd5f35df965e0ac329c5f0 Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Tue, 15 Jun 2021 22:33:20 +0200
Subject: [PATCH] Wire new search in

---
 scipost_django/scipost/urls.py                |   8 +-
 scipost_django/templates/search/search.html   |  88 +------------
 .../templates/search/search_old.html          | 120 ++++++++++++++++++
 .../templates/search/search_vue.html          |  37 ------
 scipost_vue/components/Search.vue             |  21 ++-
 .../components/SearchableObjectsTable.vue     |   9 +-
 6 files changed, 150 insertions(+), 133 deletions(-)
 create mode 100644 scipost_django/templates/search/search_old.html
 delete mode 100644 scipost_django/templates/search/search_vue.html

diff --git a/scipost_django/scipost/urls.py b/scipost_django/scipost/urls.py
index 1ec6a397c..10b53689f 100644
--- a/scipost_django/scipost/urls.py
+++ b/scipost_django/scipost/urls.py
@@ -53,11 +53,11 @@ urlpatterns = [
     ),
 
     # Search
-    url(r'^search$', views.SearchView.as_view(), name='search'),
+    url(r'^search_old$', views.SearchView.as_view(), name='search_old'),
     url(
-        r'^search_vue$',
-        TemplateView.as_view(template_name='search/search_vue.html'),
-        name='search_vue'
+        r'^search$',
+        TemplateView.as_view(template_name='search/search.html'),
+        name='search'
     ),
     url(r'^$', views.index, name='index'),
     url(r'^files/secure/(?P<path>.*)$', views.protected_serve, name='secure_file'),
diff --git a/scipost_django/templates/search/search.html b/scipost_django/templates/search/search.html
index 33cbb2849..382cda014 100644
--- a/scipost_django/templates/search/search.html
+++ b/scipost_django/templates/search/search.html
@@ -1,7 +1,6 @@
 {% extends 'scipost/base.html' %}
 
 {% load bootstrap %}
-{% load extra_form_widgets %}
 {% load render_bundle from webpack_loader %}
 
 {% block headsup %}
@@ -9,7 +8,8 @@
 {% endblock %}
 
 {% block meta_description %}{{ block.super }} Search{% endblock meta_description %}
-{% block pagetitle %} search{% if form.q.value %}: {{ form.q.value }}{% endif %}{% endblock pagetitle %}
+{% block pagetitle %} search
+{% endblock pagetitle %}
 
 {% block breadcrumb %}
   <nav class="breadcrumb-container">
@@ -30,91 +30,9 @@
     </div>
   </div>
 
-  <div class="row">
-    <div class="col-md-4">
-      <h1>Filter</h1>
-      <form method="get">
-	<div class="form-group">
-          <label for="{{ form.q.auto_id }}">{{ form.q.label }}</label>
-          <input type="text" name="{{ form.q.name }}" class="form-control form-control-lg" id="{{ form.q.auto_id }}" aria-describedby="search_help" placeholder="Search term" value="{{ form.q.value|default:'' }}" required="required">
-          <small id="search_help" class="form-text text-muted">{{ form.q.help_text }}</small>
-	</div>
-
-	<div class="form-group">
-          <label>Type</label>
-	</div>
-	<div class="form-group checkboxes auto-submit">
-          {{ form.models|checkboxes_as_btn }}
-	</div>
-
-	<label>Date from</label>
-	<div class="form-row">
-          {{ form.start }}
-	</div>
-
-	<label>Date until</label>
-	<div class="form-row">
-          {{ form.end }}
-	</div>
-
-	<div class="form-group pt-2">
-
-          <input type="submit" class="btn btn-primary" value="Search">
-          <a href="{% url 'scipost:search' %}" class="btn btn-outline-danger ms-2">Reset</a>
-	</div>
-      </form>
-    </div>
-
-    <div class="col-md-8">
-      <h1 class="my-3 mt-md-0">Search results{% if results_count and query %} ({{ results_count }} found){% endif %}</h1>
-
-      {# without this logic, an request without GET parameters would return *all* objects indexed by Haystack #}
-      {% if query %}
-	<ul class="list-group list-group-flush">
-          {% for result in object_list %}
-            <li class="list-group-item{% if result.content_type == 'journals.publication' %} border-0{% endif %}">
-              {% if result.content_type == 'submissions.submission' %}
-		{% include 'submissions/_search_card.html' with submission=result.object %}
-              {% elif result.content_type == 'commentaries.commentary' %}
-		{% include 'commentaries/_search_card.html' with commentary=result.object %}
-              {% elif result.content_type == 'theses.thesislink' %}
-		{% include 'theses/_search_card.html' with thesislink=result.object %}
-              {% elif result.content_type == 'comments.comment' %}
-		<div class="py-2">
-                  {% include 'comments/_search_card.html' with comment=result.object %}
-		</div>
-              {% elif result.content_type == 'journals.publication' %}
-		<div class="card card-publication">
-                  {% include 'journals/_search_card.html' with publication=result.object %}
-		</div>
-              {% else %}
-		<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
-              {% endif %}
-            </li>
-          {% empty %}
-            <p>Your search query did not return any result.</p>
-          {% endfor %}
-
-          {% if is_paginated %}
-            <p>
-              {% if page_obj.has_previous %}
-		<a href="?q={{ query }}&amp;page={{ page_obj.previous_page_number }}">Previous</a>
-              {% endif %}
-              Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
-              {% if page_obj.has_next %}
-		<a href="?q={{ query }}&amp;page={{ page_obj.next_page_number }}">Next</a>
-              {% endif %}
-            </p>
-          {% endif %}
-	</ul>
-      {% else %}
-	<p>Your search query did not return any result.</p>
-      {% endif %}
-    </div>
-  </div>
-
 {% endblock content %}
 
 {% block footer_script %}
+  {{ request.GET.q|json_script:"json_q" }} <!-- Feed query into search component -->
   {% render_bundle 'vue' 'js' %}
 {% endblock footer_script %}
diff --git a/scipost_django/templates/search/search_old.html b/scipost_django/templates/search/search_old.html
new file mode 100644
index 000000000..33cbb2849
--- /dev/null
+++ b/scipost_django/templates/search/search_old.html
@@ -0,0 +1,120 @@
+{% extends 'scipost/base.html' %}
+
+{% load bootstrap %}
+{% load extra_form_widgets %}
+{% load render_bundle from webpack_loader %}
+
+{% block headsup %}
+  {% render_bundle 'vue' 'css' %}
+{% endblock %}
+
+{% block meta_description %}{{ block.super }} Search{% endblock meta_description %}
+{% block pagetitle %} search{% if form.q.value %}: {{ form.q.value }}{% endif %}{% endblock pagetitle %}
+
+{% block breadcrumb %}
+  <nav class="breadcrumb-container">
+    <div class="container">
+      <ol class="breadcrumb">
+        <li class="breadcrumb-item"><a href="{% url 'scipost:index' %}" >Home</a></li>
+        <li class="breadcrumb-item active" aria-current="page">Search</span>
+      </ol>
+    </div>
+  </nav>
+{% endblock %}
+
+{% block content %}
+
+  <div class="row mb-4">
+    <div id="search">
+      <search></search>
+    </div>
+  </div>
+
+  <div class="row">
+    <div class="col-md-4">
+      <h1>Filter</h1>
+      <form method="get">
+	<div class="form-group">
+          <label for="{{ form.q.auto_id }}">{{ form.q.label }}</label>
+          <input type="text" name="{{ form.q.name }}" class="form-control form-control-lg" id="{{ form.q.auto_id }}" aria-describedby="search_help" placeholder="Search term" value="{{ form.q.value|default:'' }}" required="required">
+          <small id="search_help" class="form-text text-muted">{{ form.q.help_text }}</small>
+	</div>
+
+	<div class="form-group">
+          <label>Type</label>
+	</div>
+	<div class="form-group checkboxes auto-submit">
+          {{ form.models|checkboxes_as_btn }}
+	</div>
+
+	<label>Date from</label>
+	<div class="form-row">
+          {{ form.start }}
+	</div>
+
+	<label>Date until</label>
+	<div class="form-row">
+          {{ form.end }}
+	</div>
+
+	<div class="form-group pt-2">
+
+          <input type="submit" class="btn btn-primary" value="Search">
+          <a href="{% url 'scipost:search' %}" class="btn btn-outline-danger ms-2">Reset</a>
+	</div>
+      </form>
+    </div>
+
+    <div class="col-md-8">
+      <h1 class="my-3 mt-md-0">Search results{% if results_count and query %} ({{ results_count }} found){% endif %}</h1>
+
+      {# without this logic, an request without GET parameters would return *all* objects indexed by Haystack #}
+      {% if query %}
+	<ul class="list-group list-group-flush">
+          {% for result in object_list %}
+            <li class="list-group-item{% if result.content_type == 'journals.publication' %} border-0{% endif %}">
+              {% if result.content_type == 'submissions.submission' %}
+		{% include 'submissions/_search_card.html' with submission=result.object %}
+              {% elif result.content_type == 'commentaries.commentary' %}
+		{% include 'commentaries/_search_card.html' with commentary=result.object %}
+              {% elif result.content_type == 'theses.thesislink' %}
+		{% include 'theses/_search_card.html' with thesislink=result.object %}
+              {% elif result.content_type == 'comments.comment' %}
+		<div class="py-2">
+                  {% include 'comments/_search_card.html' with comment=result.object %}
+		</div>
+              {% elif result.content_type == 'journals.publication' %}
+		<div class="card card-publication">
+                  {% include 'journals/_search_card.html' with publication=result.object %}
+		</div>
+              {% else %}
+		<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
+              {% endif %}
+            </li>
+          {% empty %}
+            <p>Your search query did not return any result.</p>
+          {% endfor %}
+
+          {% if is_paginated %}
+            <p>
+              {% if page_obj.has_previous %}
+		<a href="?q={{ query }}&amp;page={{ page_obj.previous_page_number }}">Previous</a>
+              {% endif %}
+              Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
+              {% if page_obj.has_next %}
+		<a href="?q={{ query }}&amp;page={{ page_obj.next_page_number }}">Next</a>
+              {% endif %}
+            </p>
+          {% endif %}
+	</ul>
+      {% else %}
+	<p>Your search query did not return any result.</p>
+      {% endif %}
+    </div>
+  </div>
+
+{% endblock content %}
+
+{% block footer_script %}
+  {% render_bundle 'vue' 'js' %}
+{% endblock footer_script %}
diff --git a/scipost_django/templates/search/search_vue.html b/scipost_django/templates/search/search_vue.html
deleted file mode 100644
index a519c6ba7..000000000
--- a/scipost_django/templates/search/search_vue.html
+++ /dev/null
@@ -1,37 +0,0 @@
-{% extends 'scipost/base.html' %}
-
-{% load bootstrap %}
-{% load render_bundle from webpack_loader %}
-
-{% block headsup %}
-  {% render_bundle 'vue' 'css' %}
-{% endblock %}
-
-{% block meta_description %}{{ block.super }} Search{% endblock meta_description %}
-{% block pagetitle %} search
-{% endblock pagetitle %}
-
-{% block breadcrumb %}
-  <nav class="breadcrumb-container">
-    <div class="container">
-      <ol class="breadcrumb">
-        <li class="breadcrumb-item"><a href="{% url 'scipost:index' %}" >Home</a></li>
-        <li class="breadcrumb-item active" aria-current="page">Search</span>
-      </ol>
-    </div>
-  </nav>
-{% endblock %}
-
-{% block content %}
-
-  <div class="row mb-4">
-    <div id="search">
-      <search></search>
-    </div>
-  </div>
-
-{% endblock content %}
-
-{% block footer_script %}
-  {% render_bundle 'vue' 'js' %}
-{% endblock footer_script %}
diff --git a/scipost_vue/components/Search.vue b/scipost_vue/components/Search.vue
index 38c7e5c1a..cb71cad7a 100644
--- a/scipost_vue/components/Search.vue
+++ b/scipost_vue/components/Search.vue
@@ -178,10 +178,12 @@
       role="tabpanel"
       aria-labelledby="publications-tab"
       >
+      Initial query: {{ initialQuery }}
       <searchable-objects-table
 	:object_type="'publication'"
 	:displayfields="[{ field: 'doi_label', label: 'DOI label' }, { field: 'url', label: 'URL' }]"
 	:url="'publications'"
+	:initial_filter="initialQuery"
 	>
       </searchable-objects-table>
     </div>
@@ -203,6 +205,8 @@
 </template>
 
 <script>
+import { ref, onMounted } from '@vue/composition-api'
+
 import SearchableObjectsTable from './SearchableObjectsTable.vue'
 
 export default {
@@ -210,7 +214,22 @@ export default {
     components: {
 	SearchableObjectsTable,
     },
-    setup() {
+    props: {
+        initial_filter: {
+            type: String,
+            required: false
+        },
+    },
+    setup(props, context) {
+	const initialQuery = ref('')
+
+	onMounted( () => {
+	    initialQuery.value = JSON.parse(document.getElementById('json_q').textContent)
+	})
+
+	return {
+	    initialQuery
+	}
     },
 }
 </script>
diff --git a/scipost_vue/components/SearchableObjectsTable.vue b/scipost_vue/components/SearchableObjectsTable.vue
index a92af43ce..6b9e48250 100644
--- a/scipost_vue/components/SearchableObjectsTable.vue
+++ b/scipost_vue/components/SearchableObjectsTable.vue
@@ -319,7 +319,7 @@
 const headers = new Headers();
 headers.append('Accept', 'application/json; version=0')
 
-import { ref, computed, watch, onMounted } from '@vue/composition-api'
+import { ref, toRef, computed, watch, onMounted } from '@vue/composition-api'
 
 import ObjectRowDetails from './ObjectRowDetails/ObjectRowDetails.vue'
 
@@ -350,13 +350,13 @@ export default {
         },
         initial_filter: {
             type: String,
-            required: false
+            default: ''
         },
     },
     setup(props) {
 	const uId = ref(uniqueId())
 	const advancedSearchIsOn = ref(false)
-	const basicSearchQuery = ref('')
+	const basicSearchQuery = toRef(props, 'initial_filter')
 	const newQueryField = ref(null)
 	const allowedLookups = ref([])
 	const newQueryLookup = ref('')
@@ -479,9 +479,6 @@ export default {
 		    }
 		})
 	    }
-            if (props.initial_filter) {
-                parameters += ('&' + props.initial_filter)
-            }
 	    return parameters
 	})
 
-- 
GitLab