From 7938a354655b196842a77b0cda1d5b671176c2a6 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 8 Jul 2023 00:04:35 +0100 Subject: [PATCH] Fix: Hotwire submissions add and delete flow Because: * The add button was not displaying again after deleting your submission. This commit: * Breaks the add submission button out into its own partial so it can be rendered within turbo streams. * Adds a destroy turbo stream response to remove the submissions and display the add button. * Add a couple of expectations to ensure this cannot regress in the future. --- app/controllers/lessons/v2_project_submissions_controller.rb | 4 +++- .../lessons/v2_project_submissions/_add_button.html.erb | 3 +++ .../lessons/v2_project_submissions/create.turbo_stream.erb | 2 +- .../lessons/v2_project_submissions/destroy.turbo_stream.erb | 5 +++++ app/views/lessons/v2_project_submissions/index.html.erb | 4 +++- .../v2_lesson_project_submissions/add_submission_spec.rb | 2 ++ .../v2_lesson_project_submissions/delete_submission_spec.rb | 2 ++ 7 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 app/views/lessons/v2_project_submissions/_add_button.html.erb create mode 100644 app/views/lessons/v2_project_submissions/destroy.turbo_stream.erb diff --git a/app/controllers/lessons/v2_project_submissions_controller.rb b/app/controllers/lessons/v2_project_submissions_controller.rb index bd90a8f4ce..0e779cae17 100644 --- a/app/controllers/lessons/v2_project_submissions_controller.rb +++ b/app/controllers/lessons/v2_project_submissions_controller.rb @@ -46,7 +46,9 @@ def destroy @project_submission = current_user.project_submissions.find(params[:id]) @project_submission.destroy - render turbo_stream: turbo_stream.remove(@project_submission) + respond_to do |format| + format.turbo_stream + end end private diff --git a/app/views/lessons/v2_project_submissions/_add_button.html.erb b/app/views/lessons/v2_project_submissions/_add_button.html.erb new file mode 100644 index 0000000000..c7d173b900 --- /dev/null +++ b/app/views/lessons/v2_project_submissions/_add_button.html.erb @@ -0,0 +1,3 @@ +<%= link_to new_lesson_v2_project_submission_path(@lesson), class: 'button button--primary', data: { turbo_frame: 'modal', test_id: 'add_submission_btn' } do %> + Add submission +<% end %> diff --git a/app/views/lessons/v2_project_submissions/create.turbo_stream.erb b/app/views/lessons/v2_project_submissions/create.turbo_stream.erb index eb1006bd27..740116a61c 100644 --- a/app/views/lessons/v2_project_submissions/create.turbo_stream.erb +++ b/app/views/lessons/v2_project_submissions/create.turbo_stream.erb @@ -2,4 +2,4 @@ <%= render ProjectSubmissions::ItemComponent.new(project_submission: @project_submission, current_user: current_user) %> <% end %> -<%= turbo_stream.remove "add-submission-button" %> +<%= turbo_stream.update "add-submission-button", "" %> diff --git a/app/views/lessons/v2_project_submissions/destroy.turbo_stream.erb b/app/views/lessons/v2_project_submissions/destroy.turbo_stream.erb new file mode 100644 index 0000000000..cd40bf8822 --- /dev/null +++ b/app/views/lessons/v2_project_submissions/destroy.turbo_stream.erb @@ -0,0 +1,5 @@ +<%= turbo_stream.remove @project_submission %> + +<%= turbo_stream.update "add-submission-button" do %> + <%= render "lessons/v2_project_submissions/add_button", lesson: @lesson %> +<% end %> diff --git a/app/views/lessons/v2_project_submissions/index.html.erb b/app/views/lessons/v2_project_submissions/index.html.erb index 9ee9496f06..69fd35e868 100644 --- a/app/views/lessons/v2_project_submissions/index.html.erb +++ b/app/views/lessons/v2_project_submissions/index.html.erb @@ -19,7 +19,9 @@ <% if @current_user_submission.nil? %> - <%= link_to 'Add solution', new_lesson_v2_project_submission_path(@lesson), id: 'add-submission-button', class: 'button button--primary', data: { turbo_frame: 'modal', test_id: 'add_submission_btn' } %> +
+ <%= render 'lessons/v2_project_submissions/add_button', lesson: @lesson %> +
<% end %> diff --git a/spec/system/v2_lesson_project_submissions/add_submission_spec.rb b/spec/system/v2_lesson_project_submissions/add_submission_spec.rb index 896d10b63a..28a5fc9b7e 100644 --- a/spec/system/v2_lesson_project_submissions/add_submission_spec.rb +++ b/spec/system/v2_lesson_project_submissions/add_submission_spec.rb @@ -42,6 +42,8 @@ expect(page).to have_content(user.username) end + expect(page).not_to have_content('Add submission') + using_session('another_user') do sign_in(another_user) visit lesson_path(lesson) diff --git a/spec/system/v2_lesson_project_submissions/delete_submission_spec.rb b/spec/system/v2_lesson_project_submissions/delete_submission_spec.rb index 885fedcb7d..cda5a00004 100644 --- a/spec/system/v2_lesson_project_submissions/delete_submission_spec.rb +++ b/spec/system/v2_lesson_project_submissions/delete_submission_spec.rb @@ -31,6 +31,8 @@ end end + expect(page).to have_content('Add submission') + within(:test_id, 'submissions-list') do expect(page).not_to have_content(user.username) end