Skip to content

Commit 0d7ae3e

Browse files
authored
Merge pull request #1702 from codidact/0valt/1389/threads
Comment thread inputs no longer enable the submit button when the default comment's body is not filled in
2 parents 29b0054 + 84446c6 commit 0d7ae3e

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

app/assets/javascripts/character_count.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ $(() => {
5858
$(document).on('keyup change paste', '[data-character-count]', (ev) => {
5959
const $tgt = $(ev.target);
6060
const $counter = $($tgt.attr('data-character-count'));
61-
const $button = $counter.parents('form').find('input[type="submit"],.js-suggested-edit-approve');
61+
const $form = $counter.parents('form');
62+
const $button = $form.find('input[type="submit"],.js-suggested-edit-approve');
6263
const $count = $counter.find('.js-character-count__count');
6364
const $icon = $counter.find('.js-character-count__icon');
6465

@@ -76,19 +77,24 @@ $(() => {
7677
if (gtnMax || ltnMin) {
7778
setCounterState($counter, 'error');
7879
setCounterIcon($icon, 'fa-times');
79-
setSubmitButtonDisabledState($button, 'disabled');
8080
setInputValidationState($tgt, 'invalid');
8181
} else if (gteThreshold) {
8282
setCounterState($counter, 'warning');
8383
setCounterIcon($icon, 'fa-exclamation-circle');
84-
setSubmitButtonDisabledState($button, 'enabled');
8584
} else {
8685
setCounterState($counter, 'default');
8786
setCounterIcon($icon, 'fa-check');
88-
setSubmitButtonDisabledState($button, 'enabled');
8987
setInputValidationState($tgt, 'valid');
9088
}
9189

90+
const submittable = $form[0]?.checkValidity() ?? false;
91+
92+
if (!submittable || gtnMax || ltnMin) {
93+
setSubmitButtonDisabledState($button, 'disabled');
94+
} else {
95+
setSubmitButtonDisabledState($button, 'enabled');
96+
}
97+
9298
$count.text(text);
9399
});
94100

app/controllers/comments_controller.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ def create_thread
3232
pings = check_for_pings @comment_thread, body
3333

3434
success = ActiveRecord::Base.transaction do
35-
@comment_thread.save!
36-
@comment.save!
35+
thread_success = @comment_thread.save
36+
comment_success = @comment.save
37+
full_success = thread_success && comment_success
38+
39+
unless full_success
40+
raise ActiveRecord::Rollback
41+
end
42+
43+
full_success
3744
end
3845

3946
if success

app/views/comments/_new_thread_modal.html.erb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Start new comment thread dialog.
33
%>
44

5+
<%
6+
# TODO: make configurable
7+
min_chars = 15
8+
max_chars = 1000
9+
%>
10+
511
<div class="modal--container new-thread-modal" id="new-thread-modal-<%= post.id %>">
612
<div class="modal--header">
713
<b>Start a new comment thread</b>
@@ -18,9 +24,15 @@
1824

1925
<%= label_tag :body, 'Your comment', class: 'form-element' %>
2026
<div class="form-caption">Start the thread with a comment.</div>
21-
<%= text_area_tag :body, '', class: 'form-element js-comment-field', required: true,
22-
data: { post: post.id, thread: '-1', character_count: ".js-character-count-#{post.id}" } %>
23-
<%= render 'shared/char_count', type: post.id, min: 15, max: 1000 %>
27+
<%= text_area_tag :body, '',
28+
class: 'form-element js-comment-field',
29+
minlength: min_chars,
30+
maxlength: max_chars,
31+
required: true,
32+
data: { post: post.id,
33+
thread: '-1',
34+
character_count: ".js-character-count-#{post.id}" } %>
35+
<%= render 'shared/char_count', type: post.id, min: min_chars, max: max_chars %>
2436

2537
<%= label_tag :title, 'Comment thread title (optional)', class: 'form-element' %>
2638
<span class="form-caption">

0 commit comments

Comments
 (0)