1- import React , { Component } from 'react' ;
1+ import React from 'react' ;
2+ import { connect } from 'react-redux' ;
23import { Controlled as CodeMirror } from 'react-codemirror2' ;
34
45import 'codemirror/lib/codemirror.css' ;
56
67import Title from './common/Title' ;
78import Input from './common/Input' ;
9+ import Spinner from './common/Spinner' ;
10+ import * as actions from '../actions' ;
811
912import '../styles/Snippet.styl' ;
1013
11- class Snippet extends Component {
12- constructor ( ) {
13- super ( ) ;
14+ class Snippet extends React . Component {
15+ constructor ( props ) {
16+ super ( props ) ;
1417 this . state = { isShowEmbed : false } ;
1518 this . toggleEmbed = this . toggleEmbed . bind ( this ) ;
1619 }
1720
21+ componentDidMount ( ) {
22+ const { dispatch } = this . props ;
23+ const { id } = this . props . match . params ;
24+
25+ dispatch ( actions . fetchSnippet ( Number ( id ) ) ) ;
26+ }
27+
1828 toggleEmbed ( ) {
19- this . setState ( { isShowEmbed : ! this . state . isShowEmbed } ) ;
29+ this . setState ( prevState => ( { isShowEmbed : ! prevState . isShowEmbed } ) ) ;
2030 }
2131
2232 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+
2339 return (
2440 [
25- < Title title = "Snippet" key = "snippet " /> ,
26- < div className = "snippet" >
41+ < Title title = "Snippet" key = "Snippet title " /> ,
42+ < div className = "snippet" key = "Snippet" >
2743 < div className = "snippet-header" >
2844 < div className = "snippet-data" >
2945 < 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 >
3248 </ div >
3349 < span className = "snippet-data-author" > By Guest</ span >
3450 </ div >
@@ -46,11 +62,11 @@ class Snippet extends Component {
4662 In order to embed this content into your website or blog,
4763 simply copy and paste code provided below:
4864 </ 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>` } />
5066 </ div >
5167 < div className = "snippet-code" >
5268 < CodeMirror
53- value = "console.log('All hail XSnippet')"
69+ value = { ` ${ snippet . get ( 'content' ) } ` }
5470 options = { { lineNumbers : true , readOnly : 'nocursor' } }
5571 />
5672 < div className = "snippet-code-bottom-bar" >
@@ -64,4 +80,6 @@ class Snippet extends Component {
6480 }
6581}
6682
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