Skip to content

Commit 257f467

Browse files
committed
Merge remote-tracking branch 'upstream/pull/5346'
2 parents 5410fb6 + aca9bd2 commit 257f467

File tree

4 files changed

+102
-22
lines changed

4 files changed

+102
-22
lines changed

app/assets/javascripts/index/note.js

+27-21
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,35 @@ OSM.Note = function (map) {
3737
};
3838

3939
function initialize(path, id) {
40-
content.find("button[type=submit]").on("click", function (e) {
40+
content.find("button[name]").on("click", function (e) {
4141
e.preventDefault();
4242
var data = $(e.target).data();
43-
var form = e.target.form;
44-
45-
$(form).find("button[type=submit]").prop("disabled", true);
46-
47-
$.ajax({
43+
var name = $(e.target).attr("name");
44+
var ajaxSettings = {
4845
url: data.url,
4946
type: data.method,
5047
oauth: true,
51-
data: { text: $(form.text).val() },
52-
success: function () {
53-
OSM.loadSidebarContent(path, function () {
48+
success: () => {
49+
OSM.loadSidebarContent(path, () => {
5450
initialize(path, id);
5551
moveToNote();
5652
});
5753
},
58-
error: function (xhr) {
59-
$(form).find("#comment-error")
54+
error: (xhr) => {
55+
content.find("#comment-error")
6056
.text(xhr.responseText)
61-
.prop("hidden", false);
62-
updateButtons(form);
57+
.prop("hidden", false)
58+
.get(0).scrollIntoView({ block: "nearest" });
59+
updateButtons();
6360
}
64-
});
61+
};
62+
63+
if (name !== "subscribe" && name !== "unsubscribe") {
64+
ajaxSettings.data = { text: $("textarea").val() };
65+
}
66+
67+
content.find("button[name]").prop("disabled", true);
68+
$.ajax(ajaxSettings);
6569
});
6670

6771
content.find("textarea").on("input", function (e) {
@@ -82,14 +86,16 @@ OSM.Note = function (map) {
8286
}
8387
}
8488

85-
function updateButtons(form) {
86-
$(form).find("button[type=submit]").prop("disabled", false);
87-
if ($(form.text).val() === "") {
88-
$(form.close).text($(form.close).data("defaultActionText"));
89-
$(form.comment).prop("disabled", true);
89+
function updateButtons() {
90+
var resolveButton = content.find("button[name='close']");
91+
var commentButton = content.find("button[name='comment']");
92+
93+
content.find("button[name]").prop("disabled", false);
94+
if (content.find("textarea").val() === "") {
95+
resolveButton.text(resolveButton.data("defaultActionText"));
96+
commentButton.prop("disabled", true);
9097
} else {
91-
$(form.close).text($(form.close).data("commentActionText"));
92-
$(form.comment).prop("disabled", false);
98+
resolveButton.text(resolveButton.data("commentActionText"));
9399
}
94100
}
95101

app/views/notes/show.html.erb

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@
2828
<p class='alert alert-warning'><%= t ".anonymous_warning" %></p>
2929
<% end -%>
3030

31+
<div class="row">
32+
<div class="col">
33+
<h4><%= t(".discussion") %></h4>
34+
</div>
35+
36+
<% if current_user %>
37+
<div class="col-auto">
38+
<% if @note.subscribers.exists?(current_user.id) %>
39+
<%= tag.button t(".unsubscribe"),
40+
:type => "button",
41+
:class => "btn btn-sm btn-primary",
42+
:name => "unsubscribe",
43+
:data => { :method => "DELETE",
44+
:url => api_note_subscription_path(@note) } %>
45+
<% else %>
46+
<%= tag.button t(".subscribe"),
47+
:type => "button",
48+
:class => "btn btn-sm btn-primary",
49+
:name => "subscribe",
50+
:data => { :method => "POST",
51+
:url => api_note_subscription_path(@note) } %>
52+
<% end %>
53+
</div>
54+
<% end %>
55+
</div>
56+
3157
<% if @note_comments.length > 1 %>
3258
<div class='note-comments'>
3359
<ul class="list-unstyled">

config/locales/en.yml

+3
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,9 @@ en:
29892989
report: report this note
29902990
coordinates_html: "%{latitude}, %{longitude}"
29912991
anonymous_warning: This note includes comments from anonymous users which should be independently verified.
2992+
discussion: Discussion
2993+
subscribe: Subscribe
2994+
unsubscribe: Unsubscribe
29922995
hide: Hide
29932996
resolve: Resolve
29942997
reactivate: Reactivate

test/system/note_comments_test.rb

+46-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class NoteCommentsTest < ApplicationSystemTestCase
2222
end
2323
end
2424

25-
def test_add_comment
25+
test "can add comment" do
2626
note = create(:note_with_comments)
2727
user = create(:user)
2828
sign_in_as(user)
@@ -125,4 +125,49 @@ def test_add_comment
125125
assert_button "Reactivate", :disabled => false
126126
end
127127
end
128+
129+
test "no subscribe button when not logged in" do
130+
note = create(:note_with_comments)
131+
visit note_path(note)
132+
133+
within_sidebar do
134+
assert_no_button "Subscribe"
135+
assert_no_button "Unsubscribe"
136+
end
137+
end
138+
139+
test "can subscribe" do
140+
note = create(:note_with_comments)
141+
user = create(:user)
142+
sign_in_as(user)
143+
visit note_path(note)
144+
145+
within_sidebar do
146+
assert_button "Subscribe"
147+
assert_no_button "Unsubscribe"
148+
149+
click_on "Subscribe"
150+
151+
assert_no_button "Subscribe"
152+
assert_button "Unsubscribe"
153+
end
154+
end
155+
156+
test "can unsubscribe" do
157+
note = create(:note_with_comments)
158+
user = create(:user)
159+
create(:note_subscription, :note => note, :user => user)
160+
sign_in_as(user)
161+
visit note_path(note)
162+
163+
within_sidebar do
164+
assert_no_button "Subscribe"
165+
assert_button "Unsubscribe"
166+
167+
click_on "Unsubscribe"
168+
169+
assert_button "Subscribe"
170+
assert_no_button "Unsubscribe"
171+
end
172+
end
128173
end

0 commit comments

Comments
 (0)