@@ -18,8 +18,7 @@ export const initialState = () => {
1818 _id : r ,
1919 children : [ b , a , c ] ,
2020 fileType : 'folder' ,
21- content : '' ,
22- isFolderClosed : false
21+ content : ''
2322 } ,
2423 {
2524 name : 'sketch.js' ,
@@ -159,24 +158,28 @@ const files = (state, action) => {
159158 }
160159 return Object . assign ( { } , file , { blobURL : action . blobURL } ) ;
161160 } ) ;
162- case ActionTypes . NEW_PROJECT :
163- action . files = action . files . map ( ( file ) => {
161+ case ActionTypes . NEW_PROJECT : {
162+ const newFiles = action . files . map ( ( file ) => {
164163 const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
165164 if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
166- file . isFolderClosed = corrospondingObj . isFolderClosed ;
165+ const isFolderClosed = corrospondingObj . isFolderClosed || false ;
166+ return { ...file , isFolderClosed } ;
167167 }
168168 return file ;
169169 } ) ;
170- return setFilePaths ( action . files ) ;
171- case ActionTypes . SET_PROJECT :
172- action . files = action . files . map ( ( file ) => {
170+ return setFilePaths ( newFiles ) ;
171+ }
172+ case ActionTypes . SET_PROJECT : {
173+ const newFiles = action . files . map ( ( file ) => {
173174 const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
174175 if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
175- file . isFolderClosed = corrospondingObj . isFolderClosed ;
176+ const isFolderClosed = corrospondingObj . isFolderClosed || false ;
177+ return { ...file , isFolderClosed } ;
176178 }
177179 return file ;
178180 } ) ;
179- return setFilePaths ( action . files ) ;
181+ return setFilePaths ( newFiles ) ;
182+ }
180183 case ActionTypes . RESET_PROJECT :
181184 return initialState ( ) ;
182185 case ActionTypes . CREATE_FILE : {
@@ -190,52 +193,19 @@ const files = (state, action) => {
190193 parentFile . name === 'root'
191194 ? ''
192195 : `${ parentFile . filePath } /${ parentFile . name } ` ;
193- // const newState = [
194- // ...updateParent(state, action),
195- // {
196- // name: action.name,
197- // id: action.id,
198- // _id: action._id,
199- // content: action.content,
200- // url: action.url,
201- // children: action.children,
202- // fileType: action.fileType || 'file',
203- // filePath,
204- // isExpanded: action.isExpanded
205- // }
206- // ];
207-
208- let newState = null ;
209- if ( action . fileType === 'folder' ) {
210- newState = [
211- ...updateParent ( state , action ) ,
212- {
213- name : action . name ,
214- id : action . id ,
215- _id : action . _id ,
216- content : action . content ,
217- url : action . url ,
218- children : action . children ,
219- fileType : 'folder' ,
220- filePath,
221- isFolderClosed : false
222- }
223- ] ;
224- } else {
225- newState = [
226- ...updateParent ( state , action ) ,
227- {
228- name : action . name ,
229- id : action . id ,
230- _id : action . _id ,
231- content : action . content ,
232- url : action . url ,
233- children : action . children ,
234- fileType : 'file' ,
235- filePath
236- }
237- ] ;
238- }
196+ const newState = [
197+ ...updateParent ( state , action ) ,
198+ {
199+ name : action . name ,
200+ id : action . id ,
201+ _id : action . _id ,
202+ content : action . content ,
203+ url : action . url ,
204+ children : action . children ,
205+ fileType : action . fileType || 'file' ,
206+ filePath
207+ }
208+ ] ;
239209
240210 return newState . map ( ( file ) => {
241211 if ( file . id === action . parentId ) {
@@ -285,18 +255,14 @@ const files = (state, action) => {
285255 case ActionTypes . SHOW_FOLDER_CHILDREN :
286256 return state . map ( ( file ) => {
287257 if ( file . id === action . id ) {
288- return Object . assign ( { } , file , {
289- isFolderClosed : false
290- } ) ;
258+ return Object . assign ( { } , file , { isFolderClosed : false } ) ;
291259 }
292260 return file ;
293261 } ) ;
294262 case ActionTypes . HIDE_FOLDER_CHILDREN :
295263 return state . map ( ( file ) => {
296264 if ( file . id === action . id ) {
297- return Object . assign ( { } , file , {
298- isFolderClosed : true
299- } ) ;
265+ return Object . assign ( { } , file , { isFolderClosed : true } ) ;
300266 }
301267 return file ;
302268 } ) ;
0 commit comments