forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonContainer.jsx
More file actions
51 lines (45 loc) · 1.42 KB
/
ButtonContainer.jsx
File metadata and controls
51 lines (45 loc) · 1.42 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
47
48
49
50
51
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { connect } from 'react-redux';
import Button from './Button';
import { submitAnswer } from '../actions/submitAnswer';
import { getCurrent, getPatterns } from '../selectors';
import { shuffle } from '../helpers/shuffleArray';
const StyledButtonContainer = styled.div`
align-content: space-around;
display: flex;
flex-wrap: wrap;
height: 9rem;
justify-content: space-around;
margin: 1rem 0 2rem;
`;
export const ButtonContainer = props => {
const { current, patterns, onSubmitAnswer } = props;
// get 3 random patterns in addition to correct one
const allOtherAnswers = patterns.filter(pattern => pattern.uuid !== current.uuid);
const additional = shuffle(allOtherAnswers).slice(0, 3);
// shuffle the 4 answers
const variants = shuffle([current, ...additional]);
return (
<StyledButtonContainer>
{variants.map(({ uuid, name }) => (
<Button label={name} id={uuid} key={uuid} onClick={() => onSubmitAnswer(uuid)} />
))}
</StyledButtonContainer>
);
};
ButtonContainer.propTypes = {
patterns: PropTypes.array.isRequired,
current: PropTypes.object.isRequired,
onSubmitAnswer: PropTypes.func.isRequired
};
export default connect(
state => ({
current: getCurrent(state),
patterns: getPatterns(state)
}),
{
onSubmitAnswer: id => submitAnswer(id)
}
)(ButtonContainer);