Skip to content

Instantly share code, notes, and snippets.

@ktusznio
Last active October 16, 2015 20:51
Show Gist options
  • Save ktusznio/e68b7244b8e9818318df to your computer and use it in GitHub Desktop.
Save ktusznio/e68b7244b8e9818318df to your computer and use it in GitHub Desktop.
onSectionSubmit = (sectionConfig, options = {}) => {
var applicationData = this.props.applicationData;
if (!applicationData.isAuthenticated()) {
applicationData.fetch().done(data => {
var sectionConfigs = this.state.sectionConfigs;
sectionConfigs.push(this.getNextSectionConfig(sectionConfig));
this.setState({sectionConfigs});
this.scrollToLastSection();
});
return;
}
var {section, model, modelIndex} = sectionConfig.unpack();
if (this.sectionsWithoutSaveOnSubmit.indexOf(section) === -1) {
if (this.sectionsStoredInAppModel.indexOf(section) !== -1) {
applicationData.set(section, model);
} else {
this.state.applicant.saveSection(section, model, modelIndex);
}
applicationData.save().fail(function() {
FinancingHelpers.alert('Error saving application. Please try again.');
});
}
// Generate next state.
var nextSectionConfig;
var sectionInReview;
if (this.isSectionInReview(sectionConfig)) {
if (section === FinancingConstants.EMPLOYMENT_KIND) {
this.didSubmitEmploymentKindSectionInReview(sectionConfig);
}
nextSectionConfig = this.getNextSectionToReview(sectionConfig) || this.buildReviewSectionConfig();
sectionInReview = nextSectionConfig;
} else {
nextSectionConfig = this.getNextSectionConfig(sectionConfig);
sectionInReview = null;
}
var applicant = nextSectionConfig.get('applicant') || this.state.applicant;
var applicantIndex = applicant === this.props.applicant ? 0 : 1;
var sectionConfigs;
if (this.isSectionInPage(nextSectionConfig)) {
sectionConfigs = this.state.sectionConfigs;
} else {
var insertIndex = this.findSectionConfigIndex(sectionConfig) + 1;
sectionConfigs = this.state.sectionConfigs.slice(0, insertIndex)
.concat([nextSectionConfig])
.concat(this.state.sectionConfigs.slice(insertIndex));
}
this.setState({applicant, applicantIndex, sectionConfigs, sectionInReview});
// Scroll to next section.
if (options.scrollToTop) {
this.scrollToTop();
} else {
this.scrollToSectionConfig(nextSectionConfig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment