From 16b083013923b6f842d210381603220820fff7ad Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 5 Apr 2021 18:59:44 -0400 Subject: [PATCH] Implement progress bar --- package.json | 1 + public/index.html | 2 +- src/index.js | 111 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 95 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 2180e84..d840c5d 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "my-app", "version": "0.1.0", "private": true, + "homepage": "/makeachoice", "dependencies": { "@testing-library/jest-dom": "^5.11.10", "@testing-library/react": "^11.2.6", diff --git a/public/index.html b/public/index.html index 3431b23..8a9a5b0 100644 --- a/public/index.html +++ b/public/index.html @@ -19,7 +19,7 @@
-

Make a choice

+

Make a choice

diff --git a/src/index.js b/src/index.js index 488cd99..9849b6e 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,22 @@ function shuffleArray(array) { } } +function choicesToMarkdown(choices) { + const choiceNames = choices.map((c) => c.choice); + const choiceWidth = Math.max( + ...choiceNames.concat("Choice").map((x) => x.length + 3) + ); + const countWidth = 6; + + let r = "| Choice".padEnd(choiceWidth, " ") + "| Count |\n"; + r += "|".padEnd(choiceWidth, "-") + "|-------|\n"; + for (const c of choices) { + r += ("| " + c.choice).padEnd(choiceWidth, " ") + "| "; + r += c.count.toString().padStart(countWidth, " ") + "|\n"; + } + return r; +} + class Decision { constructor(indexA, indexB, choices) { this.indexA = indexA; @@ -37,7 +53,7 @@ class Options extends React.Component { constructor(props) { super(props); this.state = { - choicesText: null, + choicesText: "", choices: [], decisions: null, decisionsMade: null, @@ -75,27 +91,49 @@ class Options extends React.Component { }); } + startAgain() { + this.setState({ + decisions: null, + currentDecision: null, + decisionsMade: null, + }); + } + renderChoosing() { const current = this.state.currentDecision; const decision = this.state.decisions[current]; + const progress = (current / this.state.decisions.length) * 100; - return ( -
+ let choiceA = decision.choiceA; + let choiceB = decision.choiceB; + const maxChoiceLetters = Math.max(choiceA.length, choiceB.length); + + let fullText = null; + if (maxChoiceLetters > 20) { + fullText = (
-

{decision.choiceA}

+

{choiceA}

-

{decision.choiceB}

+

{choiceB}

+ ); + choiceA = choiceA.slice(0, 20) + "..."; + choiceB = choiceB.slice(0, 20) + "..."; + } + + return ( +
+ {fullText}
+
+ + {" "} + +
); } @@ -146,9 +189,10 @@ class Options extends React.Component {
+ />
@@ -190,15 +234,22 @@ class Options extends React.Component { return { choice: choice, count: 0, + relativeCount: 0, }; }); + let totalCount = 0; for (const i of this.state.decisionsMade) { if (i !== -1) { choices[i].count += 1; + totalCount += 1; } } + for (let c of choices) { + c.relativeCount = (c.count / totalCount) * 100; + } + choices.sort((a, b) => b.count - a.count); let choicesToRender = choices.map((choice, index) => { @@ -206,21 +257,45 @@ class Options extends React.Component { {choice.choice} {choice.count} + + + ); }); + let choicesMarkdown = choicesToMarkdown(choices); + return ( -
- - - - - - - - {choicesToRender} -
ChoiceCount
+
+
+ + + + + + + + + {choicesToRender} +
ChoiceCount
+
+ +
+ +
); }