Skip to content
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

Heart toggles #1238

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ def toggle_favorite

current_user.toggle_favorite(asset)
respond_to do |format|
format.turbo_stream { render turbo_stream: '' }
format.turbo_stream do
render turbo_stream: turbo_stream.update_all(".faved-#{asset.id}", asset.reload.favorites_count)
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions app/javascript/controllers/favorite_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ export default class extends HeartController {
id: Number,
}

static outlets = [ "favorite" ]

isFavorited() {
return window.userFavorites.includes(this.idValue)
}

broadcast() {
this.favoriteOutlets.forEach((result) => {
if (this.idValue == result.idValue)
result.toggle()
})
}
}
14 changes: 9 additions & 5 deletions app/views/assets/_asset.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@
<%= button_to toggle_favorite_path(asset_id: asset.id), class: 'add_to_favorites',
method: :put, data: {
controller: 'favorite',
action: 'favorite#toggle',
'favorite-id-value': asset.id
action: 'favorite#broadcast',
'favorite-id-value': asset.id,
'favorite-favorite-outlet': ".add_to_favorites"
} do %>
<%== render file: svg_path('svg/animation_heart.svg') %>
<% end %>
Expand All @@ -84,10 +85,13 @@
</i>
</div>

<div class="favorites"><%= asset.favorites_count %>
<i class="icon_favs">
<div class="favorites">
<div class="<%= "faved-#{asset.id}"%>">
<%= asset.favorites_count %>
</div>
<i class="icon_favs">
<%== render file: svg_path('svg/icon_favorite_inverted.svg') %>
</i>
</i>
</div>
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions app/views/shared/_asset.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
<i class="icon_favs">
<%== render file: svg_path('svg/icon_favorite_inverted.svg') %>
</i>
<%= @asset.favorites_count %>
<div class="<%= "faved-#{@asset.id}"%>">
<%= @asset.favorites_count %>
</div>
</div>
<div class="plays">
<i class="icon_plays">
Expand Down Expand Up @@ -124,4 +126,5 @@
</div>
<% end %>
</div>
</turbo-frame>
</turbo-frame>