-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Liking project submissions with Hotwire #3929
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bit of a workaround for now. After we get feature parity with the current submissions, we would be in a good place to revisit the design of submissions; and cleanly separate the users submission from the rest.