From fa2327bbce9e3964ab085840910f511cd1f97a48 Mon Sep 17 00:00:00 2001
From: Simon Mayer <simon.mayer@onb.ac.at>
Date: Thu, 13 Oct 2022 18:16:04 +0200
Subject: [PATCH] WIP: Bugfix for paginating datasets

---
 app/controllers/dataset_controller.rb                  | 2 ++
 app/javascript/packs/controllers/dataset_controller.js | 9 +++++----
 app/javascript/packs/controllers/viewer_controller.js  | 2 +-
 app/views/dataset/show.html.erb                        | 5 +++--
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/app/controllers/dataset_controller.rb b/app/controllers/dataset_controller.rb
index e3d41a1..2478c32 100644
--- a/app/controllers/dataset_controller.rb
+++ b/app/controllers/dataset_controller.rb
@@ -15,6 +15,7 @@ class DatasetController < ApplicationController
     @dataset = Dataset.find(params[:id])
     @current_page = params[:page] || 1
     @per_page = params[:per_page] || 10
+    @nb_pages = params[:nb_pages] || (@dataset.nb_articles / @per_page).to_i + 1
     session[:working_dataset] = @dataset.id
     # puts "The session now has working_dataset " + String(session[:working_dataset])
   end
@@ -204,6 +205,7 @@ class DatasetController < ApplicationController
     out[:pagination] = render_to_string(layout: false,
                                         partial: "pagination",
                                         locals: { nb_pages: res[:nb_pages].to_i, current_page: params[:page].to_i })
+    out[:nb_pages] = res[:nb_pages]
     render json: out
   end
 
diff --git a/app/javascript/packs/controllers/dataset_controller.js b/app/javascript/packs/controllers/dataset_controller.js
index e9aa9f5..914b258 100644
--- a/app/javascript/packs/controllers/dataset_controller.js
+++ b/app/javascript/packs/controllers/dataset_controller.js
@@ -4,7 +4,7 @@ import {SearchAPI} from "../utils/search_api";
 
 export default class extends Controller {
     static targets = [ ]
-    static values = { id: Number, currentPage: Number, perPage: Number, sort: String, sortOrder: String, selectedDocuments: Array }
+    static values = { id: Number, currentPage: Number, nbPages: Number, perPage: Number, sort: String, sortOrder: String, selectedDocuments: Array }
 
     connect() {
         this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, "article")
@@ -67,6 +67,7 @@ export default class extends Controller {
         DatasetAPI.paginateDataset(datasetId, page, per_page, sort, sort_order, type, (data) => {
             $("#documents-list").html(data.documents)
             $("#results_navigation").html(data.pagination)
+            console.log(data.nb_pages)
         })
     }
 
@@ -80,7 +81,7 @@ export default class extends Controller {
         event.preventDefault()
         if (this.currentPageValue > 1) {
             this.currentPageValue--
-            this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, "all")
+            this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, $("#doctype_selection input:checked").data("doctype"))
         }
     }
 
@@ -88,14 +89,14 @@ export default class extends Controller {
         event.preventDefault()
         if (this.currentPageValue < this.nbPagesValue) {
             this.currentPageValue++
-            this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, "all")
+            this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, $("#doctype_selection input:checked").data("doctype"))
         }
     }
 
     page_button(event) {
         event.preventDefault()
         this.currentPageValue = event.target.textContent
-        this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, "all")
+        this.loadDocuments(this.idValue, this.currentPageValue, this.perPageValue, this.sortValue, this.sortOrderValue, $("#doctype_selection input:checked").data("doctype"))
     }
 
 }
\ No newline at end of file
diff --git a/app/javascript/packs/controllers/viewer_controller.js b/app/javascript/packs/controllers/viewer_controller.js
index 62a64df..42888d4 100644
--- a/app/javascript/packs/controllers/viewer_controller.js
+++ b/app/javascript/packs/controllers/viewer_controller.js
@@ -5,7 +5,7 @@ import Sortable from 'sortablejs'
 
 export default class extends Controller {
     static targets = ['currentPage', 'articleOverlay', 'selectedArticlePanel', 'addArticleButton', 'addCompoundArticleButton', 'compoundArticlePanel']
-    static values = {currentPage: Number, nbpages: Number, pages: Array, articles: Array, selectedArticles: Array, issueId: String, compoundMode: Boolean}
+    static values = {currentPage: Number, nbPages: Number, pages: Array, articles: Array, selectedArticles: Array, issueId: String, compoundMode: Boolean}
 
     isDragged = false
     viewer = null
diff --git a/app/views/dataset/show.html.erb b/app/views/dataset/show.html.erb
index 442dc6b..23bbb1d 100644
--- a/app/views/dataset/show.html.erb
+++ b/app/views/dataset/show.html.erb
@@ -3,8 +3,9 @@
     <div class="row"
          data-controller="dataset"
          data-dataset-id-value="<%= @dataset.id %>"
-         data-dataset-current-page-value="1"
-         data-dataset-per-page-value="10"
+         data-dataset-current-page-value="<%= @current_page %>"
+         data-dataset-nb-pages-value="<%= @nb_pages %>"
+         data-dataset-per-page-value="<%= @per_page %>"
          data-dataset-sort-value="default"
          data-dataset-sort-order-value="asc"
          data-dataset-selected-documents-value="">
-- 
GitLab