From dc35f8ed24a137ec0bf947af9a6076aaf061ed6d Mon Sep 17 00:00:00 2001 From: Daniel Uber Date: Wed, 3 Nov 2021 13:04:28 -0400 Subject: [PATCH] Remove optional chaining syntax from comments page initializer (#15277) * Remove questionable syntax from comments initializer Not sure what ?. is, changing to . to placate the asset precompiler * Restore conditional access Rather than changing the ?. to ., wrap the chained access in a guard clause. * Fix error in logic First pass had wrapped this in an "if currentUser" which caused the else block only to hide comments when no user was logged in, however the actual behavior should have been to hide comments unless current user was an article author. --- .../initializeCommentsPage.js.erb | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb index f24141889..5ddcf132e 100644 --- a/app/assets/javascripts/initializers/initializeCommentsPage.js.erb +++ b/app/assets/javascripts/initializers/initializeCommentsPage.js.erb @@ -602,23 +602,27 @@ function handleHiddenComments(commentableType){ if(commentableType === "Article"){ const articleContainer = document.querySelector('#article-show-container'); if(articleContainer){ - commentableAuthorIds.push(articleContainer?.dataset?.authorId); - coAuthorIds = articleContainer?.dataset?.coAuthorIds; - if(coAuthorIds){ - coAuthorIds.split(',').forEach(coAuthorId => { - commentableAuthorIds.push(coAuthorId); - }); + if (articleContainer.dataset) { + commentableAuthorIds.push(articleContainer.dataset.authorId); + coAuthorIds = articleContainer.dataset.coAuthorIds; + if(coAuthorIds){ + coAuthorIds.split(',').forEach(coAuthorId => { + commentableAuthorIds.push(coAuthorId); + }); + } } } else { const commentsContainer = document.querySelector('#comments-container'); if(commentsContainer){ - commentableAuthorIds.push(commentsContainer?.dataset?.commentableAuthorId); - coAuthorIds = commentsContainer?.dataset?.commentableCoAuthorIds; - if(coAuthorIds){ - coAuthorIds.split(',').forEach(coAuthorId => { - commentableAuthorIds.push(coAuthorId); - }); + if(commentsContainer.dataset) { + commentableAuthorIds.push(commentsContainer.dataset.commentableAuthorId); + coAuthorIds = commentsContainer.dataset.commentableCoAuthorIds; + if(coAuthorIds){ + coAuthorIds.split(',').forEach(coAuthorId => { + commentableAuthorIds.push(coAuthorId); + }); + } } } } @@ -628,7 +632,7 @@ function handleHiddenComments(commentableType){ commentableAuthorIds.push(podCastEpisodeContainer.dataset.creatorId); } } - if (commentableAuthorIds.includes(currentUser?.id?.toString())){ + if(currentUser && commentableAuthorIds.includes(currentUser.id.toString())){ collapseCommentsHiddenByCommentableUser(); } else {