From 75f4f983f3fc3f93f6e6cc6b51f9882689c68b53 Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Thu, 10 Mar 2022 20:50:08 +0100
Subject: [PATCH] add delete page and rotate page feature

---
 .../viewer/action_buttons/index.hbs           | 14 +++++++++
 app/components/viewer/index.hbs               |  2 ++
 app/components/viewer/index.js                | 26 ++++++++++++++++
 app/components/viewer/thumbnail/index.js      |  4 +++
 app/services/requests.js                      | 31 +++++++++++++++++--
 5 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/app/components/viewer/action_buttons/index.hbs b/app/components/viewer/action_buttons/index.hbs
index 8ca8a63..7768236 100644
--- a/app/components/viewer/action_buttons/index.hbs
+++ b/app/components/viewer/action_buttons/index.hbs
@@ -20,6 +20,20 @@
       </button>
     {{/if}}
   {{#if this.is_any_page_selected }}
+    <button
+      class="btn btn-light"
+      type="button"
+      {{on "click" @onRotateClockwise}} >
+      <i class="bi bi-arrow-clockwise"></i>
+      Rotate
+    </button>
+    <button
+      class="btn btn-light"
+      type="button"
+      {{on "click" @onRotateCounterclockwise}} >
+      <i class="bi bi-arrow-counterclockwise"></i>
+      Rotate
+    </button>
     <button
       class="btn btn-danger mx-5"
       type="button"
diff --git a/app/components/viewer/index.hbs b/app/components/viewer/index.hbs
index 33b1f8b..9a506cd 100644
--- a/app/components/viewer/index.hbs
+++ b/app/components/viewer/index.hbs
@@ -6,6 +6,8 @@
       @ocrStatus={{this.ocrStatus}}
       @selectedPages={{this.selected_pages}}
       @openConfirmDeletionModal={{this.openConfirmDeletionModal}}
+      @onRotateClockwise={{this.onRotateClockwise}}
+      @onRotateCounterclockwise={{this.onRotateCounterclockwise}}
       @onRunOCR={{this.onRunOCR}} />
 
     <Viewer::ActionModes
diff --git a/app/components/viewer/index.js b/app/components/viewer/index.js
index d53b6af..e821fd5 100644
--- a/app/components/viewer/index.js
+++ b/app/components/viewer/index.js
@@ -104,6 +104,32 @@ export default class ViewerComponent extends Component {
     });
   }
 
+  @action
+  async onRotateClockwise() {
+    let page_ids = [];
+
+    page_ids = this.selected_pages.map(page => page.id);
+    await this.requests.rotatePages({
+      page_ids: page_ids,
+      angle: 90
+    });
+    this.selected_pages = A([]);
+    this.router.refresh();
+  }
+
+  @action
+  async onRotateCounterclockwise() {
+    let page_ids = [];
+
+    page_ids = this.selected_pages.map(page => page.id);
+    await this.requests.rotatePages({
+      page_ids: page_ids,
+      angle: 270
+    });
+    this.selected_pages = A([]);
+    this.router.refresh();
+  }
+
   @action
   onNodeClicked() {
   }
diff --git a/app/components/viewer/thumbnail/index.js b/app/components/viewer/thumbnail/index.js
index 57474a2..c4d9bb0 100644
--- a/app/components/viewer/thumbnail/index.js
+++ b/app/components/viewer/thumbnail/index.js
@@ -37,4 +37,8 @@ export default class ViewerThumbnailComponent extends Component {
 
     return false;
   }
+
+  set is_selected(value) {
+
+  }
 }
\ No newline at end of file
diff --git a/app/services/requests.js b/app/services/requests.js
index 2c4ea02..ef82def 100644
--- a/app/services/requests.js
+++ b/app/services/requests.js
@@ -37,6 +37,15 @@ export default class Requests extends Service {
     return this._delete('/pages/', {'pages': page_ids});
   }
 
+  async rotatePages({page_ids, angle}) {
+    let pages = [];
+
+    pages = page_ids.map(page_id => {
+      return {id: page_id, angle: angle};
+    });
+    return this._post('/pages/rotate/', {'pages': pages});
+  }
+
   /**
   *  `document_version` contains following attributes:
   *    id
@@ -154,7 +163,25 @@ export default class Requests extends Service {
     });
   }
 
+  async _post(url, data) {
+    return this._generic({
+      method: 'POST',
+      url: url,
+      data: data,
+      content_type: 'application/json'
+    });
+  }
+
   async _delete(url, data) {
+    return this._generic({
+      method: 'DELETE',
+      url: url,
+      data: data,
+      content_type: 'application/json'
+    });
+  }
+
+  async _generic({method, url, data, content_type}) {
     let url_with_base,
       body_data = '',
       headers_copy = {};
@@ -162,14 +189,14 @@ export default class Requests extends Service {
     url_with_base = `${base_url()}${url}`;
 
     Object.assign(headers_copy, this.headers);
-    headers_copy['Content-Type'] = 'application/json';
+    headers_copy['Content-Type'] = content_type;
 
     if (data) {
       body_data = JSON.stringify(data);
     }
 
     return fetch(url_with_base, {
-      method: 'DELETE',
+      method: method,
       headers: headers_copy,
       body: body_data,
     });
-- 
GitLab