2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-04-25 12:41:01 +02:00
|
|
|
class Dossiers::MessageComponent < ApplicationComponent
|
2023-12-10 18:25:58 +01:00
|
|
|
def initialize(commentaire:, connected_user:, messagerie_seen_at: nil, show_reply_button: false, groupe_gestionnaire: nil)
|
2022-04-25 12:41:01 +02:00
|
|
|
@commentaire = commentaire
|
|
|
|
@connected_user = connected_user
|
|
|
|
@messagerie_seen_at = messagerie_seen_at
|
|
|
|
@show_reply_button = show_reply_button
|
2023-12-10 18:25:58 +01:00
|
|
|
@groupe_gestionnaire = groupe_gestionnaire
|
2022-04-25 12:41:01 +02:00
|
|
|
end
|
|
|
|
|
2023-12-10 18:25:58 +01:00
|
|
|
attr_reader :commentaire, :connected_user, :messagerie_seen_at, :groupe_gestionnaire
|
2022-04-25 12:41:01 +02:00
|
|
|
|
2023-06-02 15:46:23 +02:00
|
|
|
def correction_badge
|
2023-12-10 18:25:58 +01:00
|
|
|
return if groupe_gestionnaire || commentaire.dossier_correction.nil?
|
2023-06-02 15:46:23 +02:00
|
|
|
return helpers.correction_resolved_badge if commentaire.dossier_correction.resolved?
|
|
|
|
|
|
|
|
helpers.pending_correction_badge(connected_user.is_a?(Instructeur) ? :for_instructeur : :for_user)
|
|
|
|
end
|
|
|
|
|
2022-04-25 12:41:01 +02:00
|
|
|
private
|
|
|
|
|
2023-12-07 15:41:50 +01:00
|
|
|
def soft_deletable?
|
|
|
|
commentaire.soft_deletable?(connected_user)
|
|
|
|
end
|
|
|
|
|
2022-04-25 12:41:01 +02:00
|
|
|
def show_reply_button?
|
|
|
|
@show_reply_button
|
|
|
|
end
|
|
|
|
|
2023-12-07 15:41:50 +01:00
|
|
|
def delete_button_text
|
2023-12-10 18:25:58 +01:00
|
|
|
if groupe_gestionnaire.nil? && commentaire.dossier_correction&.pending?
|
2023-12-07 15:41:50 +01:00
|
|
|
t('.delete_with_correction_button')
|
|
|
|
else
|
|
|
|
t('.delete_button')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-25 12:41:01 +02:00
|
|
|
def highlight_if_unseen_class
|
2022-05-18 12:41:01 +02:00
|
|
|
if highlight?
|
|
|
|
'highlighted'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def scroll_to_target
|
|
|
|
if highlight?
|
|
|
|
{ scroll_to_target: 'to' }
|
|
|
|
end
|
2022-04-25 12:41:01 +02:00
|
|
|
end
|
|
|
|
|
2023-11-28 14:45:15 +01:00
|
|
|
def icon
|
2022-04-25 12:41:01 +02:00
|
|
|
if commentaire.sent_by_system?
|
2023-11-28 14:45:15 +01:00
|
|
|
dsfr_icon('fr-icon-message-2-fill', :sm, :mr)
|
2022-04-25 12:41:01 +02:00
|
|
|
elsif commentaire.sent_by?(connected_user)
|
2023-11-28 14:45:15 +01:00
|
|
|
dsfr_icon('fr-icon-user-fill', :sm, :mr)
|
2022-04-25 12:41:01 +02:00
|
|
|
else
|
2023-11-28 14:45:15 +01:00
|
|
|
dsfr_icon('fr-icon-discuss-fill', :sm, :mr)
|
2022-04-25 12:41:01 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commentaire_issuer
|
|
|
|
if commentaire.sent_by_system?
|
|
|
|
t('.automatic_email')
|
|
|
|
elsif commentaire.sent_by?(connected_user)
|
|
|
|
t('.you')
|
2023-12-10 18:25:58 +01:00
|
|
|
elsif groupe_gestionnaire
|
|
|
|
commentaire.gestionnaire_id ? commentaire.gestionnaire_email : commentaire.sender_email
|
2022-04-25 12:41:01 +02:00
|
|
|
else
|
|
|
|
commentaire.redacted_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commentaire_from_guest?
|
2023-12-10 18:25:58 +01:00
|
|
|
groupe_gestionnaire ? false : commentaire.dossier.invites.map(&:email).include?(commentaire.email)
|
2022-04-25 12:41:01 +02:00
|
|
|
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
|
|
|
|
|
2023-12-10 18:25:58 +01:00
|
|
|
def delete_url
|
|
|
|
groupe_gestionnaire ? gestionnaire_groupe_gestionnaire_commentaire_path(groupe_gestionnaire, commentaire) : instructeur_commentaire_path(commentaire.dossier.procedure, commentaire.dossier, commentaire)
|
|
|
|
end
|
|
|
|
|
2022-05-18 12:41:01 +02:00
|
|
|
def highlight?
|
2023-12-10 18:25:58 +01:00
|
|
|
commentaire.persisted? && (messagerie_seen_at.nil? || messagerie_seen_at < commentaire.created_at)
|
2022-05-18 12:41:01 +02:00
|
|
|
end
|
2022-04-25 12:41:01 +02:00
|
|
|
end
|