@@ -18,8 +18,7 @@ export const initialState = () => {
18
18
_id : r ,
19
19
children : [ b , a , c ] ,
20
20
fileType : 'folder' ,
21
- content : '' ,
22
- isFolderClosed : false
21
+ content : ''
23
22
} ,
24
23
{
25
24
name : 'sketch.js' ,
@@ -159,24 +158,28 @@ const files = (state, action) => {
159
158
}
160
159
return Object . assign ( { } , file , { blobURL : action . blobURL } ) ;
161
160
} ) ;
162
- case ActionTypes . NEW_PROJECT :
163
- action . files = action . files . map ( ( file ) => {
161
+ case ActionTypes . NEW_PROJECT : {
162
+ const newFiles = action . files . map ( ( file ) => {
164
163
const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
165
164
if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
166
- file . isFolderClosed = corrospondingObj . isFolderClosed ;
165
+ const isFolderClosed = corrospondingObj . isFolderClosed || false ;
166
+ return { ...file , isFolderClosed } ;
167
167
}
168
168
return file ;
169
169
} ) ;
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 ) => {
173
174
const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
174
175
if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
175
- file . isFolderClosed = corrospondingObj . isFolderClosed ;
176
+ const isFolderClosed = corrospondingObj . isFolderClosed || false ;
177
+ return { ...file , isFolderClosed } ;
176
178
}
177
179
return file ;
178
180
} ) ;
179
- return setFilePaths ( action . files ) ;
181
+ return setFilePaths ( newFiles ) ;
182
+ }
180
183
case ActionTypes . RESET_PROJECT :
181
184
return initialState ( ) ;
182
185
case ActionTypes . CREATE_FILE : {
@@ -190,52 +193,19 @@ const files = (state, action) => {
190
193
parentFile . name === 'root'
191
194
? ''
192
195
: `${ 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
+ ] ;
239
209
240
210
return newState . map ( ( file ) => {
241
211
if ( file . id === action . parentId ) {
@@ -285,18 +255,14 @@ const files = (state, action) => {
285
255
case ActionTypes . SHOW_FOLDER_CHILDREN :
286
256
return state . map ( ( file ) => {
287
257
if ( file . id === action . id ) {
288
- return Object . assign ( { } , file , {
289
- isFolderClosed : false
290
- } ) ;
258
+ return Object . assign ( { } , file , { isFolderClosed : false } ) ;
291
259
}
292
260
return file ;
293
261
} ) ;
294
262
case ActionTypes . HIDE_FOLDER_CHILDREN :
295
263
return state . map ( ( file ) => {
296
264
if ( file . id === action . id ) {
297
- return Object . assign ( { } , file , {
298
- isFolderClosed : true
299
- } ) ;
265
+ return Object . assign ( { } , file , { isFolderClosed : true } ) ;
300
266
}
301
267
return file ;
302
268
} ) ;
0 commit comments