Skip to content

Commit

Permalink
Fix: Hotwire submissions add and delete flow (#3925)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KevinMulhern committed Jul 10, 2023
1 parent 4aecfec commit 3c3bd6f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/controllers/lessons/v2_project_submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/views/lessons/v2_project_submissions/_add_button.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
Original file line number Diff line number Diff line change
Expand Up @@ -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", "" %>
Original file line number Diff line number Diff line change
@@ -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 %>
4 changes: 3 additions & 1 deletion app/views/lessons/v2_project_submissions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
</h4>
</div>
<% 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' } %>
<div id="add-submission-button">
<%= render 'lessons/v2_project_submissions/add_button', lesson: @lesson %>
</div>
<% end %>
</header>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c3bd6f

Please sign in to comment.