-
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
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
3f6cf7f
commit f3a109b
Showing
17 changed files
with
319 additions
and
49 deletions.
There are no files selected for viewing
87 changes: 41 additions & 46 deletions
87
app/components/project_submissions/item_component.html.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 |
---|---|---|
@@ -1,52 +1,47 @@ | ||
<%= turbo_frame_tag project_submission do %> | ||
<div data-test-id="submission-item" class="relative py-6 border-solid border-t border-gray-300 flex flex-col md:flex-row justify-between items-center"> | ||
<div id="<%= dom_id(project_submission) %>" data-id="<%= project_submission.id %>" data-sort-target="item" data-sort-code="<%= sort_code %>"> | ||
<div data-test-id="submission-item" class="relative py-6 border-solid border-t border-gray-300 flex flex-col md:flex-row justify-between items-center"> | ||
|
||
<div class="flex items-center mb-4 md:mb-0"> | ||
|
||
<button class="text-gray-300 mr-4 flex items-center hint--top" data-test-id="like-btn" aria-label="Like submission"> | ||
<span class="mr-1" data-test-id="number-of-likes"><%= project_submission.votes_for.size %></span> | ||
<%= inline_svg_tag 'icons/heart.svg', class: 'h-5 w-5', aria: true, title: 'heart', desc: 'heart icon' %> | ||
</button> | ||
|
||
<p class="truncate max-w-xs lg:max-w-lg font-medium text-lg break-words"><%= project_submission.user.username %></p> | ||
</div> | ||
|
||
<div class="flex flex-col md:flex-row md:items-center"> | ||
<%= link_to 'View code', project_submission.repo_url, target: '_blank', rel: 'noreferrer', class: 'button button--gray font-semibold md:mr-4', data: { test_id: 'view-code-btn' } %> | ||
<%= link_to 'Live preview', project_submission.live_preview_url, target: '_blank', rel: 'noreferrer', class: 'button button--gray font-semibold mt-5 md:mt-0 md:mr-4', data: { test_id: 'live-preview-btn' } %> | ||
|
||
<div class="relative flex-none" data-controller="visibility" data-action="visibility:click:outside->visibility#off" data-visibility-visible-value="false"> | ||
<button type="button" data-action="click->visibility#toggle" data-test-id="submission-action-menu-btn" class="-m-2.5 block p-2.5 text-gray-500 hover:text-gray-900" id="options-menu-0-button" aria-expanded="false" aria-haspopup="true"> | ||
<span class="sr-only">Open options</span> | ||
<svg class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> | ||
<path d="M10 3a1.5 1.5 0 110 3 1.5 1.5 0 010-3zM10 8.5a1.5 1.5 0 110 3 1.5 1.5 0 010-3zM11.5 15.5a1.5 1.5 0 10-3 0 1.5 1.5 0 003 0z" /> | ||
</svg> | ||
</button> | ||
|
||
<div | ||
data-visibility-target="content" | ||
data-transition-enter="transition ease-out duration-200" | ||
data-transition-enter-start="transform opacity-0 scale-95" | ||
data-transition-enter-end="transform opacity-100 scale-100" | ||
data-transition-leave="transition ease-in duration-75" | ||
data-transition-leave-start="transform opacity-100 scale-100" | ||
data-transition-leave-end="transform opacity-10 scale-95" | ||
class="absolute right-0 z-10 mt-2 w-32 origin-top-right rounded-md bg-white py-2 shadow-lg ring-1 ring-gray-900/5 focus:outline-none" role="menu" aria-orientation="vertical" aria-labelledby="options-menu-0-button" tabindex="-1"> | ||
|
||
<% if project_submission.user == current_user %> | ||
<%= link_to edit_lesson_v2_project_submission_path(project_submission.lesson, project_submission), class: 'text-gray-700 group flex items-center px-4 py-2 text-sm', role: 'menuitem', tabindex: '-1', data: { turbo_frame: 'modal', test_id: 'edit-submission'} do %> | ||
<%= inline_svg_tag 'icons/pencil-square.svg', class: 'mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500', aria: true, title: 'edit', desc: 'edit icon' %> | ||
Edit | ||
<% end %> | ||
<div class="flex items-center mb-4 md:mb-0"> | ||
<%= render ProjectSubmissions::LikeComponent.new(project_submission) %> | ||
<p class="truncate max-w-xs lg:max-w-lg font-medium text-lg break-words"><%= project_submission.user.username %></p> | ||
</div> | ||
|
||
<%= link_to lesson_v2_project_submission_path(project_submission.lesson, project_submission), class: 'text-gray-700 group flex items-center px-4 py-2 text-sm', role: 'menuitem', tabindex: '-1', data: { turbo_method: :delete, turbo_confirm: 'Are you sure? this cannot be undone.', test_id: 'delete-submission' } do %> | ||
<%= inline_svg_tag 'icons/trash.svg', class: 'mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500', aria: true, title: 'edit', desc: 'edit icon' %> | ||
Delete | ||
<div class="flex flex-col md:flex-row md:items-center"> | ||
<%= link_to 'View code', project_submission.repo_url, target: '_blank', rel: 'noreferrer', class: 'button button--gray font-semibold md:mr-4', data: { test_id: 'view-code-btn' } %> | ||
<%= link_to 'Live preview', project_submission.live_preview_url, target: '_blank', rel: 'noreferrer', class: 'button button--gray font-semibold mt-5 md:mt-0 md:mr-4', data: { test_id: 'live-preview-btn' } %> | ||
|
||
<div class="relative flex-none" data-controller="visibility" data-action="visibility:click:outside->visibility#off" data-visibility-visible-value="false"> | ||
<button type="button" data-action="click->visibility#toggle" data-test-id="submission-action-menu-btn" class="-m-2.5 block p-2.5 text-gray-500 hover:text-gray-900" id="options-menu-0-button" aria-expanded="false" aria-haspopup="true"> | ||
<span class="sr-only">Open options</span> | ||
<svg class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> | ||
<path d="M10 3a1.5 1.5 0 110 3 1.5 1.5 0 010-3zM10 8.5a1.5 1.5 0 110 3 1.5 1.5 0 010-3zM11.5 15.5a1.5 1.5 0 10-3 0 1.5 1.5 0 003 0z" /> | ||
</svg> | ||
</button> | ||
|
||
<div | ||
data-visibility-target="content" | ||
data-transition-enter="transition ease-out duration-200" | ||
data-transition-enter-start="transform opacity-0 scale-95" | ||
data-transition-enter-end="transform opacity-100 scale-100" | ||
data-transition-leave="transition ease-in duration-75" | ||
data-transition-leave-start="transform opacity-100 scale-100" | ||
data-transition-leave-end="transform opacity-10 scale-95" | ||
class="hidden absolute right-0 z-10 mt-2 w-32 origin-top-right rounded-md bg-white py-2 shadow-lg ring-1 ring-gray-900/5 focus:outline-none" role="menu" aria-orientation="vertical" aria-labelledby="options-menu-0-button" tabindex="-1"> | ||
|
||
<% if project_submission.user == current_user %> | ||
<%= link_to edit_lesson_v2_project_submission_path(project_submission.lesson, project_submission), class: 'text-gray-700 group flex items-center px-4 py-2 text-sm', role: 'menuitem', tabindex: '-1', data: { turbo_frame: 'modal', test_id: 'edit-submission'} do %> | ||
<%= inline_svg_tag 'icons/pencil-square.svg', class: 'mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500', aria: true, title: 'edit', desc: 'edit icon' %> | ||
Edit | ||
<% end %> | ||
<%= link_to lesson_v2_project_submission_path(project_submission.lesson, project_submission), class: 'text-gray-700 group flex items-center px-4 py-2 text-sm', role: 'menuitem', tabindex: '-1', data: { turbo_method: :delete, turbo_confirm: 'Are you sure? this cannot be undone.', test_id: 'delete-submission' } do %> | ||
<%= inline_svg_tag 'icons/trash.svg', class: 'mr-3 h-5 w-5 text-gray-400 group-hover:text-gray-500', aria: true, title: 'edit', desc: 'edit icon' %> | ||
Delete | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> |
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
Oops, something went wrong.