Skip to content

Commit a90c181

Browse files
LakshSinglacatarak
authored andcommitted
Duplicate functionality extended for non opened sketches too
1 parent e04d448 commit a90c181

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

client/modules/IDE/actions/project.js

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -246,49 +246,65 @@ function generateNewIdsForChildren(file, files) {
246246
file.children = newChildren; // eslint-disable-line
247247
}
248248

249-
export function cloneProject() {
249+
export function cloneProject(id) {
250250
return (dispatch, getState) => {
251251
dispatch(setUnsavedChanges(false));
252-
const state = getState();
253-
const newFiles = state.files.map((file) => { // eslint-disable-line
254-
return { ...file };
255-
});
252+
new Promise((resolve, reject) => {
253+
if (!id) {
254+
resolve(getState());
255+
} else {
256+
console.log(id);
257+
fetch(`${ROOT_URL}/projects/${id}`)
258+
.then(res => res.json())
259+
.then(data => resolve({
260+
files: data.files,
261+
project: {
262+
name: data.name
263+
}
264+
}));
265+
}
266+
}).then((state) => {
267+
console.log('Huuray');
268+
console.log(state);
269+
const newFiles = state.files.map((file) => { // eslint-disable-line
270+
return { ...file };
271+
});
256272

257-
// generate new IDS for all files
258-
const rootFile = newFiles.find(file => file.name === 'root');
259-
const newRootFileId = objectID().toHexString();
260-
rootFile.id = newRootFileId;
261-
rootFile._id = newRootFileId;
262-
generateNewIdsForChildren(rootFile, newFiles);
273+
// generate new IDS for all files
274+
const rootFile = newFiles.find(file => file.name === 'root');
275+
const newRootFileId = objectID().toHexString();
276+
rootFile.id = newRootFileId;
277+
rootFile._id = newRootFileId;
278+
generateNewIdsForChildren(rootFile, newFiles);
263279

264-
// duplicate all files hosted on S3
265-
each(newFiles, (file, callback) => {
266-
if (file.url && file.url.includes('amazonaws')) {
267-
const formParams = {
268-
url: file.url
269-
};
270-
axios.post(`${ROOT_URL}/S3/copy`, formParams, { withCredentials: true })
280+
// duplicate all files hosted on S3
281+
each(newFiles, (file, callback) => {
282+
if (file.url && file.url.includes('amazonaws')) {
283+
const formParams = {
284+
url: file.url
285+
};
286+
axios.post(`${ROOT_URL}/S3/copy`, formParams, { withCredentials: true })
287+
.then((response) => {
288+
file.url = response.data.url;
289+
callback(null);
290+
});
291+
} else {
292+
callback(null);
293+
}
294+
}, (err) => {
295+
// if not errors in duplicating the files on S3, then duplicate it
296+
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
297+
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
271298
.then((response) => {
272-
file.url = response.data.url;
273-
callback(null);
274-
});
275-
} else {
276-
callback(null);
277-
}
278-
}, (err) => {
279-
// if not errors in duplicating the files on S3, then duplicate it
280-
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
281-
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
282-
.then((response) => {
283-
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
284-
dispatch(setNewProject(response.data));
285-
})
286-
.catch(response => dispatch({
287-
type: ActionTypes.PROJECT_SAVE_FAIL,
288-
error: response.data
289-
}));
290-
});
291-
};
299+
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
300+
dispatch(setNewProject(response.data));
301+
})
302+
.catch(response => dispatch({
303+
type: ActionTypes.PROJECT_SAVE_FAIL,
304+
error: response.data
305+
}));
306+
});
307+
});
292308
}
293309

294310
export function showEditProjectName() {

0 commit comments

Comments
 (0)