Skip to content

Commit

Permalink
Merge pull request #30 from David-Kasilia/commentsCounter
Browse files Browse the repository at this point in the history
Adds function for counting comments:
  • Loading branch information
HabibPayenda committed Sep 2, 2022
2 parents 2952306 + e92a4b3 commit de26fe6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/modules/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ const postComment = async (options) => {
commentsShow(options.item_id);
};

const commentsCounter = (data) => {
let count = 0;
if (data && data.length > 0) {
count = data.length;
}
return count;
};

const showModal = (item) => {
const commentsModal = document.createElement('div');
commentsModal.classList.add('commentsModal');
Expand Down Expand Up @@ -73,7 +81,6 @@ const showModal = (item) => {

const commentsTitle = document.createElement('h2');
commentsTitle.classList.add('commentsTitle');
commentsTitle.innerText = 'Comments (0)';

const comments = document.createElement('ul');
comments.classList.add('comments');
Expand All @@ -85,6 +92,7 @@ const showModal = (item) => {
commentsModal.append(close, commentsImg, modalTitle, commentsDetails, commentsContainer);

showComments = (data) => {
const count = commentsCounter(data);
if (data && data.length > 0) {
comments.innerHTML = '';
data.forEach((item) => {
Expand All @@ -99,6 +107,8 @@ const showModal = (item) => {
comments.append(noComments);
}

commentsTitle.innerText = `Comments (${count})`;

commentsContainer.append(comments);
};

Expand Down

0 comments on commit de26fe6

Please sign in to comment.