Skip to content

Commit b012a2f

Browse files
committed
cleaned up create and save logic
1 parent 700700e commit b012a2f

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/lib/project-saver-hoc.jsx

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
getIsShowingWithId,
2323
getIsShowingWithoutId,
2424
getIsUpdating,
25-
manualUpdateProject,
2625
projectError
2726
} from '../reducers/project-state';
2827

@@ -51,24 +50,25 @@ const ProjectSaverHOC = function (WrappedComponent) {
5150
this.createRemixToStorage();
5251
}
5352

54-
// check if the project state, and user capabilities, have changed so as to indicate
55-
// that we should create or update project
53+
// see if we should "create" the current project on the server
5654
//
57-
// if we're newly able to create this project on the server, create it!
58-
const showingCreateable = this.props.canCreateNew && this.props.isShowingWithoutId;
59-
const prevShowingCreateable = prevProps.canCreateNew && prevProps.isShowingWithoutId;
60-
const justTriedCreatingNew = prevProps.isCreatingNew; // don't immediately keep trying
61-
if (showingCreateable && !prevShowingCreateable && !justTriedCreatingNew) {
55+
// don't try to create or save immediately after trying to create
56+
if (prevProps.isCreatingNew) return;
57+
// if we're newly able to create this project, create it!
58+
if (this.isShowingCreatable(this.props) && !this.isShowingCreatable(prevProps)) {
6259
this.props.onCreateProject();
63-
} else {
64-
// if we're newly able to save this project, save it!
65-
const showingSaveable = this.props.canSave && this.props.isShowingWithId;
66-
const becameAbleToSave = this.props.canSave && !prevProps.canSave;
67-
const becameShared = this.props.isShared && !prevProps.isShared;
68-
const justTriedUpdating = prevProps.isUpdating; // don't immediately keep trying
69-
if (showingSaveable && !justTriedUpdating && (becameAbleToSave || becameShared)) {
70-
this.props.onAutoUpdateProject();
71-
}
60+
}
61+
62+
// see if we should save/update the current project on the server
63+
//
64+
// don't try to save immediately after trying to save
65+
if (prevProps.isUpdating) return;
66+
// if we're newly able to save this project, save it!
67+
const showingSaveable = this.props.canSave && this.props.isShowingWithId;
68+
const becameAbleToSave = this.props.canSave && !prevProps.canSave;
69+
const becameShared = this.props.isShared && !prevProps.isShared;
70+
if (showingSaveable && (becameAbleToSave || becameShared)) {
71+
this.props.onAutoUpdateProject();
7272
}
7373
}
7474
componentWillUnmount () {
@@ -77,11 +77,15 @@ const ProjectSaverHOC = function (WrappedComponent) {
7777
this.updateProjectToStorage();
7878
}
7979
}
80+
isShowingCreatable (props) {
81+
return props.canCreateNew && props.isShowingWithoutId;
82+
}
8083
updateProjectToStorage () {
8184
this.props.onShowSavingAlert();
8285
return this.storeProject(this.props.reduxProjectId)
8386
.then(() => {
84-
// there is nothing we expect to find in response that we need to check here
87+
// there's an http response object available here, but we don't need to examine
88+
// it, because there are no values contained in it that we care about
8589
this.props.onUpdatedProject(this.props.loadingState);
8690
this.props.onShowSaveSuccessAlert();
8791
})
@@ -190,7 +194,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
190194
onAutoUpdateProject,
191195
onCreatedProject,
192196
onCreateProject,
193-
onManualUpdateProject,
194197
onProjectError,
195198
onShowAlert,
196199
onShowCreateSuccessAlert,
@@ -226,7 +229,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
226229
onAutoUpdateProject: PropTypes.func,
227230
onCreateProject: PropTypes.func,
228231
onCreatedProject: PropTypes.func,
229-
onManualUpdateProject: PropTypes.func,
230232
onProjectError: PropTypes.func,
231233
onShowAlert: PropTypes.func,
232234
onShowCreateSuccessAlert: PropTypes.func,
@@ -258,7 +260,6 @@ const ProjectSaverHOC = function (WrappedComponent) {
258260
onAutoUpdateProject: () => dispatch(autoUpdateProject()),
259261
onCreatedProject: (projectId, loadingState) => dispatch(doneCreatingProject(projectId, loadingState)),
260262
onCreateProject: () => dispatch(createProject()),
261-
onManualUpdateProject: () => dispatch(manualUpdateProject()),
262263
onProjectError: error => dispatch(projectError(error)),
263264
onShowAlert: alertType => dispatch(showStandardAlert(alertType)),
264265
onShowCreateSuccessAlert: () => showAlertWithTimeout(dispatch, 'createSuccess'),

0 commit comments

Comments
 (0)