forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercentage.jsx
More file actions
46 lines (38 loc) · 1.01 KB
/
Percentage.jsx
File metadata and controls
46 lines (38 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { connect } from 'react-redux';
import { getAnswers } from '../selectors';
const StyledPercentage = styled.h1`
border: 4px solid ${props => props.level};
border-radius: 50%;
color: ${props => props.level};
font-family: 'Karla', 'sans-serif';
font-size: 4rem;
height: 10rem;
margin: auto;
line-height: 10rem;
text-align: center;
width: 10rem;
`;
export const Percentage = ({ answers }) => {
let level = 'red';
let correct = 0;
answers.map(answer => (answer.correct ? correct++ : null));
const percent = Math.ceil((correct * 100) / 23);
switch (true) {
case percent >= 70:
level = 'limegreen';
break;
case percent >= 40:
level = 'orange';
break;
}
return <StyledPercentage level={level}>{percent}%</StyledPercentage>;
};
Percentage.propTypes = {
answers: PropTypes.array.isRequired
};
export default connect(state => ({
answers: getAnswers(state)
}))(Percentage);