Migrate List actions to Turbo Streams and Frames

This commit is contained in:
Andreas Nedbal 2022-12-05 23:10:11 +01:00
parent 7a56c1e5bd
commit 44f9437536
7 changed files with 83 additions and 28 deletions

View file

@ -8,22 +8,64 @@ class ListsController < ApplicationController
@lists = List.where(user: current_user)
end
def create
target_user = User.find_by!(screen_name: params[:user])
list = List.create! user: current_user, display_name: params[:name]
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("create-form", partial: "lists/form", locals: { user: target_user }),
turbo_stream.prepend("lists", partial: "lists/item", locals: { list:, user: target_user })
]
end
format.html { redirect_to user_path(target_user) }
end
end
def update
@add = params[:add] == "true"
@target_user = User.find_by!(screen_name: params[:user])
@list = current_user.lists.find(params[:list])
raise Errors::ListingSelfBlockedOther if current_user.blocking?(@target_user)
raise Errors::ListingOtherBlockedSelf if @target_user.blocking?(@current_user)
if @add
@list.add_member @target_user if @list.members.find_by(user_id: @target_user.id).nil?
else
@list.remove_member @target_user unless @list.members.find_by(user_id: @target_user.id).nil?
end
respond_to do |format|
format.turbo_stream do
render "update", layout: false, status: :see_other
end
format.html { redirect_to user_path(@target_user) }
end
end
def destroy
@list = List.find(params[:id])
@list = List.find(params[:list])
@list.destroy
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.remove("list_#{params[:id]}")
render turbo_stream: turbo_stream.remove("list_#{params[:list]}")
end
format.html { redirect_to root_path }
end
end
private
def set_user
@user = User.where("LOWER(screen_name) = ?", params[:username].downcase).includes(:profile).first!
@user = User.where("LOWER(screen_name) = ?", params[:user].downcase).includes(:profile).first!
end
def list_params

View file

@ -0,0 +1,4 @@
%form#create-form{ action: lists_create_path, method: :post }
%input{ type: :hidden, name: "user", value: user.screen_name }
%input.form-control.mb-2#name{ type: :text, name: "name", placeholder: t(".placeholder") }
%input{ type: :submit, class: "btn btn-primary", value: t(".action") }

View file

@ -0,0 +1,17 @@
%li.list-group-item{ id: "list_#{list.id}" }
.d-flex
%div
.list-group-item-heading= list.display_name
.list-group-item-text.text-muted
%span{ id: "list_#{list.id}_members" }= t("voc.members", count: list.members.count)
·
= link_to lists_destroy_path(list), data: { turbo_method: :delete }, class: "text-danger" do
%i.fa.fa-close
= t("voc.delete")
.ml-auto.d-inline-flex{ id: "list_#{list.id}_membership" }
- if user.member_of?(list)
= link_to lists_update_path(list, user:, add: false), class: "btn btn-default align-self-center", data: { turbo_method: :patch } do
= t("lists.actions.remove")
- else
= link_to lists_update_path(list, user:, add: true), class: "btn btn-primary align-self-center", data: { turbo_method: :patch } do
= t("lists.actions.add")

View file

@ -18,13 +18,12 @@
.tab-content
.tab-pane.active{ role: :tabpanel, id: "lists-list" }
%ul.list-group
%ul.list-group#lists
- @lists.each do |list|
= render "modal/list/item", list: list, user: @user
= render "lists/item", list: list, user: @user
.tab-pane{ role: :tabpanel, id: "create" }
.modal-body
%input.form-control#new-list-name{ type: :text, placeholder: t(".tab.create.placeholder") }
%button.btn.btn-primary#create-list{ type: :button }= t(".tab.create.action")
= render "lists/form", user: @user
.modal-footer
= button_to modal_close_path, class: "btn btn-primary" do
= t("voc.close")

View file

@ -0,0 +1,10 @@
= turbo_stream.update "list_#{@list.id}_membership" do
- if @target_user.member_of?(@list)
= link_to lists_update_path(@list, user: @target_user, add: false), class: "btn btn-default align-self-center", data: { turbo_method: :patch } do
= t("lists.actions.remove")
- else
= link_to lists_update_path(@list, user: @target_user, add: true), class: "btn btn-primary align-self-center", data: { turbo_method: :patch } do
= t("lists.actions.add")
= turbo_stream.update "list_#{@list.id}_members" do
= t("voc.members", count: @list.members.count)

View file

@ -1,17 +0,0 @@
%li.list-group-item{ id: "list_#{list.name}" }
.media
.pull-left
.custom-control.custom-checkbox
%input.custom-control-input{ type: :checkbox,
id: "listCheck#{list.id}",
name: 'gm-list-check',
data: { list: list.name, user: user.screen_name }, checked: user.member_of?(list), autocomplete: :off }
%label.custom-control-label{ for: "listCheck#{list.id}" }
.media-body
.list-group-item-heading= list.display_name
.list-group-item-text.text-muted
%span{ id: "#{list.name}-members", data: { count: list.members.count } }= t(".members", count: list.members.count)
·
= link_to lists_destroy_path(list), data: { turbo_method: :destroy }, class: "text-danger" do
%i.fa.fa-close
= t("voc.delete")

View file

@ -127,15 +127,15 @@ Rails.application.routes.draw do
post "/create_comment", to: "comment#create", as: :create_comment
post "/destroy_comment", to: "comment#destroy", as: :destroy_comment
post "/report", to: "report#create", as: :report
post "/create_list", to: "list#create", as: :create_list
post "/destroy_list", to: "list#destroy", as: :destroy_list
post "/list_membership", to: "list#membership", as: :list_membership
post "/subscribe", to: "subscription#subscribe", as: :subscribe_answer
post "/unsubscribe", to: "subscription#unsubscribe", as: :unsubscribe_answer
end
post "/lists", to: "lists#index", as: :lists
get "/lists", to: "lists#index", as: :lists
post "/lists", to: "lists#create", as: :lists_create
patch "/lists/:list", to: "lists#update", as: :lists_update
delete "/lists/:list", to: "lists#destroy", as: :lists_destroy
resource :anonymous_block, controller: :anonymous_block, only: %i[create destroy]
get "/discover", to: "discover#index", as: :discover