From 513c177698679e64fe8183766997ada43913d9de Mon Sep 17 00:00:00 2001 From: Glen Wider Date: Mon, 26 Apr 2021 19:10:00 +0200 Subject: [PATCH] improve CommentViewer getHeader() function --- src/components/CodeReview.tsx | 2 +- src/components/CommentViewer.tsx | 56 +++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/components/CodeReview.tsx b/src/components/CodeReview.tsx index 64ca99d..37bacad 100644 --- a/src/components/CodeReview.tsx +++ b/src/components/CodeReview.tsx @@ -110,7 +110,7 @@ export const CodeReview: React.FC = ({ )} -
+
onCommentCreated(createComment(value, author))} diff --git a/src/components/CommentViewer.tsx b/src/components/CommentViewer.tsx index 4d5dc97..a75b081 100644 --- a/src/components/CommentViewer.tsx +++ b/src/components/CommentViewer.tsx @@ -45,15 +45,61 @@ export const CommentViewer: React.FC = ({comments, setOldToggleState(toggle) }, [toggle, oldToggleState, activeKey, toggleCollapse]) + const countInfos = () => { + let count = 0; + comments.forEach(element => { + if(element.type === ("mildInfo" || "severeInfo")) { + count++ + } + }) + return count + } + + const countComments = () => { + let count = 0; + comments.forEach(element => { + if(element.type === "comment") { + count++ + } + }) + return count + } + + const getCommentWording = () => { + if(countComments() === 1) { + return " comment" + } else { + return " comments" + } + } + + const getInfoWording = () => { + if(countInfos() === 1) { + return " info" + } else { + return " infos" + } + } + const getHeader = () => { if(result) { return "Result" } - const commentNumber = comments.length; - if(commentNumber === 1) { - return commentNumber + " comment"; - } else { - return commentNumber + " comments"; + + if(countComments() > 0) { + if(countInfos() > 0) { + return countComments() + + getCommentWording() + + ", " + + countInfos() + + getInfoWording() + } else { + return countComments() + + getCommentWording() + } + } + if(countInfos() > 0) { + return countInfos() + getInfoWording() } }