Skip to content

Commit 27cdfcc

Browse files
authored
Refactor (#3)
1 parent 702c5bd commit 27cdfcc

File tree

5 files changed

+26
-40
lines changed

5 files changed

+26
-40
lines changed

routers/web/repo/pull.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -771,10 +771,7 @@ func ViewPullFiles(ctx *context.Context) {
771771
}
772772
}
773773
ctx.Data["CurrentReview"] = currentReview
774-
ctx.Data["PendingCodeComments"] = numPendingCodeComments
775-
ctx.PageData["prReview"] = map[string]interface{}{
776-
"pendingCodeComments": numPendingCodeComments,
777-
}
774+
ctx.Data["PendingCodeCommentNumber"] = numPendingCodeComments
778775

779776
getBranchData(ctx, issue)
780777
ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.Doer.ID)

templates/repo/diff/new_review.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="ui top right pointing dropdown custom" id="review-box">
22
<div class="ui tiny green button btn-review">
33
{{.i18n.Tr "repo.diff.review"}}
4-
<span class="ui small label review-comments-counter" {{if eq .PendingCodeComments 0}}style="display: none"{{end}}>{{if gt .PendingCodeComments 0}}{{.PendingCodeComments}}{{end}}</span>
4+
<span class="ui small label review-comments-counter" data-pending-comment-number="{{.PendingCodeCommentNumber}}">{{.PendingCodeCommentNumber}}</span>
55
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
66
</div>
77
<div class="menu review-box">

web_src/js/features/repo-diff.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,25 @@ import {initCompReactionSelector} from './comp/ReactionSelector.js';
33
import {initRepoIssueContentHistory} from './repo-issue-content.js';
44
import {validateTextareaNonEmpty} from './comp/EasyMDE.js';
55

6-
const {csrfToken, pageData} = window.config;
7-
const prReview = pageData.prReview || {};
6+
const {csrfToken} = window.config;
87

98
export function initRepoDiffReviewButton() {
10-
const reviewBox = document.querySelector('#review-box');
9+
const $reviewBox = $('#review-box');
10+
const $counter = $reviewBox.find('.review-comments-counter');
11+
1112
$(document).on('click', 'button[name="is_review"]', (e) => {
12-
$(e.target).closest('form').append('<input type="hidden" name="is_review" value="true">');
13+
const $form = $(e.target).closest('form');
14+
$form.append('<input type="hidden" name="is_review" value="true">');
1315

1416
// Watch for the form's submit event.
15-
e.target.closest('form').addEventListener('submit', () => {
16-
// Set a zero-timeout, so this would be executed after the network request has finished.
17-
setTimeout(() => {
18-
const commentCounter = document.querySelector('#review-box .review-comments-counter');
19-
// Remove the display: none.
20-
commentCounter.style.display = '';
21-
// Increase counter by one, in case it's the first review, `pendingCodeComments` will
22-
// return undefined so it will default to '1'.
23-
prReview.pendingCodeComments = prReview.pendingCodeComments + 1 || 1;
24-
commentCounter.textContent = String(prReview.pendingCodeComments);
25-
26-
// Make the review-box to do a little pulse.
27-
reviewBox.classList.remove('pulse');
28-
// Force the browser to reflow the DOM. This is to ensure that the browser.
29-
// Actually removes the 'pulse' class from the DOM. Otherwise the browser
30-
// is smart enough to de-duplicate these two requests.
31-
const _ = reviewBox.offsetWidth;
32-
// Add the class again.
33-
reviewBox.classList.add('pulse');
34-
});
17+
$form.on('submit', () => {
18+
const num = parseInt($counter.attr('data-pending-comment-number')) + 1 || 1;
19+
$counter.attr('data-pending-comment-number', num);
20+
$counter.text(num);
21+
// Force the browser to reflow the DOM. This is to ensure that the browser replay the animation
22+
$reviewBox.removeClass('pulse');
23+
$reviewBox.width();
24+
$reviewBox.addClass('pulse');
3525
});
3626
});
3727
}

web_src/js/features/repo-issue.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {createCommentEasyMDE, getAttachedEasyMDE} from './comp/EasyMDE.js';
55
import {initCompImagePaste} from './comp/ImagePaste.js';
66
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
77

8-
const {appSubUrl, csrfToken, pageData} = window.config;
9-
const prReview = pageData.prReview || {};
8+
const {appSubUrl, csrfToken} = window.config;
109

1110
export function initRepoIssueTimeTracking() {
1211
$(document).on('click', '.issue-add-time', () => {
@@ -164,15 +163,11 @@ export function initRepoIssueCommentDelete() {
164163

165164
// Check if this was a pending comment.
166165
if ($conversationHolder.find('.pending-label').length) {
167-
// This is a pending comment.
168-
const commentCounter = document.querySelector('#review-box .review-comments-counter');
169-
// Decrease the counter by one.
170-
prReview.pendingCodeComments -= 1;
171-
commentCounter.textContent = String(prReview.pendingCodeComments);
172-
// If no pending comments remains, don't show the counter.
173-
if (commentCounter.textContent === '0') {
174-
commentCounter.style.display = 'none';
175-
}
166+
const $counter = $('#review-box .review-comments-counter');
167+
let num = parseInt($counter.attr('data-pending-comment-number')) - 1 || 0;
168+
num = Math.max(num, 0);
169+
$counter.attr('data-pending-comment-number', num);
170+
$counter.text(num);
176171
}
177172

178173
$(`#${$this.data('comment-id')}`).remove();

web_src/less/_review.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ a.blob-excerpt:hover {
251251
background-color: var(--color-primary-light-5);
252252
}
253253

254+
#review-box .review-comments-counter[data-pending-comment-number="0"] {
255+
display: none;
256+
}
257+
254258
.pull.files.diff [id] {
255259
scroll-margin-top: 99px;
256260

0 commit comments

Comments
 (0)