-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Liking project submissions with Hotwire (#3929)
Because: * We are moving our React components to Hotwire This commit: * Add like view component to encapsulate the like button. * Add like singular resource route and controller - now with a proper delete route instead of reusing the create action. * Decorates submissions with current user likes when they are being fetched. This saves us from checking each one individually and hitting the database each time. * Adds a reusable sorting stimulus controller - can be used to automatically sort lists on page load or when a list item changes.
- Loading branch information
1 parent
f0bab11
commit 0302b1c
Showing
17 changed files
with
282 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<%= turbo_frame_tag dom_id(project_submission, :likes) do %> | ||
<%= button_to project_submission_v2_like_path(project_submission), method: http_action, class: 'text-gray-400 mr-4 flex items-center hint--top', data: { test_id: 'like-submission' }, aria: { label: 'Like submission' } do %> | ||
<span class="mr-1" data-test-id="like-count"><%= project_submission.cached_votes_total %></span> | ||
<%= inline_svg_tag 'icons/heart.svg', class: "h-5 w-5 #{bg_color_class}", aria: true, title: 'heart', desc: 'heart icon' %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module ProjectSubmissions | ||
class LikeComponent < ApplicationComponent | ||
def initialize(project_submission) | ||
@project_submission = project_submission | ||
end | ||
|
||
private | ||
|
||
attr_reader :project_submission | ||
|
||
def http_action | ||
project_submission.liked? ? :delete : :post | ||
end | ||
|
||
def bg_color_class | ||
project_submission.liked? ? 'text-teal-700' : 'text-gray-400' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
app/controllers/project_submissions/v2_likes_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class ProjectSubmissions::V2LikesController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def create | ||
@project_submission = ProjectSubmission.find(params[:project_submission_id]) | ||
@project_submission.like!(current_user) | ||
|
||
respond_to do |format| | ||
format.turbo_stream | ||
end | ||
end | ||
|
||
def destroy | ||
@project_submission = ProjectSubmission.find(params[:project_submission_id]) | ||
@project_submission.unlike!(current_user) | ||
|
||
respond_to do |format| | ||
format.turbo_stream { render :create } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import Sortable from 'sortablejs'; | ||
|
||
export default class extends Controller { | ||
static targets = ['item']; | ||
|
||
connect() { | ||
this.sortable = Sortable.create(this.element, { | ||
animation: 300, | ||
easing: 'cubic-bezier(0.61, 1, 0.88, 1)', | ||
disabled: true, | ||
}); | ||
} | ||
|
||
itemTargetConnected() { | ||
const items = Array.from(this.itemTargets); | ||
|
||
if (this.itemsAreSorted(items)) return; | ||
|
||
const sortedItems = items.sort((a, b) => this.compareItems(a, b)).map((item) => item.dataset.id); | ||
this.sortable.sort(sortedItems, true); | ||
} | ||
|
||
itemsAreSorted() { | ||
return this.itemSortCodes().every((sortCode, index, items) => { | ||
if (index === 0) return true; | ||
return sortCode <= items[index - 1]; | ||
}); | ||
} | ||
|
||
itemSortCodes() { | ||
return this.itemTargets.map((item) => this.getSortCode(item)); | ||
} | ||
|
||
/* eslint-disable class-methods-use-this */ | ||
getSortCode(item) { | ||
return parseFloat(item.getAttribute('data-sort-code')) || 0; | ||
} | ||
|
||
compareItems(left, right) { | ||
return this.getSortCode(right) - this.getSortCode(left); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module ProjectSubmissions | ||
class MarkLiked | ||
def initialize(user:, project_submissions:) | ||
@user = user | ||
@project_submissions = project_submissions | ||
end | ||
|
||
def self.call(**args) | ||
new(**args).call | ||
end | ||
|
||
def call | ||
project_submissions.each do |submission| | ||
next if liked_submission_ids.exclude?(submission.id) | ||
|
||
submission.like! | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :user, :project_submissions | ||
|
||
def liked_submission_ids | ||
@liked_submission_ids ||= user.votes.where(votable: project_submissions).pluck(:votable_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/views/project_submissions/v2_likes/create.turbo_stream.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= turbo_stream.replace @project_submission do %> | ||
<%= render ProjectSubmissions::ItemComponent.new(project_submission: @project_submission, current_user: current_user) %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Liking project submissions' do | ||
let(:user) { create(:user) } | ||
let(:lesson) { create(:lesson, :project) } | ||
|
||
context "when liking other users' submissions" do | ||
before do | ||
Flipper.enable(:v2_project_submissions) | ||
create(:project_submission, lesson:) | ||
|
||
sign_in(user) | ||
visit lesson_path(lesson) | ||
end | ||
|
||
after do | ||
Flipper.disable(:v2_project_submissions) | ||
end | ||
|
||
it 'you can like another users submission' do | ||
within(:test_project_submission, 1) do | ||
expect(find(:test_id, 'like-count')).to have_content('0') | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('1') | ||
end | ||
end | ||
|
||
it 'you can unlike another users submission' do | ||
within(:test_project_submission, 1) do | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('1') | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('0') | ||
end | ||
end | ||
end | ||
|
||
context 'when liking your own submission' do | ||
before do | ||
Flipper.enable(:v2_project_submissions) | ||
create(:project_submission, lesson:, user:) | ||
|
||
sign_in(user) | ||
visit lesson_path(lesson) | ||
end | ||
|
||
after do | ||
Flipper.disable(:v2_project_submissions) | ||
end | ||
|
||
it 'you can like your submission' do | ||
within(:test_project_submission, 1) do |submission| | ||
expect(submission).to have_content(user.username) | ||
expect(find(:test_id, 'like-count')).to have_content('0') | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('1') | ||
end | ||
end | ||
|
||
it 'you can unlike your submission' do | ||
within(:test_project_submission, 1) do |submission| | ||
expect(submission).to have_content(user.username) | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('1') | ||
find(:test_id, 'like-submission').click | ||
expect(find(:test_id, 'like-count')).to have_content('0') | ||
end | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.