diff --git a/app/assets/javascripts/sitewide/print_progress_modal.js b/app/assets/javascripts/sitewide/print_progress_modal.js index 217b35d41..68e972d94 100644 --- a/app/assets/javascripts/sitewide/print_progress_modal.js +++ b/app/assets/javascripts/sitewide/print_progress_modal.js @@ -1,32 +1,33 @@ -function updateProgressModal() { - var status; - var modal = $(document).find('.label-printing-progress-modal'); +(function() { + function updateProgressModal() { + var modal = $('.label-printing-progress-modal'); - if (modal.length === 0) { - return; - } + if (modal.length === 0) return; - $.getJSON( - `/label_printers/${modal.data('labelPrinterId')}/update_progress_modal` - + `?starting_item_count=${modal.data('startingItemCount')}`, - function(data) { - modal.replaceWith(data.html); - - status = modal.data('label-printer-status'); - if (status !== 'done' && status !== 'error') { - setTimeout(updateProgressModal, 3000); + $.getJSON( + modal.data('progress-url'), function(data) { + modal.replaceWith(data.html); + let status = modal.data('label-printer-status'); + if (!['done', 'error'].includes(status)) { + setTimeout(updateProgressModal, 3000); + } } - } - ); -} - -$(document).on('click', '.label-printing-progress-modal .close', function() { - $(this).closest('.label-printing-progress-modal').remove(); -}); - -$(document).on('turbolinks:load', function() { - var modal = $(document).find('.label-printing-progress-modal'); - if (modal.length > 0) { - updateProgressModal(); + ); } -}); + + $(document).on('click', '.label-printing-progress-modal .close', function() { + $(this).closest('.label-printing-progress-modal').remove(); + }); + + $(document).on('ajax:success', '.print-label-form', function(e, data) { + var modal = $('.label-printing-progress-modal'); + if (modal.length) { + modal.replaceWith(data.html); + } else { + $('body').append($(data.html)); + } + + updateProgressModal(); + $('#modal-print-repository-row-label').modal('hide'); + }); +}()); diff --git a/app/controllers/repository_rows_controller.rb b/app/controllers/repository_rows_controller.rb index 5d727bdf4..bfa721807 100644 --- a/app/controllers/repository_rows_controller.rb +++ b/app/controllers/repository_rows_controller.rb @@ -109,7 +109,13 @@ class RepositoryRowsController < ApplicationController label_printer.update!(current_print_job_ids: job_ids * params[:copies].to_i) - redirect_to repository_path(@repository) + render json: { + html: render_to_string( + partial: 'label_printers/print_progress_modal.html.erb', + locals: { starting_item_count: label_printer.current_print_job_ids.length, + label_printer: label_printer } + ) + } end def update diff --git a/app/views/label_printers/_print_progress_modal.html.erb b/app/views/label_printers/_print_progress_modal.html.erb index c5f1656f1..0f9b7c54f 100644 --- a/app/views/label_printers/_print_progress_modal.html.erb +++ b/app/views/label_printers/_print_progress_modal.html.erb @@ -1,4 +1,7 @@ -
+