Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions election-ui-javascript/src/components/PartyLinks/PartyLinks.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
.Party-links {
color: red;
display: grid;
grid-template-columns: repeat(3, 1fr);
list-style: none;
gap: 10px;
grid-template-rows: repeat(2, 1fr);
padding-inline-start: 0px;
}

.Party-links li {
padding: 1rem;
border: 3px solid #bb1919;
margin-top: 1rem;
}
.Party-links li a {
color: black;
display: inline-block;
transition: all 500ms;
}
.Party-links li a:hover {
transform: scale(1.1);
}
12 changes: 7 additions & 5 deletions election-ui-javascript/src/components/Scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import PartyLinks from "./PartyLinks";
function Scoreboard() {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const [results, setResults] = useState([]);
const [data, setData] = useState({results: [], isComplete: false});


async function getData() {
try {
setLoading(true);
const resultData = await fetchData();
setResults(resultData.results);
const theData = await fetchData();
setData({results: theData.results, isComplete: theData.isComplete})
setLoading(false);
} catch (e) {
setLoading(false);
Expand All @@ -37,9 +38,10 @@ function Scoreboard() {
error ? <h1>Error</h1> :
<>
<h1>Results</h1>
<Scorecard results={results} />
<a className="Scoreboard-refresh">Refresh</a>
<Scorecard data={data} />
<button className="Scoreboard-refresh" onClick={() => getData()}>Refresh</button>
<h1>Learn more about the parties...</h1>

<PartyLinks />
</>
}
Expand Down
51 changes: 49 additions & 2 deletions election-ui-javascript/src/components/Scorecard/Scorecard.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.Scorecard {
/* .Scorecard {
background-color: #eeeeee;
}

Expand All @@ -18,4 +18,51 @@

.Scorecard-table tbody tr:nth-of-type(2n + 1) {
background-color: white;
}
}

.winner{
background-color: yellow;
}


.badge {
background-color: red;
color: white;
padding: 4px 8px;
text-align: center;
border-radius: 5px;
} */

.Scorecard {
background-color: #eeeeee;
}

.Scorecard-table {
width: 100%;
& th,
& td {
padding: 0.5rem;
}
& thead {
background-color: #bb1919;
color: white;
}

& tbody {
& tr:nth-of-type(2n + 1) {
background-color: white;
}
}
}

.winner {
background-color: yellow;
}

.badge {
background-color: red;
color: white;
padding: 4px 8px;
text-align: center;
border-radius: 5px;
}
19 changes: 16 additions & 3 deletions election-ui-javascript/src/components/Scorecard/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import './Scorecard.css';

function Scorecard({ results }) {
function Scorecard({data}) {
const {results, isComplete} = data ;
if (!results || results.length === 0) {
return <div>No results</div>;
}

let scores = [];


//gets the winner
//const winner = results.reduce((max, { votes }) => Math.max(max, votes), 0);
const votesArr = results.map(({votes}) => votes);
const winner = Math.max(...votesArr);

const isWinner = (votes ,comp, winner ) => comp && votes === winner ;



for (let i=0; i < results.length; i++) {
const theWinner = isWinner(results[i].votes, isComplete, winner);
scores.push(
<tr key={i}>
<td>{results[i].party}</td>
<td>{results[i].candidateId}</td>
<td>{results[i].votes}</td>
<td>{results[i].name} </td>
<td className={theWinner && 'winner'}>{results[i].votes} {theWinner && <span className={'badge'}>The winner</span>}</td>
</tr>
)
}
Expand Down
12 changes: 9 additions & 3 deletions election-ui-javascript/src/dataFetcher/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { fetchResultData, fetchCandidateData } from '../fakeAPI'; // Let's imagine this is an external service that we are calling via https

async function fetchResults() {
const results = await fetchResultData();
const candidateData = fetchCandidateData();
const {results, isComplete} = await fetchResultData();
const candidateData = fetchCandidateData();

const newResults = results.map((item, index) => {
return {...item, name: candidateData?.[index]?.name}
})

return results;
const theResults = {isComplete, results: newResults};

return theResults
}

export default fetchResults;