@@ -258,64 +258,50 @@ function generateNewIdsForChildren(file, files) {
258
258
file . children = newChildren ; // eslint-disable-line
259
259
}
260
260
261
- export function cloneProject ( id ) {
261
+ export function cloneProject ( project ) {
262
262
return ( dispatch , getState ) => {
263
263
dispatch ( setUnsavedChanges ( false ) ) ;
264
- new Promise ( ( resolve , reject ) => {
265
- if ( ! id ) {
266
- resolve ( getState ( ) ) ;
267
- } else {
268
- apiClient . get ( `/projects/${ id } ` )
269
- . then ( res => res . json ( ) )
270
- . then ( data => resolve ( {
271
- files : data . files ,
272
- project : {
273
- name : data . name
274
- }
275
- } ) ) ;
276
- }
277
- } ) . then ( ( state ) => {
278
- const newFiles = state . files . map ( ( file ) => { // eslint-disable-line
279
- return { ...file } ;
280
- } ) ;
264
+ const state = getState ( ) ;
265
+ const files = project ? project . files : state . files ;
266
+ const projectName = project ? project . name : state . project . name ;
267
+ const newFiles = files . map ( file => ( { ...file } ) ) ;
281
268
282
- // generate new IDS for all files
283
- const rootFile = newFiles . find ( file => file . name === 'root' ) ;
284
- const newRootFileId = objectID ( ) . toHexString ( ) ;
285
- rootFile . id = newRootFileId ;
286
- rootFile . _id = newRootFileId ;
287
- generateNewIdsForChildren ( rootFile , newFiles ) ;
269
+ // generate new IDS for all files
270
+ const rootFile = newFiles . find ( file => file . name === 'root' ) ;
271
+ const newRootFileId = objectID ( ) . toHexString ( ) ;
272
+ rootFile . id = newRootFileId ;
273
+ rootFile . _id = newRootFileId ;
274
+ generateNewIdsForChildren ( rootFile , newFiles ) ;
288
275
289
- // duplicate all files hosted on S3
290
- each ( newFiles , ( file , callback ) => {
291
- if ( file . url && ( file . url . includes ( S3_BUCKET_URL_BASE ) || file . url . includes ( S3_BUCKET ) ) ) {
292
- const formParams = {
293
- url : file . url
294
- } ;
295
- apiClient . post ( '/S3/copy' , formParams )
296
- . then ( ( response ) => {
297
- file . url = response . data . url ;
298
- callback ( null ) ;
299
- } ) ;
300
- } else {
301
- callback ( null ) ;
302
- }
303
- } , ( err ) => {
304
- // if not errors in duplicating the files on S3, then duplicate it
305
- const formParams = Object . assign ( { } , { name : `${ state . project . name } copy` } , { files : newFiles } ) ;
306
- apiClient . post ( '/projects' , formParams )
276
+ // duplicate all files hosted on S3
277
+ each ( newFiles , ( file , callback ) => {
278
+ if ( file . url && ( file . url . includes ( S3_BUCKET_URL_BASE ) || file . url . includes ( S3_BUCKET ) ) ) {
279
+ const formParams = {
280
+ url : file . url
281
+ } ;
282
+ apiClient . post ( '/S3/copy' , formParams )
307
283
. then ( ( response ) => {
308
- browserHistory . push ( `/${ response . data . user . username } /sketches/${ response . data . id } ` ) ;
309
- dispatch ( setNewProject ( response . data ) ) ;
310
- } )
311
- . catch ( ( error ) => {
312
- const { response } = error ;
313
- dispatch ( {
314
- type : ActionTypes . PROJECT_SAVE_FAIL ,
315
- error : response . data
316
- } ) ;
284
+ file . url = response . data . url ;
285
+ callback ( null ) ;
317
286
} ) ;
318
- } ) ;
287
+ } else {
288
+ callback ( null ) ;
289
+ }
290
+ } , ( err ) => {
291
+ // if not errors in duplicating the files on S3, then duplicate it
292
+ const formParams = Object . assign ( { } , { name : `${ projectName } copy` } , { files : newFiles } ) ;
293
+ apiClient . post ( '/projects' , formParams )
294
+ . then ( ( response ) => {
295
+ browserHistory . push ( `/${ response . data . user . username } /sketches/${ response . data . id } ` ) ;
296
+ dispatch ( setNewProject ( response . data ) ) ;
297
+ } )
298
+ . catch ( ( error ) => {
299
+ const { response } = error ;
300
+ dispatch ( {
301
+ type : ActionTypes . PROJECT_SAVE_FAIL ,
302
+ error : response . data
303
+ } ) ;
304
+ } ) ;
319
305
} ) ;
320
306
} ;
321
307
}
0 commit comments