forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPattern.jsx
More file actions
115 lines (99 loc) · 2.83 KB
/
Pattern.jsx
File metadata and controls
115 lines (99 loc) · 2.83 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import styled from 'styled-components';
import { Link } from 'react-router-dom';
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
import js from 'react-syntax-highlighter/dist/languages/hljs/javascript';
import styleLight from '../styles/hljs/hljs.light';
import styleDark from '../styles/hljs/hljs.dark';
import { patterns } from '../static/patterns';
import { restart } from '../actions/restart';
import { getMode } from '../selectors';
SyntaxHighlighter.registerLanguage('javascript', js);
const StyledPattern = styled.div`
color: ${props => props.theme.text};
h2,
h3 {
color: ${props => props.theme.header};
margin-top: 2.5rem;
}
`;
const SubHeader = styled.span`
background-color: ${props => props.theme.titleBackground};
color: ${props => props.theme.header};
display: block;
margin-bottom: 8px;
padding: 4px;
text-transform: uppercase;
`;
const Type = styled.span`
text-transform: capitalize;
`;
const StyledLink = styled(Link)`
border-bottom: 1px solid ${props => props.theme.CRIMSON};
color: ${props => props.theme.CRIMSON};
display: inline-block;
margin-top: 2rem;
text-decoration: none;
&:hover {
border-bottom: 1px solid transparent;
}
`;
class Pattern extends React.Component {
componentDidMount() {
this.props.reset();
}
render() {
const {
params: { id }
} = this.props.match;
const pattern = patterns.filter(item => item.id === id)[0];
const style = this.props.mode === 'dark' ? styleDark : styleLight;
return (
<StyledPattern>
<StyledLink to="/patterns">← Back to Patterns List</StyledLink>
{pattern && (
<React.Fragment>
<h2>{pattern.name}</h2>
<p>
<SubHeader>Type:</SubHeader>
<Type>{pattern.type} pattern</Type>
</p>
<p>
<SubHeader>Definition:</SubHeader>
{pattern.definition}
</p>
{pattern.when && (
<p>
<SubHeader>Use when…</SubHeader>
…{pattern.when}.
</p>
)}
<h3>ES6</h3>
<SyntaxHighlighter language="javascript" style={style}>
{pattern.codeES6}
</SyntaxHighlighter>
<h3>ES5</h3>
<SyntaxHighlighter language="javascript" style={style}>
{pattern.codeES5}
</SyntaxHighlighter>
</React.Fragment>
)}
</StyledPattern>
);
}
}
Pattern.propTypes = {
match: PropTypes.object.isRequired,
mode: PropTypes.string.isRequired,
reset: PropTypes.func.isRequired
};
export default connect(
state => ({
mode: getMode(state)
}),
{
reset: () => restart()
}
)(Pattern);