Merge pull request #4993 from betagouv/fix-piece-justificative-in-repetitions

Usager : correction de l'envoi automatique de pièces justificatives dans un bloc répétable
This commit is contained in:
Pierre de La Morinerie 2020-04-06 12:03:49 +02:00 committed by GitHub
commit 0538da3fad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 25 deletions

View file

@ -49,6 +49,15 @@ module ApplicationHelper
end end
end end
def render_champ(champ)
champ_selector = ".editable-champ[data-champ-id=\"#{champ.id}\"]"
form_html = render 'shared/dossiers/edit', dossier: champ.dossier, apercu: false
champ_html = Nokogiri::HTML.fragment(form_html).at_css(champ_selector).to_s
# rubocop:disable Rails/OutputSafety
raw("document.querySelector('#{champ_selector}').outerHTML = \"#{escape_javascript(champ_html)}\";")
# rubocop:enable Rails/OutputSafety
end
def remove_element(selector, timeout: 0, inner: false) def remove_element(selector, timeout: 0, inner: false)
script = "(function() {"; script = "(function() {";
script << "var el = document.querySelector('#{selector}');" script << "var el = document.querySelector('#{selector}');"

View file

@ -33,8 +33,8 @@ module ChampHelper
end end
def auto_attach_url(form, object) def auto_attach_url(form, object)
if feature_enabled?(:autoupload_dossier_attachments) && object.is_a?(Champ) && object.public? if feature_enabled?(:autoupload_dossier_attachments) && object.is_a?(Champ) && object.persisted? && object.public?
champs_piece_justificative_url(form.index) champs_piece_justificative_url(object.id)
end end
end end
end end

View file

@ -23,18 +23,12 @@ export default class AutoUploadController {
throw new Error('Lattribut "data-auto-attach-url" est manquant'); throw new Error('Lattribut "data-auto-attach-url" est manquant');
} }
const champ = this.input.closest('.editable-champ[data-champ-id]');
if (!champ) {
throw new Error('Impossible de trouver lélément ".editable-champ"');
}
const champId = champ.dataset.champId;
// Upload the file (using Direct Upload) // Upload the file (using Direct Upload)
let blobSignedId = await this._upload(); let blobSignedId = await this._upload();
// Attach the blob to the champ // Attach the blob to the champ
// (The request responds with Javascript, which displays the attachment HTML fragment). // (The request responds with Javascript, which displays the attachment HTML fragment).
await this._attach(champId, blobSignedId, autoAttachUrl); await this._attach(blobSignedId, autoAttachUrl);
// Everything good: clear the original file input value // Everything good: clear the original file input value
this.input.value = null; this.input.value = null;
@ -60,17 +54,21 @@ export default class AutoUploadController {
return await uploader.start(); return await uploader.start();
} }
async _attach(champId, blobSignedId, autoAttachUrl) { async _attach(blobSignedId, autoAttachUrl) {
// Now that the upload is done, display a new progress bar // Now that the upload is done, display a new progress bar
// to show that the attachment request is still pending. // to show that the attachment request is still pending.
const progressBar = new ProgressBar(this.input, champId, this.file); const progressBar = new ProgressBar(
this.input,
`${this.input.id}-progress-bar`,
this.file
);
progressBar.progress(100); progressBar.progress(100);
progressBar.end(); progressBar.end();
const attachmentRequest = { const attachmentRequest = {
url: autoAttachUrl, url: autoAttachUrl,
type: 'PUT', type: 'PUT',
data: `champ_id=${champId}&blob_signed_id=${blobSignedId}` data: `blob_signed_id=${blobSignedId}`
}; };
await ajax(attachmentRequest); await ajax(attachmentRequest);

View file

@ -1,15 +1,4 @@
<% dossier = @champ.dossier %> <%= render_champ(@champ) %>
<%= fields_for dossier do |form| %>
<%= form.fields_for :champs, dossier.champs.where(id: @champ.id), include_id: false do |champ_form| %>
<% render_to_element(".editable-champ[data-champ-id=\"#{@champ.id}\"]",
partial: 'shared/dossiers/editable_champs/editable_champ',
locals: {
champ: @champ,
form: champ_form
}) %>
<% end %>
<% end %>
<% attachment = @champ.piece_justificative_file.attachment %> <% attachment = @champ.piece_justificative_file.attachment %>
<% if attachment.virus_scanner.pending? %> <% if attachment.virus_scanner.pending? %>

View file

@ -121,7 +121,7 @@ Rails.application.routes.draw do
get ':position/dossier_link', to: 'dossier_link#show', as: :dossier_link get ':position/dossier_link', to: 'dossier_link#show', as: :dossier_link
post ':position/carte', to: 'carte#show', as: :carte post ':position/carte', to: 'carte#show', as: :carte
post ':position/repetition', to: 'repetition#show', as: :repetition post ':position/repetition', to: 'repetition#show', as: :repetition
put ':position/piece_justificative', to: 'piece_justificative#update', as: :piece_justificative put 'piece_justificative/:champ_id', to: 'piece_justificative#update', as: :piece_justificative
end end
get 'attachments/:id', to: 'attachments#show', as: :attachment get 'attachments/:id', to: 'attachments#show', as: :attachment