From 597c859c2dd7d388876e937fb4120334309a4a13 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 10 Mar 2024 18:02:49 +0100 Subject: [PATCH] Refactor comments into CommentComponent --- app/components/comment_component.html.haml | 22 +++++++++++++++++++++ app/components/comment_component.rb | 12 +++++++++++ app/views/answerbox/_comments.html.haml | 23 +--------------------- 3 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 app/components/comment_component.html.haml create mode 100644 app/components/comment_component.rb diff --git a/app/components/comment_component.html.haml b/app/components/comment_component.html.haml new file mode 100644 index 00000000..c7a1543c --- /dev/null +++ b/app/components/comment_component.html.haml @@ -0,0 +1,22 @@ +%li.comment{ data: { comment_id: @comment.id } } + .d-flex + .flex-shrink-0 + %a{ href: user_path(@comment.user), target: :_top } + = render AvatarComponent.new(user: @comment.user, size: "sm", classes: ["comment__user-avatar"]) + .flex-grow-1 + %h6.comment__user + = user_screen_name @comment.user + %span.text-muted + · + = time_tooltip @comment + .comment__content + = markdown @comment.content + .flex-shrink-0.ms-auto + - if current_user&.smiled?(@comment) + = render "reactions/destroy", type: "Comment", target: @comment + - else + = render "reactions/create", type: "Comment", target: @comment + .dropdown.d-inline + %button.btn.btn-link.answerbox__action{ data: { bs_toggle: :dropdown }, aria: { expanded: false } } + %i.fa.fa-fw.fa-ellipsis + = render "actions/comment", comment: @comment, answer: @answer diff --git a/app/components/comment_component.rb b/app/components/comment_component.rb new file mode 100644 index 00000000..1fe41b9a --- /dev/null +++ b/app/components/comment_component.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CommentComponent < ApplicationComponent + include ApplicationHelper + include BootstrapHelper + include UserHelper + + def initialize(comment:, answer:) + @comment = comment + @answer = answer + end +end diff --git a/app/views/answerbox/_comments.html.haml b/app/views/answerbox/_comments.html.haml index 04aa7b7f..e7d61654 100644 --- a/app/views/answerbox/_comments.html.haml +++ b/app/views/answerbox/_comments.html.haml @@ -3,25 +3,4 @@ - else %ul.comment__container - comments.order(:created_at).each do |comment| - %li.comment{ data: { comment_id: comment.id } } - .d-flex - .flex-shrink-0 - %a{ href: user_path(comment.user) } - = render AvatarComponent.new(user: comment.user, size: "sm", classes: ["comment__user-avatar"]) - .flex-grow-1 - %h6.comment__user - = user_screen_name comment.user - %span.text-muted - · - = time_tooltip comment - .comment__content - = markdown comment.content - .flex-shrink-0.ms-auto - - if current_user&.smiled?(comment) - = render "reactions/destroy", type: "Comment", target: comment - - else - = render "reactions/create", type: "Comment", target: comment - .dropdown.d-inline - %button.btn.btn-link.answerbox__action{ data: { bs_toggle: :dropdown }, aria: { expanded: false } } - %i.fa.fa-fw.fa-ellipsis - = render "actions/comment", comment: comment, answer: a + = render CommentComponent.new(comment:, answer: a)