Skip to content

Commit

Permalink
Nobn-js destroy action for support letter attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBitZesty committed Aug 27, 2024
1 parent 1c4c572 commit 69dae12
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
29 changes: 29 additions & 0 deletions app/controllers/form/support_letter_attachments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Form::SupportLetterAttachmentsController < Form::BaseController
include FormAnswerSubmissionMixin
before_action :set_support_letter

def show; end

def destroy
attachment = SupportLetterAttachment.find(params[:id])
form_answer = attachment.form_answer

if attachment.destroy
updated_list = form_answer.document['supporter_letters_list'].reject { |letter| letter['letter_of_support'] == attachment.id }
form_answer.update(document: form_answer.document.merge(supporter_letters_list: updated_list))

flash[:notice] = 'Attachment successfully deleted.'
else
flash[:alert] = 'Failed to delete attachment.'
end

redirect_to form_form_answer_supporters_path(form_answer)
end

private

def set_support_letter
@support_letter = SupportLetter.find(params[:support_letter_id])
@form_answer = @support_letter.form_answer
end
end
20 changes: 20 additions & 0 deletions app/views/form/support_letter_attachments/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- content_for :page_title, "Support Letter Attachment"

.govuk-grid-row
.govuk-grid-column-two-thirds
h1.govuk-heading-xl Support Letter Attachment

- if @support_letter.support_letter_attachment.present?
.govuk-summary-list
.govuk-summary-list__row
dt.govuk-summary-list__key
| File Name
dd.govuk-summary-list__value
= @support_letter.support_letter_attachment.original_filename

= button_to "Delete Attachment",
form_form_answer_support_letter_support_letter_attachment_path(@form_answer, @support_letter, @support_letter.support_letter_attachment),
method: :delete,
class: "govuk-button govuk-button--warning"
- else
p.govuk-body No attachment found for this support letter.
4 changes: 3 additions & 1 deletion app/views/form/support_letters/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
= "Upload the #{first_or_second} letter of support"
span.question-context.question-debug.govuk-hint
' If you upload the wrong file, click the 'Remove' link next to the file name to delete it. The file upload button will reappear, allowing you to select the correct file.

= ff.simple_fields_for :support_letter_attachment, (ff.object.support_letter_attachment || ff.object.build_support_letter_attachment) do |fff|
= fff.input :attachment, as: :file, label: false, input_html: { class: "form-control" }, wrapper_html: { style: "margin-bottom: -1rem;" }
- if fff.object.attachment.present?
p.govuk-body.support-letter-attachment-filename
= render "shared/attachment_with_virus_check_status", item: fff.object, mount_name: :attachment

= fff.input :attachment_cache, as: :hidden

= fff.input :form_answer_id, as: :hidden, input_html: { value: @form_answer.id }
= fff.input :user_id, as: :hidden, input_html: { value: current_user.id }

- if ff.object.support_letter_attachment.id.present?
= link_to 'Remove Attachment', form_form_answer_support_letter_support_letter_attachment_path(@form_answer, ff.object, ff.object.support_letter_attachment), method: :delete, class: 'govuk-link'
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
namespace :form do
resources :form_answers do
resources :supporters, only: [:new, :create, :index, :destroy]
resources :support_letters, only: [:create]
resources :support_letters, only: [:create] do
resources :support_letter_attachments, only: [:show, :destroy]
end
resources :form_attachments, only: [:index, :new, :create, :destroy]
end
end
Expand Down

0 comments on commit 69dae12

Please sign in to comment.