Skip to content

Commit 79ae566

Browse files
authored
Merge pull request #1686 from processing/bug/duplicate-sketch
[#1677] Remove API call when duplicating sketch from SketchList
2 parents a9ddb48 + a255f10 commit 79ae566

File tree

3 files changed

+40
-54
lines changed

3 files changed

+40
-54
lines changed

client/modules/App/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class App extends React.Component {
4141
render() {
4242
return (
4343
<div className="app">
44-
{false && this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
44+
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
4545
{this.props.children}
4646
</div>
4747
);

client/modules/IDE/actions/project.js

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -258,64 +258,50 @@ function generateNewIdsForChildren(file, files) {
258258
file.children = newChildren; // eslint-disable-line
259259
}
260260

261-
export function cloneProject(id) {
261+
export function cloneProject(project) {
262262
return (dispatch, getState) => {
263263
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 }));
281268

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);
288275

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)
307283
.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);
317286
});
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+
});
319305
});
320306
};
321307
}

client/modules/IDE/components/SketchList.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class SketchListRowBase extends React.Component {
139139

140140
handleSketchDuplicate = () => {
141141
this.closeAll();
142-
this.props.cloneProject(this.props.sketch.id);
142+
this.props.cloneProject(this.props.sketch);
143143
}
144144

145145
handleSketchShare = () => {

0 commit comments

Comments
 (0)