Skip to content

Commit

Permalink
[Fix #1016] display notifications for new messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagn committed Dec 12, 2017
1 parent 6f1951d commit d744685
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
18 changes: 17 additions & 1 deletion app/assets/javascripts/new_design/messagerie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
TPS.scrollMessagerie = function () {
var scrollTo = function ($container, $scrollTo) {
$container.scrollTop(
$scrollTo.offset().top - $container.offset().top + $container.scrollTop()
);
}

var scrollToBottom = function ($container) {
$container.scrollTop($container.prop('scrollHeight'));
}

var $ul = $(".messagerie ul").first();
if($ul.length) {
$ul.scrollTop($ul.prop('scrollHeight'));
var $elementToScroll = $('.date.highlighted').first();

if ($elementToScroll.length != 0) {
scrollTo($ul, $elementToScroll);
} else {
scrollToBottom($ul);
}
}
};

Expand Down
9 changes: 7 additions & 2 deletions app/controllers/new_gestionnaire/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class DossiersController < ProceduresController
include ActionView::Helpers::TextHelper

after_action :mark_demande_as_read, only: :show
after_action :mark_messagerie_as_read, only: [:messagerie, :create_commentaire]

def attestation
send_data(dossier.attestation.pdf.read, filename: 'attestation.pdf', type: 'application/pdf')
Expand All @@ -14,9 +15,8 @@ def show
end

def messagerie
dossier.notifications.messagerie.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :messagerie)
@commentaire = Commentaire.new
@messagerie_seen_at = current_gestionnaire.follows.find_by(dossier: dossier)&.messagerie_seen_at
end

def annotations_privees
Expand Down Expand Up @@ -198,5 +198,10 @@ def mark_demande_as_read
dossier.notifications.demande.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :demande)
end

def mark_messagerie_as_read
dossier.notifications.messagerie.mark_as_read
current_gestionnaire.mark_tab_as_seen(dossier, :messagerie)
end
end
end
2 changes: 1 addition & 1 deletion app/views/new_gestionnaire/avis/messagerie.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
%ul.messages-list
- @dossier.commentaires.each do |commentaire|
%li
= render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire }
= render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire, messagerie_seen_at: nil }

= render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_avis_path(@avis) }
2 changes: 1 addition & 1 deletion app/views/new_gestionnaire/dossiers/messagerie.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
%ul.messages-list
- @dossier.commentaires.each do |commentaire|
%li
= render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire }
= render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire, messagerie_seen_at: @messagerie_seen_at }

= render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_dossier_path(@dossier.procedure, @dossier) }
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
%h2
%span.mail
= render partial: 'new_gestionnaire/shared/commentaires/commentaire_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire }
- if ![current_gestionnaire.email, @dossier.user.email, '[email protected]'].include?(commentaire.email)
- if ![current_gestionnaire.email, commentaire.dossier.user.email, '[email protected]'].include?(commentaire.email)
%span.guest Invité
%span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y')
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
= I18n.l(commentaire.created_at.localtime, format: '%d/%m/%Y à %H:%M ')
.rich-text= sanitize(commentaire.body)

- if commentaire.piece_justificative
Expand Down
28 changes: 28 additions & 0 deletions spec/helpers/dossier_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rails_helper'

RSpec.describe DossierHelper, type: :helper do
describe ".highlight_if_unseen_class" do
let(:seen_at) { DateTime.now }

subject { highlight_if_unseen_class(seen_at, updated_at) }

context "when commentaire date is created before last seen datetime" do
let(:updated_at) { seen_at - 2.days }

it { is_expected.to eq nil }
end

context "when commentaire date is created after last seen datetime" do
let(:updated_at) { seen_at + 2.hours }

it { is_expected.to eq "highlighted" }
end

context "when there is no last seen datetime" do
let(:updated_at) { DateTime.now }
let(:seen_at) { nil }

it { is_expected.to eq nil }
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe 'new_gestionnaire/shared/commentaires/commentaire.html.haml', type: :view do
before { view.extend DossierHelper }

subject { render 'new_gestionnaire/shared/commentaires/commentaire.html.haml', commentaire: commentaire, messagerie_seen_at: seen_at, current_gestionnaire: current_gestionnaire }

let(:dossier) { create(:dossier) }
let(:commentaire) { create(:commentaire, dossier: dossier) }
let(:current_gestionnaire) { create(:gestionnaire) }
let(:seen_at) { commentaire.created_at + 1.hour }

context "with a seen_at after commentaire created_at" do
let(:seen_at) { commentaire.created_at + 1.hour }

it { is_expected.not_to have_css(".highlighted") }
end

context "with a seen_at after commentaire created_at" do
let(:seen_at) { commentaire.created_at - 1.hour }

it { is_expected.to have_css(".highlighted") }
end
end

0 comments on commit d744685

Please sign in to comment.