@@ -17,9 +17,9 @@ import {
1717 ToastContainer ,
1818 VisualizationViewer ,
1919} from '/components' ;
20- import { CategoryApi , GitHubApi } from '/apis' ;
20+ import { AlgorithmApi , GitHubApi } from '/apis' ;
2121import { actions } from '/reducers' ;
22- import { extension , handleError , refineGist } from '/common/util' ;
22+ import { extension , getFiles , getTitleArray , handleError , refineGist } from '/common/util' ;
2323import { exts , languages , us } from '/common/config' ;
2424import { README_MD , SCRATCH_PAPER_MD } from '/skeletons' ;
2525import styles from './stylesheet.scss' ;
@@ -49,7 +49,7 @@ class App extends React.Component {
4949 const accessToken = Cookies . get ( 'access_token' ) ;
5050 if ( accessToken ) this . signIn ( accessToken ) ;
5151
52- CategoryApi . getCategories ( )
52+ AlgorithmApi . getCategories ( )
5353 . then ( ( { categories } ) => this . props . setCategories ( categories ) )
5454 . catch ( handleError . bind ( this ) ) ;
5555
@@ -65,7 +65,12 @@ class App extends React.Component {
6565
6666 componentWillReceiveProps ( nextProps ) {
6767 const { params } = nextProps . match ;
68- const { categoryKey, algorithmKey, gistId } = nextProps . current ;
68+ const { algorithm, scratchPaper } = nextProps . current ;
69+
70+ const categoryKey = algorithm && algorithm . categoryKey ;
71+ const algorithmKey = algorithm && algorithm . algorithmKey ;
72+ const gistId = scratchPaper && scratchPaper . gistId ;
73+
6974 if ( params . categoryKey !== categoryKey ||
7075 params . algorithmKey !== algorithmKey ||
7176 params . gistId !== gistId ) {
@@ -135,13 +140,14 @@ class App extends React.Component {
135140 const { ext } = this . props . env ;
136141 let fetchPromise = null ;
137142 if ( categoryKey && algorithmKey ) {
138- fetchPromise = CategoryApi . getAlgorithm ( categoryKey , algorithmKey )
139- . then ( ( { algorithm } ) => algorithm ) ;
143+ fetchPromise = AlgorithmApi . getAlgorithm ( categoryKey , algorithmKey )
144+ . then ( ( { algorithm } ) => this . props . setAlgorithm ( algorithm ) ) ;
140145 } else if ( [ 'new' , 'forked' ] . includes ( gistId ) ) {
141146 gistId = 'new' ;
142147 const language = languages . find ( language => language . ext === ext ) ;
143- fetchPromise = Promise . resolve ( {
144- titles : [ 'Scratch Paper' , 'Untitled' ] ,
148+ fetchPromise = Promise . resolve ( this . props . setScratchPaper ( {
149+ gistId,
150+ title : 'Untitled' ,
145151 files : [ {
146152 name : 'README.md' ,
147153 content : SCRATCH_PAPER_MD ,
@@ -151,24 +157,29 @@ class App extends React.Component {
151157 content : language . skeleton ,
152158 contributors : undefined ,
153159 } ] ,
154- } ) ;
160+ } ) ) ;
155161 } else if ( gistId ) {
156- fetchPromise = GitHubApi . getGist ( gistId , { timestamp : Date . now ( ) } ) . then ( refineGist ) ;
162+ fetchPromise = GitHubApi . getGist ( gistId , { timestamp : Date . now ( ) } )
163+ . then ( refineGist )
164+ . then ( this . props . setScratchPaper ) ;
157165 } else {
158166 fetchPromise = Promise . reject ( new Error ( ) ) ;
159167 }
160168 fetchPromise
161- . then ( algorithm => this . props . setCurrent ( categoryKey , algorithmKey , gistId , algorithm . titles , algorithm . files , algorithm . gist ) )
162169 . catch ( error => {
163170 if ( error . message ) handleError . bind ( this ) ( error ) ;
164- this . props . setCurrent ( undefined , undefined , undefined , [ 'Algorithm Visualizer' ] , [ {
165- name : 'README.md' ,
166- content : README_MD ,
167- contributors : [ us ] ,
168- } ] , undefined ) ;
171+ this . props . setAlgorithm ( {
172+ categoryName : 'Algorithm Visualizer' ,
173+ algorithmName : 'Home' ,
174+ files : [ {
175+ name : 'README.md' ,
176+ content : README_MD ,
177+ contributors : [ us ] ,
178+ } ] ,
179+ } ) ;
169180 } )
170181 . finally ( ( ) => {
171- const { files } = this . props . current ;
182+ const files = getFiles ( this . props . current ) ;
172183 let editorTabIndex = files . findIndex ( file => extension ( file . name ) === ext ) ;
173184 if ( ! ~ editorTabIndex ) editorTabIndex = files . findIndex ( file => exts . includes ( extension ( file . name ) ) ) ;
174185 if ( ! ~ editorTabIndex ) editorTabIndex = Math . min ( 0 , files . length - 1 ) ;
@@ -182,15 +193,15 @@ class App extends React.Component {
182193 }
183194
184195 handleChangeEditorTabIndex ( editorTabIndex ) {
185- const { files } = this . props . current ;
196+ const files = getFiles ( this . props . current ) ;
186197 if ( editorTabIndex === files . length ) this . handleAddFile ( ) ;
187198 this . setState ( { editorTabIndex } ) ;
188199 this . props . shouldBuild ( ) ;
189200 }
190201
191202 handleAddFile ( ) {
192203 const { ext } = this . props . env ;
193- const { files } = this . props . current ;
204+ const files = getFiles ( this . props . current ) ;
194205 let name = `code.${ ext } ` ;
195206 let count = 0 ;
196207 while ( files . some ( file => file . name === name ) ) name = `code-${ ++ count } .${ ext } ` ;
@@ -210,7 +221,7 @@ class App extends React.Component {
210221
211222 handleDeleteFile ( ) {
212223 const { editorTabIndex } = this . state ;
213- const { files } = this . props . current ;
224+ const files = getFiles ( this . props . current ) ;
214225 this . handleChangeEditorTabIndex ( Math . min ( editorTabIndex , files . length - 2 ) ) ;
215226 this . props . deleteFile ( editorTabIndex ) ;
216227 }
@@ -220,15 +231,15 @@ class App extends React.Component {
220231 }
221232
222233 isGistSaved ( ) {
223- const { titles, files, lastTitles, lastFiles } = this . props . current ;
224- const serializeTitles = titles => JSON . stringify ( titles ) ;
234+ const { scratchPaper } = this . props . current ;
235+ if ( ! scratchPaper ) return true ;
236+ const { title, files, lastTitle, lastFiles } = scratchPaper ;
225237 const serializeFiles = files => JSON . stringify ( files . map ( ( { name, content } ) => ( { name, content } ) ) ) ;
226- return serializeTitles ( titles ) === serializeTitles ( lastTitles ) &&
227- serializeFiles ( files ) === serializeFiles ( lastFiles ) ;
238+ return title === lastTitle && serializeFiles ( files ) === serializeFiles ( lastFiles ) ;
228239 }
229240
230241 getDescription ( ) {
231- const { files } = this . props . current ;
242+ const files = getFiles ( this . props . current ) ;
232243 const readmeFile = files . find ( file => file . name === 'README.md' ) ;
233244 if ( ! readmeFile ) return '' ;
234245 const groups = / ^ \s * # .* \n + ( [ ^ \n ] + ) / . exec ( readmeFile . content ) ;
@@ -237,7 +248,8 @@ class App extends React.Component {
237248
238249 render ( ) {
239250 const { navigatorOpened, workspaceWeights, editorTabIndex } = this . state ;
240- const { titles, files } = this . props . current ;
251+ const files = getFiles ( this . props . current ) ;
252+ const titleArray = getTitleArray ( this . props . current ) ;
241253
242254 const gistSaved = this . isGistSaved ( ) ;
243255 const description = this . getDescription ( ) ;
@@ -257,7 +269,7 @@ class App extends React.Component {
257269 return (
258270 < div className = { styles . app } >
259271 < Helmet >
260- < title > { gistSaved ? '' : '(Unsaved) ' } { titles . join ( ' - ' ) } </ title >
272+ < title > { gistSaved ? '' : '(Unsaved) ' } { titleArray . join ( ' - ' ) } </ title >
261273 < meta name = "description" content = { description } />
262274 </ Helmet >
263275 < Header className = { styles . header } onClickTitleBar = { ( ) => this . toggleNavigatorOpened ( ) }
0 commit comments