feat(gallery): update gallery item badge class after seen
This commit is contained in:
parent
d9f604e8ce
commit
3bc232e81e
4 changed files with 49 additions and 5 deletions
|
@ -2,11 +2,12 @@
|
|||
|
||||
class Attachment::GalleryItemComponent < ApplicationComponent
|
||||
include GalleryHelper
|
||||
attr_reader :attachment
|
||||
attr_reader :attachment, :seen_at
|
||||
|
||||
def initialize(attachment:, gallery_demande: false)
|
||||
def initialize(attachment:, gallery_demande: false, seen_at: nil)
|
||||
@attachment = attachment
|
||||
@gallery_demande = gallery_demande
|
||||
@seen_at = seen_at
|
||||
end
|
||||
|
||||
def blob
|
||||
|
@ -77,7 +78,7 @@ class Attachment::GalleryItemComponent < ApplicationComponent
|
|||
def badge_updated_class
|
||||
class_names(
|
||||
"fr-badge fr-badge--sm" => true,
|
||||
"fr-badge--new" => updated?
|
||||
"fr-badge--new" => seen_at.present? && updated_at&.>(seen_at)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -386,6 +386,7 @@ module Instructeurs
|
|||
.flatten
|
||||
|
||||
@gallery_attachments = champs_attachments + commentaires_attachments
|
||||
@pieces_jointes_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.pieces_jointes_seen_at
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -5,4 +5,4 @@
|
|||
.fr-container
|
||||
.gallery.gallery-pieces-jointes{ "data-controller": "lightbox" }
|
||||
- @gallery_attachments.each do |attachment|
|
||||
= render Attachment::GalleryItemComponent.new(attachment:)
|
||||
= render Attachment::GalleryItemComponent.new(attachment:, seen_at: @pieces_jointes_seen_at)
|
||||
|
|
|
@ -9,16 +9,23 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do
|
|||
let(:dossier) { create(:dossier, :with_populated_champs, :en_construction, procedure:) }
|
||||
let(:filename) { attachment.blob.filename.to_s }
|
||||
let(:gallery_demande) { false }
|
||||
let(:seen_at) { nil }
|
||||
let(:now) { Time.zone.parse('01/01/2010') }
|
||||
|
||||
let(:component) { described_class.new(attachment: attachment, gallery_demande:) }
|
||||
let(:component) { described_class.new(attachment: attachment, gallery_demande:, seen_at: seen_at) }
|
||||
|
||||
subject { render_inline(component).to_html }
|
||||
|
||||
after { Timecop.return }
|
||||
|
||||
context "when attachment is from a piece justificative champ" do
|
||||
let(:champ) { dossier.champs.first }
|
||||
let(:libelle) { champ.libelle }
|
||||
let(:attachment) { champ.piece_justificative_file.attachments.first }
|
||||
|
||||
# Correspond au cas standard où le blob est créé avant le dépôt du dossier
|
||||
before { dossier.touch(:depose_at) }
|
||||
|
||||
it "displays libelle, link, tag and renders title" do
|
||||
expect(subject).to have_text(libelle)
|
||||
expect(subject).not_to have_text('Pièce jointe au message')
|
||||
|
@ -28,9 +35,20 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do
|
|||
end
|
||||
|
||||
it "displays when gallery item has been added" do
|
||||
expect(subject).to have_text('Ajoutée le')
|
||||
expect(subject).not_to have_css('.fr-badge--new')
|
||||
expect(subject).to have_text(component.helpers.try_format_datetime(attachment.record.created_at, format: :veryshort))
|
||||
end
|
||||
|
||||
context "when gallery item has been updated" do
|
||||
# un nouveau blob est créé après modification d'un champ pièce justificative
|
||||
before { attachment.blob.touch(:created_at) }
|
||||
|
||||
it 'displays the right text' do
|
||||
expect(subject).to have_text('Modifiée le')
|
||||
end
|
||||
end
|
||||
|
||||
context "when gallery item is in page Demande" do
|
||||
let(:gallery_demande) { true }
|
||||
|
||||
|
@ -51,6 +69,30 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do
|
|||
expect(subject).to have_text('Messagerie (usager)')
|
||||
expect(component.title).to eq("Pièce jointe au message -- #{filename}")
|
||||
end
|
||||
|
||||
context "when instructeur has not seen it yet" do
|
||||
let(:seen_at) { Timecop.freeze(now - 1.day) }
|
||||
|
||||
before do
|
||||
attachment.blob.update(created_at: Timecop.freeze(now))
|
||||
end
|
||||
|
||||
it 'displays datetime in the right style' do
|
||||
expect(subject).to have_css('.fr-badge--new')
|
||||
end
|
||||
end
|
||||
|
||||
context "when instructeur has already seen it" do
|
||||
let!(:seen_at) { Timecop.freeze(now) }
|
||||
|
||||
before do
|
||||
attachment.blob.touch(:created_at)
|
||||
end
|
||||
|
||||
it 'displays datetime in the right style' do
|
||||
expect(subject).not_to have_css('.fr-badge--new')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'from an instructeur' do
|
||||
|
|
Loading…
Reference in a new issue