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' %> - -
-
- -
-

- <%= t("users.settings.account.label_printer.#{@label_printer.type_of}_printer") %> -

-
- <% if @label_printer.ready? %> - <%= t('users.settings.account.addons.printers.ready') %> - <% else %> - <%= t('users.settings.account.addons.printers.not_ready') %> - <% end %> -
-
-
- - -
diff --git a/app/views/label_printers/index.html.erb b/app/views/label_printers/index.html.erb new file mode 100644 index 000000000..80c265f3d --- /dev/null +++ b/app/views/label_printers/index.html.erb @@ -0,0 +1,102 @@ +<% provide(:head_title, t('users.settings.account.addons.head_title')) %> +<% provide(:container_class, "no-second-nav-container") %> + +<%= render partial: 'users/settings/sidebar.html.erb' %> + +
+
+ +
+

+ <%= t("users.settings.account.label_printer.fluics_printer") %> +

+
+
+ + +
+<%= javascript_include_tag "label_printers/index" %> diff --git a/app/views/users/settings/account/addons/index.html.erb b/app/views/users/settings/account/addons/index.html.erb index bc9290748..dda6df42e 100644 --- a/app/views/users/settings/account/addons/index.html.erb +++ b/app/views/users/settings/account/addons/index.html.erb @@ -18,39 +18,30 @@

<%= t('users.settings.account.addons.label_printers') %>

- <% if @label_printers.none? %> - <%= form_with url: create_fluics_label_printers_path, method: :post do |f| %> -

FLUICS Printer Management Placeholder

- <%= f.text_field :fluics_api_key %> - <%= f.submit "Fetch FLUICS Printers" %> - <% end %> - <% end %> - <% @label_printers.each do |label_printer| %> -
-
-
-
- <%= t('users.settings.account.addons.fluics_printer.title') %> -
-
- <% if label_printer.ready? %> - <%= t('users.settings.account.addons.printers.ready') %> - <% else %> - <%= t('users.settings.account.addons.printers.not_ready') %> - <% end %> -
-
- <%= t('users.settings.account.addons.printers.enabled') %> - -
+
+
+
+
+ <%= t('users.settings.account.addons.fluics_printer.title') %>
-
- <%= t('users.settings.account.addons.fluics_printer.description') %> +
+ <%= t('users.settings.account.addons.printers.enabled') %> +
- <%= link_to t('users.settings.account.addons.printers.printer_details'), edit_label_printer_path(label_printer), class: 'printer-details' %>
+
+ <%= t('users.settings.account.addons.fluics_printer.description') %> +
+ <% if !@label_printer_any && can_manage_label_printers? %> + <%= link_to label_printers_path(), class: 'printer-details btn btn-primary' do %> + + <%= t('users.settings.account.addons.printers.enter_api_key') %> + <% end %> + <% else %> + <%= link_to t('users.settings.account.addons.printers.printer_details'), label_printers_path(), class: 'printer-details' %> + <% end %>
- <% end %> +
diff --git a/app/views/users/settings/account/addons/label_printer.html.erb b/app/views/users/settings/account/addons/label_printer.html.erb deleted file mode 100644 index 21d331299..000000000 --- a/app/views/users/settings/account/addons/label_printer.html.erb +++ /dev/null @@ -1,63 +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' %> - -
-
- -
-

- <%= t("users.settings.account.label_printer.#{@printer[:printer_type]}_printer") %> -

-
- <% if @printer[:ready] %> - <%= t('users.settings.account.addons.printers.ready') %> - <% else %> - <%= t('users.settings.account.addons.printers.not_ready') %> - <% end %> -
-
-
- -
    -
  • -
    - -
    <%= t("users.settings.account.label_printer.instructions") %>
    -
    -
      - Instruction here -
    -
  • -
  • -
    - -
    <%= t("users.settings.account.label_printer.settings") %>
    -
    -
      - <% if @printer[:printer_type] == :fluics %> - <%= form_with url: "" do |form| %> -
      - <%= form.label :api_key, t("users.settings.account.label_printer.api_key_label") %> -
      - <%= form.text_field :api_key, class: "sci-input-field api-key-input" %> - <%= form.submit t('general.save'), class: "btn btn-secondary" %> -
      -
      - <%= link_to t("users.settings.account.label_printer.api_key_information"), '' %> - <% end %> - <% end %> -
    -
  • -
- - -
- diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index c2d256238..328dc32d1 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -76,6 +76,7 @@ Rails.application.config.assets.precompile += %w(system_notifications/index.js) Rails.application.config.assets.precompile += %w(users/invite_users_modal.js) Rails.application.config.assets.precompile += %w(highlightjs-github-theme.css) Rails.application.config.assets.precompile += %w(search.js) +Rails.application.config.assets.precompile += %w(label_printers/index.js) Rails.application.config.assets.precompile += %w(repositories/index.js) Rails.application.config.assets.precompile += %w(repositories/edit.js) Rails.application.config.assets.precompile += %w(repositories/repository_datatable.js) diff --git a/config/locales/en.yml b/config/locales/en.yml index 71ffab2ee..5d0d5c1ca 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1835,6 +1835,7 @@ en: ready: "Ready" not_ready: "Not ready" printer_details: "Printer details" + enter_api_key: "Enter API key" enabled: "Enabled" fluics_printer: title: "FLUICS Printer" @@ -1842,9 +1843,21 @@ en: label_printer: fluics_printer: "Label printer: FLUICS Printer" instructions: "Instructions" + fluics_instruction: + p1: "To make it work, please follow these steps:" + p2: "1. Open the FLUICS Connect web app or install an iOS / Android mobile app." + p3: "2. Sign up for a free account. Follow the instructions to set up your workspace." + p4: "3. Upgrade to a Pro Workspace to get your API key. Read more on how to get an API key." + p5: "4. Enter the API key into the field below and start printing your own labels." settings: "Settings" api_key_label: "API key" + api_key_warning: "API key change will lead to printer list refresh" + api_key_error: "Invalid API key" api_key_information: "Don’t have API key? Click here for more info." + saved: "Saved" + update_printers: "Update printers" + no_printers_available: "No printers available" + connected_accounts: head_title: "Settings | Connected Accounts"