11var React = require ( 'react/addons' )
22var RouteHandler = require ( 'react-router' ) . RouteHandler
33var ReactRouter = require ( 'react-router' )
4- var Link = ReactRouter . Link
54var ModalBase = require ( '../Components/ModalBase' )
65var LaunchModal = require ( '../Components/LaunchModal' )
6+ var CodeViewer = require ( '../Components/CodeViewer' )
77
8- var currentUser = {
9- name : 'testcat' ,
10- email : 'testcat@example.com' ,
11- profileName : 'Test Cat'
12- }
13-
14- var userPlanets = [
15- {
16- id : 1 ,
17- name : 'myplanet' ,
18- profileName : 'TestCat'
19- } ,
20- {
21- id : 2 ,
22- name : 'group1' ,
23- profileName : 'Some Group#1'
24- } ,
25- {
26- id : 3 ,
27- name : 'group2' ,
28- profileName : 'Some Group#1'
29- }
30- ]
8+ var AuthStore = require ( '../Stores/AuthStore' )
9+ var PlanetStore = require ( '../Stores/PlanetStore' )
10+
11+ var fetchPlanet = require ( '../Actions/fetchPlanet' )
3112
3213var PlanetHeader = React . createClass ( {
3314 propTypes : {
@@ -76,24 +57,7 @@ var PlanetHeader = React.createClass({
7657 )
7758 }
7859} )
79-
80- var PlanetMain = React . createClass ( {
81- propTypes : {
82- currentPlanet : React . PropTypes . object ,
83- currentUser : React . PropTypes . object
84- } ,
85- render : function ( ) {
86- return (
87- < div className = 'PlanetMain' >
88- < PlanetHeader currentPlanet = { this . props . currentPlanet } currentUser = { this . props . currentUser } />
89- < SideNavigator currentPlanet = { this . props . currentPlanet } currentUser = { this . props . currentUser } />
90- < PlanetBody currentPlanet = { this . props . currentPlanet } />
91- </ div >
92- )
93- }
94- } )
95-
96- var SideNavigator = React . createClass ( {
60+ var PlanetNavigator = React . createClass ( {
9761 propTypes : {
9862 currentPlanet : React . PropTypes . shape ( {
9963 name : React . PropTypes . string
@@ -119,72 +83,168 @@ var SideNavigator = React.createClass({
11983 this . setState ( { isLaunchModalOpen : false } )
12084 } ,
12185 render : function ( ) {
122- var currentPlanetName = this . props . currentPlanet . name
123- var currentUserName = this . props . currentUser . name
124-
12586 return (
126- < div className = 'SideNavigator ' >
87+ < div className = 'PlanetNavigator ' >
12788 < button onClick = { this . openLaunchModal } className = 'btn-primary btn-block' >
12889 < i className = 'fa fa-rocket fa-fw' /> Launch
12990 </ button >
13091 < ModalBase isOpen = { this . state . isLaunchModalOpen } close = { this . closeLaunchModal } >
13192 < LaunchModal submit = { this . submitLaunchModal } close = { this . closeLaunchModal } />
13293 </ ModalBase >
13394 < nav >
134- < Link to = 'dashboard' params = { { userName : currentUserName , planetName : currentPlanetName } } >
95+ < a >
13596 < i className = 'fa fa-home fa-fw' /> Home
136- </ Link >
137- < Link to = 'snippets' params = { { userName : currentUserName , planetName : currentPlanetName } } >
97+ </ a >
98+ < a >
13899 < i className = 'fa fa-code fa-fw' /> Snippets
139- </ Link >
140- < Link to = 'blueprint' params = { { userName : currentUserName , planetName : currentPlanetName } } >
100+ </ a >
101+ < a >
141102 < i className = 'fa fa-file-text-o fa-fw' /> Blueprints
142- </ Link >
103+ </ a >
143104 </ nav >
144105 </ div >
145106 )
146107 }
147108} )
148109
149- var PlanetBody = React . createClass ( {
110+ var PlanetArticleList = React . createClass ( {
111+ mixins : [ ReactRouter . Navigation , ReactRouter . State ] ,
112+ propTypes : {
113+ planet : React . PropTypes . shape ( {
114+ Snippets : React . PropTypes . array ,
115+ Blueprints : React . PropTypes . array
116+ } )
117+ } ,
118+ render : function ( ) {
119+ var articles = this . props . planet . Snippets . map ( function ( snippet ) {
120+ var tags = snippet . Tags . map ( function ( tag ) {
121+ return (
122+ < a key = { tag . id } href > #{ tag . name } </ a >
123+ )
124+ } )
125+ var params = this . getParams ( )
126+
127+ var isActive = parseInt ( params . localId , 10 ) === snippet . localId
128+
129+ var handleClick = function ( ) {
130+ this . transitionTo ( 'snippets' , {
131+ userName : params . userName ,
132+ planetName : params . planetName ,
133+ localId : snippet . localId
134+ } )
135+ } . bind ( this )
136+
137+ return (
138+ < li onClick = { handleClick } className = { isActive ? 'active' : '' } key = { snippet . id } >
139+ < div className = 'callSign' > < i className = 'fa fa-code' > </ i > { snippet . callSign } </ div >
140+ < div className = 'description' > { snippet . description } </ div >
141+ < div className = 'updatedAt' > { snippet . updatedAt } </ div >
142+ < div className = 'tags' > < i className = 'fa fa-tags' /> { tags } </ div >
143+ </ li >
144+ )
145+ } . bind ( this ) )
146+
147+ return (
148+ < div className = 'PlanetArticleList' >
149+ < ul >
150+ { articles }
151+ </ ul >
152+ </ div >
153+ )
154+ }
155+ } )
156+
157+ var PlanetArticleDetail = React . createClass ( {
158+ propTypes : {
159+ snippet : React . PropTypes . object
160+ } ,
150161 render : function ( ) {
162+ var snippet = this . props . snippet
163+
164+ var tags = snippet . Tags . map ( function ( tag ) {
165+ return (
166+ < a key = { tag . id } href > #{ tag . name } </ a >
167+ )
168+ } )
169+
151170 return (
152- < div className = 'PlanetBody' >
153- < RouteHandler />
171+ < div className = 'PlanetArticleDetail' >
172+ < div className = 'viewer-header' >
173+ < i className = 'fa fa-code' > </ i > { snippet . callSign } < small className = 'updatedAt' > { snippet . updatedAt } </ small >
174+ < span className = 'control-group' >
175+ < button className = 'btn-default btn-square btn-sm' > < i className = 'fa fa-edit fa-fw' > </ i > </ button >
176+ < button className = 'btn-default btn-square btn-sm' > < i className = 'fa fa-trash fa-fw' > </ i > </ button >
177+ </ span >
178+ </ div >
179+ < div className = 'viewer-body' >
180+ < div className = 'viewer-detail' >
181+ < div className = 'description' > { snippet . description } </ div >
182+ < div className = 'tags' > < i className = 'fa fa-tags' /> { tags } </ div >
183+ </ div >
184+ < div className = 'content' >
185+ < CodeViewer code = { snippet . content } mode = { snippet . mode } />
186+ </ div >
187+ </ div >
154188 </ div >
155189 )
156190 }
157191} )
158192
159193module . exports = React . createClass ( {
160- mixins : [ ReactRouter . Navigation ] ,
194+ mixins : [ ReactRouter . Navigation , ReactRouter . State ] ,
161195 propTypes : {
162196 params : React . PropTypes . object ,
163197 planetName : React . PropTypes . string
164198 } ,
165- render : function ( ) {
166- var currentPlanetName = this . props . params . planetName
167- var currentPlanet = null
168- userPlanets . some ( function ( planet ) {
169- if ( planet . name === currentPlanetName ) {
170- currentPlanet = planet
171- return true
199+ getInitialState : function ( ) {
200+ return {
201+ currentPlanet : null
202+ }
203+ } ,
204+ componentDidMount : function ( ) {
205+ this . unsubscribe = PlanetStore . listen ( this . onFetched )
206+
207+ fetchPlanet ( this . props . params . userName + '/' + this . props . params . planetName )
208+ } ,
209+ componentWillUnmount : function ( ) {
210+ this . unsubscribe ( )
211+ } ,
212+ onFetched : function ( planet ) {
213+ this . setState ( { currentPlanet : planet } , function ( ) {
214+ if ( planet . Snippets . length > 0 ) {
215+ this . transitionTo ( 'snippets' , {
216+ userName : this . props . params . userName ,
217+ planetName : this . props . params . planetName ,
218+ localId : planet . Snippets [ 0 ] . localId } )
172219 }
173- return false
174220 } )
175- if ( currentPlanet == null ) {
176- var redirectTo = userPlanets [ 0 ] . name
177- this . transitionTo ( 'planet' , { planetName : redirectTo } )
178- return (
179- < div className = 'PlanetContainer' >
180- Redirecting...
181- </ div >
182- )
221+ } ,
222+ render : function ( ) {
223+ var user = AuthStore . getUser ( )
224+ if ( user == null ) return ( < div /> )
225+ if ( this . state . currentPlanet == null ) return ( < div /> )
226+
227+ var content = ( < div > No selected</ div > )
228+ if ( this . isActive ( 'snippets' ) ) {
229+ var localId = parseInt ( this . props . params . localId , 10 )
230+
231+ this . state . currentPlanet . Snippets . some ( function ( _snippet ) {
232+ if ( localId === _snippet . localId ) {
233+ content = (
234+ < PlanetArticleDetail snippet = { _snippet } />
235+ )
236+ return true
237+ }
238+ return false
239+ } )
183240 }
184241
185242 return (
186243 < div className = 'PlanetContainer' >
187- < PlanetMain currentPlanet = { currentPlanet } currentUser = { currentUser } />
244+ < PlanetHeader currentPlanet = { this . state . currentPlanet } currentUser = { user } />
245+ < PlanetNavigator currentPlanet = { this . state . currentPlanet } currentUser = { user } />
246+ < PlanetArticleList planet = { this . state . currentPlanet } />
247+ { content }
188248 </ div >
189249 )
190250 }
0 commit comments