1
- import React , { Component } from 'react' ;
1
+ import React from 'react' ;
2
+ import { connect } from 'react-redux' ;
2
3
import { Controlled as CodeMirror } from 'react-codemirror2' ;
3
4
4
5
import 'codemirror/lib/codemirror.css' ;
5
6
6
7
import Title from './common/Title' ;
7
8
import Input from './common/Input' ;
9
+ import Spinner from './common/Spinner' ;
10
+ import * as actions from '../actions' ;
8
11
9
12
import '../styles/Snippet.styl' ;
10
13
11
- class Snippet extends Component {
12
- constructor ( ) {
13
- super ( ) ;
14
+ class Snippet extends React . Component {
15
+ constructor ( props ) {
16
+ super ( props ) ;
14
17
this . state = { isShowEmbed : false } ;
15
18
this . toggleEmbed = this . toggleEmbed . bind ( this ) ;
16
19
}
17
20
21
+ componentDidMount ( ) {
22
+ const { dispatch } = this . props ;
23
+ const { id } = this . props . match . params ;
24
+
25
+ dispatch ( actions . fetchSnippet ( Number ( id ) ) ) ;
26
+ }
27
+
18
28
toggleEmbed ( ) {
19
- this . setState ( { isShowEmbed : ! this . state . isShowEmbed } ) ;
29
+ this . setState ( prevState => ( { isShowEmbed : ! prevState . isShowEmbed } ) ) ;
20
30
}
21
31
22
32
render ( ) {
33
+ const { snippet } = this . props ;
34
+
35
+ if ( ! snippet ) return < Spinner /> ;
36
+
37
+ const snippetTitle = snippet . get ( 'title' ) ? snippet . get ( 'title' ) : `#${ snippet . get ( 'id' ) } , Untitled` ;
38
+
23
39
return (
24
40
[
25
- < Title title = "Snippet" key = "snippet " /> ,
26
- < div className = "snippet" >
41
+ < Title title = "Snippet" key = "Snippet title " /> ,
42
+ < div className = "snippet" key = "Snippet" >
27
43
< div className = "snippet-header" >
28
44
< div className = "snippet-data" >
29
45
< div >
30
- < span className = "snippet-data-title" > #235435, Untitled </ span >
31
- < span className = "snippet-data-lang" > [ Java ]</ span >
46
+ < span className = "snippet-data-title" > { snippetTitle } </ span >
47
+ < span className = "snippet-data-lang" > [ { snippet . get ( 'syntax' , 'Text' ) } ]</ span >
32
48
</ div >
33
49
< span className = "snippet-data-author" > By Guest</ span >
34
50
</ div >
@@ -46,11 +62,11 @@ class Snippet extends Component {
46
62
In order to embed this content into your website or blog,
47
63
simply copy and paste code provided below:
48
64
</ p >
49
- < Input value = " <script src='http://xsnippet.org/363686 /embed/'></script>" />
65
+ < Input value = { ` <script src='http://xsnippet.org/${ snippet . get ( 'id' ) } /embed/'></script>` } />
50
66
</ div >
51
67
< div className = "snippet-code" >
52
68
< CodeMirror
53
- value = "console.log('All hail XSnippet')"
69
+ value = { ` ${ snippet . get ( 'content' ) } ` }
54
70
options = { { lineNumbers : true , readOnly : 'nocursor' } }
55
71
/>
56
72
< div className = "snippet-code-bottom-bar" >
@@ -64,4 +80,6 @@ class Snippet extends Component {
64
80
}
65
81
}
66
82
67
- export default Snippet ;
83
+ export default connect ( ( state , ownProps ) => ( {
84
+ snippet : state . snippets . get ( Number ( ownProps . match . params . id ) ) ,
85
+ } ) ) ( Snippet ) ;
0 commit comments