@@ -18,7 +18,8 @@ export const initialState = () => {
1818 _id : r ,
1919 children : [ b , a , c ] ,
2020 fileType : 'folder' ,
21- content : ''
21+ content : '' ,
22+ isFolderClosed : false //
2223 } ,
2324 {
2425 name : 'sketch.js' ,
@@ -159,8 +160,22 @@ const files = (state, action) => {
159160 return Object . assign ( { } , file , { blobURL : action . blobURL } ) ;
160161 } ) ;
161162 case ActionTypes . NEW_PROJECT :
163+ action . files = action . files . map ( ( file ) => {
164+ const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
165+ if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
166+ file . isFolderClosed = corrospondingObj . isFolderClosed ;
167+ }
168+ return file ;
169+ } ) ;
162170 return setFilePaths ( action . files ) ;
163171 case ActionTypes . SET_PROJECT :
172+ action . files = action . files . map ( ( file ) => {
173+ const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
174+ if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
175+ file . isFolderClosed = corrospondingObj . isFolderClosed ;
176+ }
177+ return file ;
178+ } ) ;
164179 return setFilePaths ( action . files ) ;
165180 case ActionTypes . RESET_PROJECT :
166181 return initialState ( ) ;
@@ -175,19 +190,53 @@ const files = (state, action) => {
175190 parentFile . name === 'root'
176191 ? ''
177192 : `${ parentFile . filePath } /${ parentFile . name } ` ;
178- const newState = [
179- ...updateParent ( state , action ) ,
180- {
181- name : action . name ,
182- id : action . id ,
183- _id : action . _id ,
184- content : action . content ,
185- url : action . url ,
186- children : action . children ,
187- fileType : action . fileType || 'file' ,
188- filePath
189- }
190- ] ;
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+ }
239+
191240 return newState . map ( ( file ) => {
192241 if ( file . id === action . parentId ) {
193242 file . children = sortedChildrenId ( newState , file . children ) ;
@@ -236,14 +285,18 @@ const files = (state, action) => {
236285 case ActionTypes . SHOW_FOLDER_CHILDREN :
237286 return state . map ( ( file ) => {
238287 if ( file . id === action . id ) {
239- return Object . assign ( { } , file , { isFolderClosed : false } ) ;
288+ return Object . assign ( { } , file , {
289+ isFolderClosed : false
290+ } ) ;
240291 }
241292 return file ;
242293 } ) ;
243294 case ActionTypes . HIDE_FOLDER_CHILDREN :
244295 return state . map ( ( file ) => {
245296 if ( file . id === action . id ) {
246- return Object . assign ( { } , file , { isFolderClosed : true } ) ;
297+ return Object . assign ( { } , file , {
298+ isFolderClosed : true
299+ } ) ;
247300 }
248301 return file ;
249302 } ) ;
0 commit comments