From f344e01003ee0069dba2ab49ccff3f69cc023bb2 Mon Sep 17 00:00:00 2001 From: Cassie Tarakajian Date: Fri, 17 Feb 2017 14:53:48 -0500 Subject: [PATCH] fix #289 --- client/modules/IDE/actions/project.js | 29 +++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index e6d75e2a..e1dead8b 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -1,7 +1,7 @@ import * as ActionTypes from '../../../constants'; import { browserHistory } from 'react-router'; import axios from 'axios'; -// import objectID from 'bson-objectid'; +import objectID from 'bson-objectid'; import { showToast, setToastText } from './toast'; import { setUnsavedChanges, justOpenedProject, @@ -166,17 +166,30 @@ export function newProject() { return resetProject(); } +function generateNewIdsForChildren(file, files) { + const newChildren = []; + file.children.forEach((childId) => { + const child = files.find(childFile => childFile.id === childId); + const newId = objectID().toHexString(); + child.id = newId; + child._id = newId; + newChildren.push(newId); + generateNewIdsForChildren(child, files); + }); + file.children = newChildren; // eslint-disable-line +} + export function cloneProject() { return (dispatch, getState) => { dispatch(setUnsavedChanges(false)); const state = getState(); - // const newFiles = state.files.map(file => { - // const newFile = Object.assign({}, file); - // newFile._id = objectID().toHexString(); - // newFile.id = newFile._id; - // return newFile; - // }); - const newFiles = state.files; + const newFiles = [...state.files]; + const rootFile = newFiles.find(file => file.name === 'root'); + const newRootFileId = objectID().toHexString(); + rootFile.id = newRootFileId; + rootFile._id = newRootFileId; + generateNewIdsForChildren(rootFile, newFiles); + // const newFiles = state.files; const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles }); axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true }) .then(response => {