From 4a8582ebc8bee1bc7fe224d1ed653312edf880b7 Mon Sep 17 00:00:00 2001 From: artoscinote <85488244+artoscinote@users.noreply.github.com> Date: Tue, 11 Jul 2023 09:27:15 +0200 Subject: [PATCH] Initial implementation of protocol processing modal [SCI-8661] (#5683) * Initial implementation of protocol processing modal [SCI-8661] * Prepare placeholder jobs and integrate notifications [SCI-8661] --- app/assets/javascripts/protocols/index.js | 45 ++++---- app/controllers/protocols_controller.rb | 30 +---- .../packs/vue/protocol_file_import_modal.js | 18 +++ .../vue/protocol_import/file_import_modal.vue | 105 ++++++++++++++++++ app/jobs/protocols/docx_import_job.rb | 9 ++ .../protocol_importers/docx_service.rb | 43 +++++++ app/views/protocols/index.html.erb | 5 + .../protocols/index/_general_toolbar.html.erb | 2 +- config/locales/en.yml | 18 ++- config/routes.rb | 1 + config/webpack/webpack.config.js | 3 +- 11 files changed, 231 insertions(+), 48 deletions(-) create mode 100644 app/javascript/packs/vue/protocol_file_import_modal.js create mode 100644 app/javascript/vue/protocol_import/file_import_modal.vue create mode 100644 app/jobs/protocols/docx_import_job.rb create mode 100644 app/services/protocol_importers/docx_service.rb diff --git a/app/assets/javascripts/protocols/index.js b/app/assets/javascripts/protocols/index.js index 444148cf8..2d6bf7cfd 100644 --- a/app/assets/javascripts/protocols/index.js +++ b/app/assets/javascripts/protocols/index.js @@ -667,28 +667,33 @@ var ProtocolsIndex = (function() { var importUrl = fileInput.attr('data-import-url'); var teamId = fileInput.attr('data-team-id'); var type = fileInput.attr('data-type'); - importProtocolFromFile( - ev.target.files[0], - importUrl, - { team_id: teamId, type: type }, - false, - function(datas) { - var nrSuccessful = 0; - _.each(datas, function(data) { - if (data.status === 'ok') { - nrSuccessful += 1; - } - }); - animateSpinner(null, false); - if (nrSuccessful) { - HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_ok_html', { count: nrSuccessful }), 'success'); - reloadTable(); - } else { - HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_failed'), 'danger'); + if(ev.target.files[0].name.split('.').pop() === 'eln') { + importProtocolFromFile( + ev.target.files[0], + importUrl, + { team_id: teamId, type: type }, + false, + function(datas) { + var nrSuccessful = 0; + _.each(datas, function(data) { + if (data.status === 'ok') { + nrSuccessful += 1; + } + }); + animateSpinner(null, false); + + if (nrSuccessful) { + HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_ok_html', { count: nrSuccessful }), 'success'); + reloadTable(); + } else { + HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_failed'), 'danger'); + } } - } - ); + ); + } else { + protocolFileImportModal.init(ev.target.files, reloadTable); + } $(this).val(''); }); } diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 7cab925e7..3dbdc0c09 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -591,7 +591,6 @@ class ProtocolsController < ApplicationController message_items: { protocol: protocol.id }) - generate_import_protocol_notification(current_user, protocol) format.json do render json: { status: :ok }, status: :ok end @@ -918,6 +917,11 @@ class ProtocolsController < ApplicationController } end + def import_docx + @job = Protocols::DocxImportJob.perform_later(params[:files], current_user) + render json: { job_id: @job.job_id } + end + private def set_importer @@ -929,30 +933,6 @@ class ProtocolsController < ApplicationController end end - def generate_import_protocol_notification(user, protocol) - protocol_download_link = "" \ - "#{export_protocol_file_name([protocol])}" - - notification = Notification.create( - type_of: :deliver, - title: I18n.t('protocols.import_export.import_protocol_notification.title', link: protocol_download_link), - message: "#{I18n.t('protocols.import_export.import_protocol_notification.message')} " \ - "#{protocol.name}" - ) - - UserNotification.create(notification: notification, user: user) - end - def export_protocol_file_name(protocols) protocol_name = get_protocol_name(protocols[0]) diff --git a/app/javascript/packs/vue/protocol_file_import_modal.js b/app/javascript/packs/vue/protocol_file_import_modal.js new file mode 100644 index 000000000..8e7acc245 --- /dev/null +++ b/app/javascript/packs/vue/protocol_file_import_modal.js @@ -0,0 +1,18 @@ +import TurbolinksAdapter from 'vue-turbolinks'; +import Vue from 'vue/dist/vue.esm'; +import ProtocolFileImportModal from '../../vue/protocol_import/file_import_modal.vue'; + + +Vue.use(TurbolinksAdapter); +Vue.prototype.i18n = window.I18n; + +window.initProtocolFileImportModalComponent = () => { + new Vue({ + el: '#protocolFileImportModal', + components: { + 'protocol-file-import-modal': ProtocolFileImportModal + } + }); +}; + +initProtocolFileImportModalComponent(); diff --git a/app/javascript/vue/protocol_import/file_import_modal.vue b/app/javascript/vue/protocol_import/file_import_modal.vue new file mode 100644 index 000000000..28f3f8324 --- /dev/null +++ b/app/javascript/vue/protocol_import/file_import_modal.vue @@ -0,0 +1,105 @@ + + + diff --git a/app/jobs/protocols/docx_import_job.rb b/app/jobs/protocols/docx_import_job.rb new file mode 100644 index 000000000..730457d10 --- /dev/null +++ b/app/jobs/protocols/docx_import_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Protocols + class DocxImportJob < ApplicationJob + def perform(files, user) + ProtocolImporters::DocxService.new(files, user).import! + end + end +end diff --git a/app/services/protocol_importers/docx_service.rb b/app/services/protocol_importers/docx_service.rb new file mode 100644 index 000000000..c6ce4dedd --- /dev/null +++ b/app/services/protocol_importers/docx_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module ProtocolImporters + class DocxService + def initialize(files, user) + @files = files + @user = user + end + + def import! + # TODO: Implement actual logic + ActiveRecord::Base.transaction do + @protocol = @user.current_team + .protocols + .create!( + name: "PARSED PROTOCOL #{rand * 10000}", + protocol_type: :in_repository_draft, + added_by: @user + ) + create_notification! + end + end + + def create_notification! + # TODO: Add proper protocol original file link + protocol_download_link = "" \ + "#{@protocol.name}" + + notification = Notification.create( + type_of: :deliver, + title: I18n.t('protocols.import_export.import_protocol_notification.title', link: protocol_download_link), + message: "#{I18n.t('protocols.import_export.import_protocol_notification.message')} " \ + "" \ + "#{@protocol.name}" + ) + + UserNotification.create(notification: notification, user: @user) + end + end +end diff --git a/app/views/protocols/index.html.erb b/app/views/protocols/index.html.erb index 49a0c80e0..255ef06d6 100644 --- a/app/views/protocols/index.html.erb +++ b/app/views/protocols/index.html.erb @@ -34,6 +34,11 @@ <% end %> +
+ +
+<%= javascript_include_tag 'vue_protocol_file_import_modal' %> +
<%= render partial: "protocols/index/general_toolbar.html.erb" %> <%= render partial: "protocols/index/protocol_filters.html.erb" %> diff --git a/app/views/protocols/index/_general_toolbar.html.erb b/app/views/protocols/index/_general_toolbar.html.erb index 927929f9c..afc6504b8 100644 --- a/app/views/protocols/index/_general_toolbar.html.erb +++ b/app/views/protocols/index/_general_toolbar.html.erb @@ -22,7 +22,7 @@
  • > <%= t("protocols.index.import_alt") %> -
  • diff --git a/config/locales/en.yml b/config/locales/en.yml index dff1c6dc8..0e2495537 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2684,6 +2684,22 @@ en: button: "Rearrange steps" modal: title: "Rearrange protocol steps" + import_modal: + confirm: + title: "Import protocol" + body_html: "You will start importing selected .docx file into SciNote. The import time may vary according to the size of a file and internet connection." + in_progress: + title: "Importing in progress" + body_html: "The process of importing has started. Please wait..." + not_yet_done: + title: "Importing in progress" + body_html: "The import is taking longer than expected. You'll get a notice in Notifications when your protocol template is ready. You can close the window and continue with your work." + done: + title: "Importing completed" + body_html: 'The protocol has been successfully imported. You can access the draft of protocol template here.' + import: "Import" + cancel: "Cancel" + close: "Close" print: title: "Print protocol" button: "Print" @@ -2866,7 +2882,7 @@ en: edit: "Edit" clone_btn: "Copy" import: "Import" - import_alt: "From local file" + import_alt: "From file (.docx, .eln)" import_protocols_io: "From Protocols.io" modal_import_json_upload: "Upload" modal_import_json_title: "Import protocols.io file" diff --git a/config/routes.rb b/config/routes.rb index 261d9c35d..391af47a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -654,6 +654,7 @@ Rails.application.routes.draw do post 'restore', to: 'protocols#restore' post 'clone', to: 'protocols#clone' post 'import', to: 'protocols#import' + post 'import_docx', to: 'protocols#import_docx' post 'protocolsio_import_create', to: 'protocols#protocolsio_import_create' post 'protocolsio_import_save', to: 'protocols#protocolsio_import_save' diff --git a/config/webpack/webpack.config.js b/config/webpack/webpack.config.js index b2bbee377..afb60383f 100644 --- a/config/webpack/webpack.config.js +++ b/config/webpack/webpack.config.js @@ -34,7 +34,8 @@ const entryList = { vue_repository_assign_items_to_task_modal: './app/javascript/packs/vue/assign_items_to_task_modal.js', vue_navigation_top_menu: './app/javascript/packs/vue/navigation/top_menu.js', vue_navigation_navigator: './app/javascript/packs/vue/navigation/navigator.js', - vue_components_action_toolbar: './app/javascript/packs/vue/action_toolbar.js' + vue_components_action_toolbar: './app/javascript/packs/vue/action_toolbar.js', + vue_protocol_file_import_modal: './app/javascript/packs/vue/protocol_file_import_modal.js' } // Engine pack loading based on https://github.com/rails/webpacker/issues/348#issuecomment-635480949