From e10bcb1338e8d053e296c8cc15953cf3700bdafd Mon Sep 17 00:00:00 2001
From: Ben Wheeler <wheeler.benjamin@gmail.com>
Date: Wed, 24 Oct 2018 13:37:00 -0400
Subject: [PATCH] support for creating copy and remixing states, sending these
 requests to storage

---
 src/lib/project-saver-hoc.jsx | 44 ++++++++++++++++++++++++++++++-----
 src/reducers/project-state.js | 14 +++++++----
 2 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/lib/project-saver-hoc.jsx b/src/lib/project-saver-hoc.jsx
index 7039b1a41..3c583d211 100644
--- a/src/lib/project-saver-hoc.jsx
+++ b/src/lib/project-saver-hoc.jsx
@@ -9,7 +9,9 @@ import {
     createProject,
     doneCreatingProject,
     doneUpdatingProject,
-    getIsCreating,
+    getIsCreatingCopy,
+    getIsCreatingNew,
+    getIsRemixing,
     getIsShowingProject,
     getIsShowingWithoutId,
     getIsUpdating,
@@ -41,16 +43,41 @@ const ProjectSaverHOC = function (WrappedComponent) {
                     });
             }
             // TODO: distinguish between creating new, remixing, and saving as a copy
-            if (this.props.isCreating && !prevProps.isCreating) {
+            if (this.props.isCreatingNew && !prevProps.isCreatingNew) {
                 this.storeProject()
                     .then(response => {
                         this.props.onCreatedProject(response.id.toString(), this.props.loadingState);
                     })
                     .catch(err => {
-                        // NOTE: should throw up a notice for user
                         this.props.onProjectError(`Creating a new project failed with error: ${err}`);
                     });
             }
+            if (this.props.isCreatingCopy && !prevProps.isCreatingCopy) {
+                this.storeProject(undefined, {
+                    original_id: this.props.reduxProjectId,
+                    is_copy: 1,
+                    title: encodeURIComponent(this.props.reduxProjectTitle)
+                })
+                    .then(response => {
+                        this.props.onCreatedProject(response.id.toString(), this.props.loadingState);
+                    })
+                    .catch(err => {
+                        this.props.onProjectError(`Creating a project copy failed with error: ${err}`);
+                    });
+            }
+            if (this.props.isRemixing && !prevProps.isRemixing) {
+                this.storeProject(undefined, {
+                    original_id: this.props.reduxProjectId,
+                    is_remix: 1,
+                    title: encodeURIComponent(this.props.reduxProjectTitle)
+                })
+                    .then(response => {
+                        this.props.onCreatedProject(response.id.toString(), this.props.loadingState);
+                    })
+                    .catch(err => {
+                        this.props.onProjectError(`Remixing a project failed with error: ${err}`);
+                    });
+            }
 
             // check if the project state, and user capabilities, have changed so as to indicate
             // that we should create or update project
@@ -74,7 +101,7 @@ const ProjectSaverHOC = function (WrappedComponent) {
          * @param  {number|string|undefined} projectId defined value causes PUT/update; undefined causes POST/create
          * @return {Promise} resolves with json object containing project's existing or new id
          */
-        storeProject (projectId) {
+        storeProject (projectId, urlOptions) {
             return this.props.vm.saveProjectSb3()
                 .then(content => {
                     const assetType = storage.AssetType.Project;
@@ -87,7 +114,8 @@ const ProjectSaverHOC = function (WrappedComponent) {
                         assetType,
                         dataFormat,
                         body,
-                        projectId
+                        projectId,
+                        urlOptions
                     );
                 });
         }
@@ -129,17 +157,21 @@ const ProjectSaverHOC = function (WrappedComponent) {
         onUpdateProject: PropTypes.func,
         onUpdatedProject: PropTypes.func,
         reduxProjectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
+        reduxProjectTitle: PropTypes.string,
         vm: PropTypes.instanceOf(VM).isRequired
     };
     const mapStateToProps = state => {
         const loadingState = state.scratchGui.projectState.loadingState;
         return {
-            isCreating: getIsCreating(loadingState),
+            isCreatingCopy: getIsCreatingCopy(loadingState),
+            isCreatingNew: getIsCreatingNew(loadingState),
+            isRemixing: getIsRemixing(loadingState),
             isShowingWithId: getIsShowingProject(loadingState),
             isShowingWithoutId: getIsShowingWithoutId(loadingState),
             isUpdating: getIsUpdating(loadingState),
             loadingState: loadingState,
             reduxProjectId: state.scratchGui.projectState.projectId,
+            reduxProjectTitle: state.scratchGui.projectTitle,
             vm: state.scratchGui.vm
         };
     };
diff --git a/src/reducers/project-state.js b/src/reducers/project-state.js
index 734a05b13..1a288a551 100644
--- a/src/reducers/project-state.js
+++ b/src/reducers/project-state.js
@@ -54,11 +54,15 @@ const getIsLoadingWithId = loadingState => (
     loadingState === LoadingState.LOADING_VM_WITH_ID ||
     loadingState === LoadingState.LOADING_VM_NEW_DEFAULT
 );
-const getIsCreating = loadingState => (
-    loadingState === LoadingState.CREATING_NEW ||
-    loadingState === LoadingState.REMIXING ||
+const getIsCreatingNew = loadingState => (
+    loadingState === LoadingState.CREATING_NEW
+);
+const getIsCreatingCopy = loadingState => (
     loadingState === LoadingState.CREATING_COPY
 );
+const getIsRemixing = loadingState => (
+    loadingState === LoadingState.REMIXING
+);
 const getIsUpdating = loadingState => (
     loadingState === LoadingState.UPDATING ||
     loadingState === LoadingState.UPDATING_BEFORE_NEW
@@ -401,11 +405,13 @@ export {
     defaultProjectId,
     doneCreatingProject,
     doneUpdatingProject,
-    getIsCreating,
+    getIsCreatingCopy,
+    getIsCreatingNew,
     getIsError,
     getIsFetchingWithId,
     getIsFetchingWithoutId,
     getIsLoadingWithId,
+    getIsRemixing,
     getIsShowingProject,
     getIsShowingWithId,
     getIsShowingWithoutId,
-- 
GitLab