Skip to content

Commit ce62201

Browse files
committed
Refactor project token handling in save-project-to-server and clean up URL parameters after project creation
1 parent 31cc321 commit ce62201

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎src/lib/save-project-to-server.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,27 @@ export default function (projectId, vmState, params) {
2929
if (Object.prototype.hasOwnProperty.call(params, 'isCopy')) queryParams.is_copy = params.isCopy;
3030
if (Object.prototype.hasOwnProperty.call(params, 'isRemix')) queryParams.is_remix = params.isRemix;
3131
if (Object.prototype.hasOwnProperty.call(params, 'title')) queryParams.title = params.title;
32+
33+
if (vm.runtime.storage.projectToken) {
34+
queryParams.token = vm.runtime.storage.projectToken;
35+
}else if (creatingProject) {//vm.runtime.storage.projectToken is not set when creating a new project
36+
const searchParams = new URLSearchParams(location.search);
37+
if (searchParams.has('token')) queryParams.token = searchParams.get('token');
38+
if(searchParams.has('username')) queryParams.username = searchParams.get('username');
39+
}
40+
41+
3242
let qs = queryString.stringify(queryParams);
3343
if (qs) qs = `?${qs}`;
34-
const projectToken = vm.runtime.storage.projectToken ? "?token=" + vm.runtime.storage.projectToken : "";
3544
if (creatingProject) { //POST request to create new project
3645
Object.assign(opts, {
3746
method: 'post',
38-
url: `${storage.projectHost}/${qs}${projectToken}`
47+
url: `${storage.projectHost}/${qs}`
3948
});
4049
} else { //PUT request to update existing project
4150
Object.assign(opts, {
4251
method: 'put',
43-
url: `${storage.projectHost}/${projectId}${qs}${projectToken}`
52+
url: `${storage.projectHost}/${projectId}${qs}`
4453
});
4554
}
4655
return new Promise((resolve, reject) => {

‎src/reducers/project-state.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ const createProject = () => ({
374374
});
375375

376376
const doneCreatingProject = (id, loadingState) => {
377+
const searchParams = new URLSearchParams(location.search);
378+
if (searchParams.has('new_project')) searchParams.delete('new_project');
379+
if(searchParams.has('username')) searchParams.delete('username');
380+
if(searchParams.has('token')) searchParams.delete('token');
381+
const newUrl = `${location.pathname}${searchParams.toString() ? '?' + searchParams.toString() : ''}${location.hash}`;
382+
window.history.replaceState({}, document.title, newUrl);
383+
377384
switch (loadingState) {
378385
case LoadingState.CREATING_NEW:
379386
return {

0 commit comments

Comments
 (0)