From 43571b5692090245df6c745948f261af859a7f8f Mon Sep 17 00:00:00 2001 From: aignatov-bio <47317017+aignatov-bio@users.noreply.github.com> Date: Mon, 2 Aug 2021 15:38:37 +0200 Subject: [PATCH] Print labels changes [SCI-5943] (#3461) --- .../javascripts/label_printers/index.js | 16 +++ app/assets/stylesheets/settings/addons.scss | 4 + .../stylesheets/settings/label_printers.scss | 26 +++++ app/controllers/label_printers_controller.rb | 31 ++++-- .../settings/account/addons_controller.rb | 2 +- app/permissions/organization.rb | 4 + app/views/label_printers/edit.html.erb | 60 ----------- app/views/label_printers/index.html.erb | 102 ++++++++++++++++++ .../settings/account/addons/index.html.erb | 49 ++++----- .../account/addons/label_printer.html.erb | 63 ----------- config/initializers/assets.rb | 1 + config/locales/en.yml | 13 +++ 12 files changed, 209 insertions(+), 162 deletions(-) create mode 100644 app/assets/javascripts/label_printers/index.js delete mode 100644 app/views/label_printers/edit.html.erb create mode 100644 app/views/label_printers/index.html.erb delete mode 100644 app/views/users/settings/account/addons/label_printer.html.erb diff --git a/app/assets/javascripts/label_printers/index.js b/app/assets/javascripts/label_printers/index.js new file mode 100644 index 000000000..970c5b524 --- /dev/null +++ b/app/assets/javascripts/label_printers/index.js @@ -0,0 +1,16 @@ +(function() { + $('.api-key-input').on('keyup change', function() { + var initialValue = this.dataset.originalValue; + if (initialValue.length) { + if (initialValue !== this.value) { + $('.api-key-container').addClass('warning'); + $('.save-button').removeClass('hidden'); + $('.saved-button').addClass('hidden'); + } else { + $('.api-key-container').removeClass('warning'); + $('.save-button').addClass('hidden'); + $('.saved-button').removeClass('hidden'); + } + } + }); +}()); diff --git a/app/assets/stylesheets/settings/addons.scss b/app/assets/stylesheets/settings/addons.scss index 84731c79f..c9260ae32 100644 --- a/app/assets/stylesheets/settings/addons.scss +++ b/app/assets/stylesheets/settings/addons.scss @@ -26,6 +26,10 @@ font-weight: bold; } + .control { + margin-left: auto; + } + .status { border: $border-default; color: $color-silver-chalice; diff --git a/app/assets/stylesheets/settings/label_printers.scss b/app/assets/stylesheets/settings/label_printers.scss index e53c0e3e9..db05698ac 100644 --- a/app/assets/stylesheets/settings/label_printers.scss +++ b/app/assets/stylesheets/settings/label_printers.scss @@ -30,6 +30,10 @@ } } + .collapse { + margin-bottom: 1em; + } + .fa-caret-down { cursor: pointer; margin-right: .25em; @@ -55,9 +59,31 @@ .api-key-container { display: flex; + position: relative; + + &.warning { + &::after { + color: $brand-primary-light; + content: attr(data-warning); + left: 0; + position: absolute; + bottom: -1.5em; + } + } .api-key-input { margin-right: .5em; + + + } + + .btn { + margin-left: .5em; + margin-top: 23px; } } + + .update-printers { + margin-left: auto; + } } diff --git a/app/controllers/label_printers_controller.rb b/app/controllers/label_printers_controller.rb index 5db443621..2bfe78332 100644 --- a/app/controllers/label_printers_controller.rb +++ b/app/controllers/label_printers_controller.rb @@ -3,10 +3,12 @@ class LabelPrintersController < ApplicationController include InputSanitizeHelper + before_action :check_manage_permissions, except: :index before_action :find_label_printer, only: %i(edit update destroy) def index @label_printers = LabelPrinter.all + @fluics_api_key = @label_printers.any? ? @label_printers.first.fluics_api_key : nil end def new @@ -59,23 +61,34 @@ class LabelPrintersController < ApplicationController def create_fluics # Placeholder for FLUICS printer management + begin + printers = LabelPrinters::Fluics::ApiClient.new(label_printer_params[:fluics_api_key]).list - LabelPrinters::Fluics::ApiClient.new(params[:fluics_api_key]).list.each do |fluics_printer| - label_printer = LabelPrinter.find_or_initialize_by( - fluics_api_key: params[:fluics_api_key], - fluics_lid: fluics_printer['LID'], - type_of: :fluics, - language_type: :zpl - ) + LabelPrinter.destroy_all - label_printer.update(name: fluics_printer['serviceName']) + printers.each do |fluics_printer| + label_printer = LabelPrinter.find_or_initialize_by( + fluics_api_key: label_printer_params[:fluics_api_key], + fluics_lid: fluics_printer['LID'], + type_of: :fluics, + language_type: :zpl + ) + + label_printer.update(name: fluics_printer['serviceName']) + end + rescue LabelPrinters::Fluics::ApiClient::BadRequestError + flash[:error] = t('users.settings.account.label_printer.api_key_error') end - redirect_to addons_path + redirect_to label_printers_path end private + def check_manage_permissions + render_403 unless can_manage_label_printers? + end + def label_printer_params params.require(:label_printer).permit( :name, :type_of, :fluics_api_key, :host, :port diff --git a/app/controllers/users/settings/account/addons_controller.rb b/app/controllers/users/settings/account/addons_controller.rb index 0ac76ba2e..f2647f884 100644 --- a/app/controllers/users/settings/account/addons_controller.rb +++ b/app/controllers/users/settings/account/addons_controller.rb @@ -5,7 +5,7 @@ module Users layout 'fluid' def index - @label_printers = LabelPrinter.all + @label_printer_any = LabelPrinter.any? end end end diff --git a/app/permissions/organization.rb b/app/permissions/organization.rb index 47e7891b9..14ccac47b 100644 --- a/app/permissions/organization.rb +++ b/app/permissions/organization.rb @@ -8,5 +8,9 @@ module Organization can :create_teams do |_| true end + + can :manage_label_printers do |_| + true + end end end diff --git a/app/views/label_printers/edit.html.erb b/app/views/label_printers/edit.html.erb deleted file mode 100644 index d4d8cfb3c..000000000 --- a/app/views/label_printers/edit.html.erb +++ /dev/null @@ -1,60 +0,0 @@ -<% provide(:head_title, t('users.settings.account.addons.head_title')) %> -<% provide(:container_class, "no-second-nav-container") %> - -<%= render partial: 'users/settings/sidebar.html.erb' %> - -