demarches-normaliennes/app/components/dossiers/message_component.rb

69 lines
1.7 KiB
Ruby
Raw Normal View History

class Dossiers::MessageComponent < ApplicationComponent
def initialize(commentaire:, connected_user:, messagerie_seen_at: nil, show_reply_button: false)
@commentaire = commentaire
@connected_user = connected_user
@messagerie_seen_at = messagerie_seen_at
@show_reply_button = show_reply_button
end
attr_reader :commentaire, :connected_user, :messagerie_seen_at
def correction_badge
return if commentaire.dossier_correction.nil?
return helpers.correction_resolved_badge if commentaire.dossier_correction.resolved?
helpers.pending_correction_badge(connected_user.is_a?(Instructeur) ? :for_instructeur : :for_user)
end
private
def show_reply_button?
@show_reply_button
end
def highlight_if_unseen_class
if highlight?
'highlighted'
end
end
def scroll_to_target
if highlight?
{ scroll_to_target: 'to' }
end
end
2023-11-28 14:45:15 +01:00
def icon
if commentaire.sent_by_system?
2023-11-28 14:45:15 +01:00
dsfr_icon('fr-icon-message-2-fill', :sm, :mr)
elsif commentaire.sent_by?(connected_user)
2023-11-28 14:45:15 +01:00
dsfr_icon('fr-icon-user-fill', :sm, :mr)
else
2023-11-28 14:45:15 +01:00
dsfr_icon('fr-icon-discuss-fill', :sm, :mr)
end
end
def commentaire_issuer
if commentaire.sent_by_system?
t('.automatic_email')
elsif commentaire.sent_by?(connected_user)
t('.you')
else
commentaire.redacted_email
end
end
def commentaire_from_guest?
commentaire.dossier.invites.map(&:email).include?(commentaire.email)
end
def commentaire_date
is_current_year = (commentaire.created_at.year == Time.zone.today.year)
l(commentaire.created_at, format: is_current_year ? :message_date : :message_date_with_year)
end
def highlight?
commentaire.persisted? && @messagerie_seen_at&.<(commentaire.created_at)
end
end