|
1 | 1 | import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { connect } from 'react-redux'; |
2 | 4 | import styled from 'styled-components'; |
3 | 5 | import { Link } from 'react-router-dom'; |
| 6 | +import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'; |
| 7 | +import js from 'react-syntax-highlighter/dist/languages/hljs/javascript'; |
| 8 | +import styleLight from '../styles/hljs/hljs.light'; |
| 9 | +import styleDark from '../styles/hljs/hljs.dark'; |
| 10 | +import { patterns } from '../static/patterns'; |
| 11 | +import { getMode } from '../selectors'; |
4 | 12 |
|
5 | | -const StyledPattern = styled.div``; |
| 13 | +SyntaxHighlighter.registerLanguage('javascript', js); |
| 14 | + |
| 15 | +const StyledPattern = styled.div` |
| 16 | + color: ${props => props.theme.text}; |
| 17 | +
|
| 18 | + h2, |
| 19 | + h3 { |
| 20 | + color: ${props => props.theme.header}; |
| 21 | + margin-top: 2.5rem; |
| 22 | + } |
| 23 | +`; |
| 24 | + |
| 25 | +const Memo = styled.span` |
| 26 | + color: ${props => props.theme.ATLANTIS}; |
| 27 | + display: block; |
| 28 | +`; |
| 29 | + |
| 30 | +const StyledLink = styled(Link)` |
| 31 | + border-bottom: 1px solid ${props => props.theme.CRIMSON}; |
| 32 | + color: ${props => props.theme.CRIMSON}; |
| 33 | + display: inline-block; |
| 34 | + margin-top: 2rem; |
| 35 | + text-decoration: none; |
| 36 | +
|
| 37 | + &:hover { |
| 38 | + border-bottom: 1px solid transparent; |
| 39 | + } |
| 40 | +`; |
| 41 | + |
| 42 | +const Pattern = ({ match, mode }) => { |
| 43 | + const { |
| 44 | + params: { id } |
| 45 | + } = match; |
| 46 | + |
| 47 | + const pattern = patterns.filter(item => item.id === id)[0]; |
| 48 | + |
| 49 | + const style = mode === 'dark' ? styleDark : styleLight; |
6 | 50 |
|
7 | | -const Pattern = props => { |
8 | 51 | return ( |
9 | 52 | <StyledPattern> |
10 | | - <Link to="/patterns">Back to Patterns List</Link> |
11 | | - <h1>{props.match.params.id}</h1> |
| 53 | + <StyledLink to="/patterns">← Back to Patterns List</StyledLink> |
| 54 | + {pattern && ( |
| 55 | + <React.Fragment> |
| 56 | + <h2>{pattern.name}</h2> |
| 57 | + <p> |
| 58 | + <Memo>Type:</Memo> |
| 59 | + {pattern.type} pattern |
| 60 | + </p> |
| 61 | + <p> |
| 62 | + <Memo>Description:</Memo> |
| 63 | + {`This pattern ${pattern.hint}.`} |
| 64 | + </p> |
| 65 | + |
| 66 | + <h3>ES5</h3> |
| 67 | + <SyntaxHighlighter language="javascript" style={style}> |
| 68 | + {pattern.codeES5} |
| 69 | + </SyntaxHighlighter> |
| 70 | + |
| 71 | + <h3>ES6</h3> |
| 72 | + <SyntaxHighlighter language="javascript" style={style}> |
| 73 | + {pattern.codeES6} |
| 74 | + </SyntaxHighlighter> |
| 75 | + </React.Fragment> |
| 76 | + )} |
12 | 77 | </StyledPattern> |
13 | 78 | ); |
14 | 79 | }; |
15 | 80 |
|
16 | | -export default Pattern; |
| 81 | +Pattern.propTypes = { |
| 82 | + match: PropTypes.object.isRequired, |
| 83 | + mode: PropTypes.string.isRequired |
| 84 | +}; |
| 85 | + |
| 86 | +export default connect(state => ({ |
| 87 | + mode: getMode(state) |
| 88 | +}))(Pattern); |
0 commit comments