Skip to content

[#1677] Remove API call when duplicating sketch from SketchList #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/modules/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class App extends React.Component {
render() {
return (
<div className="app">
{false && this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
{this.props.children}
</div>
);
Expand Down
90 changes: 38 additions & 52 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,64 +258,50 @@ function generateNewIdsForChildren(file, files) {
file.children = newChildren; // eslint-disable-line
}

export function cloneProject(id) {
export function cloneProject(project) {
return (dispatch, getState) => {
dispatch(setUnsavedChanges(false));
new Promise((resolve, reject) => {
if (!id) {
resolve(getState());
} else {
apiClient.get(`/projects/${id}`)
.then(res => res.json())
.then(data => resolve({
files: data.files,
project: {
name: data.name
}
}));
}
}).then((state) => {
const newFiles = state.files.map((file) => { // eslint-disable-line
return { ...file };
});
const state = getState();
const files = project ? project.files : state.files;
const projectName = project ? project.name : state.project.name;
const newFiles = files.map(file => ({ ...file }));

// generate new IDS for all files
const rootFile = newFiles.find(file => file.name === 'root');
const newRootFileId = objectID().toHexString();
rootFile.id = newRootFileId;
rootFile._id = newRootFileId;
generateNewIdsForChildren(rootFile, newFiles);
// generate new IDS for all files
const rootFile = newFiles.find(file => file.name === 'root');
const newRootFileId = objectID().toHexString();
rootFile.id = newRootFileId;
rootFile._id = newRootFileId;
generateNewIdsForChildren(rootFile, newFiles);

// duplicate all files hosted on S3
each(newFiles, (file, callback) => {
if (file.url && (file.url.includes(S3_BUCKET_URL_BASE) || file.url.includes(S3_BUCKET))) {
const formParams = {
url: file.url
};
apiClient.post('/S3/copy', formParams)
.then((response) => {
file.url = response.data.url;
callback(null);
});
} else {
callback(null);
}
}, (err) => {
// if not errors in duplicating the files on S3, then duplicate it
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
apiClient.post('/projects', formParams)
// duplicate all files hosted on S3
each(newFiles, (file, callback) => {
if (file.url && (file.url.includes(S3_BUCKET_URL_BASE) || file.url.includes(S3_BUCKET))) {
const formParams = {
url: file.url
};
apiClient.post('/S3/copy', formParams)
.then((response) => {
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setNewProject(response.data));
})
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data
});
file.url = response.data.url;
callback(null);
});
});
} else {
callback(null);
}
}, (err) => {
// if not errors in duplicating the files on S3, then duplicate it
const formParams = Object.assign({}, { name: `${projectName} copy` }, { files: newFiles });
apiClient.post('/projects', formParams)
.then((response) => {
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setNewProject(response.data));
})
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data
});
});
});
};
}
Expand Down
2 changes: 1 addition & 1 deletion client/modules/IDE/components/SketchList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class SketchListRowBase extends React.Component {

handleSketchDuplicate = () => {
this.closeAll();
this.props.cloneProject(this.props.sketch.id);
this.props.cloneProject(this.props.sketch);
}

handleSketchShare = () => {
Expand Down