Commit 0d148b49 authored by Matija Obreza's avatar Matija Obreza
Browse files

Fix: Don't update Subset and Dataset basic info unless form is dirty

- Prevents updates on simple UI navigation
parent 6eca93c4
import * as React from 'react'; import * as React from 'react';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { submit, isInvalid } from 'redux-form'; import { submit, isDirty, isInvalid } from 'redux-form';
// actions // actions
import { loadMyPartners } from 'partners/actions/dashboard'; import { loadMyPartners } from 'partners/actions/dashboard';
import { loadCrops } from 'crop/actions/public'; import { loadCrops } from 'crop/actions/public';
...@@ -10,14 +10,13 @@ import { DATASET_BASIC_INFO_FORM } from 'datasets/constants'; ...@@ -10,14 +10,13 @@ import { DATASET_BASIC_INFO_FORM } from 'datasets/constants';
// models // models
import Partner from 'model/genesys/Partner'; import Partner from 'model/genesys/Partner';
import Crop from 'model/genesys/Crop'; import Crop from 'model/genesys/Crop';
// utilities
import { log } from 'utilities/debug';
// ui // ui
import StepperTemplate from 'ui/common/stepper/StepperTemplate'; import StepperTemplate from 'ui/common/stepper/StepperTemplate';
import BasicInfoForm from './BasicInfoForm'; import BasicInfoForm from './BasicInfoForm';
interface IDatasetProps extends React.ClassAttributes<any> { interface IDatasetProps extends React.ClassAttributes<any> {
isFormDirty: boolean;
loadMyPartners: any; loadMyPartners: any;
myPartners: Partner[]; myPartners: Partner[];
submit: any; submit: any;
...@@ -28,9 +27,13 @@ interface IDatasetProps extends React.ClassAttributes<any> { ...@@ -28,9 +27,13 @@ interface IDatasetProps extends React.ClassAttributes<any> {
class BasicInfoStep extends StepperTemplate<IDatasetProps> { class BasicInfoStep extends StepperTemplate<IDatasetProps> {
protected gotoStep = (id) => { protected gotoStep = (id) => {
const { submit, onGotoStep } = this.props; const { submit, isFormDirty, onGotoStep } = this.props;
log('Saving form'); if (isFormDirty) {
submit(DATASET_BASIC_INFO_FORM); // log('Saving form');
submit(DATASET_BASIC_INFO_FORM);
} else {
console.log(`Form ${DATASET_BASIC_INFO_FORM} not dirty`);
}
setTimeout(() => onGotoStep(id)); setTimeout(() => onGotoStep(id));
} }
...@@ -52,6 +55,7 @@ const mapStateToProps = (state, ownProps) => ({ ...@@ -52,6 +55,7 @@ const mapStateToProps = (state, ownProps) => ({
crops: state.crop.public.list, crops: state.crop.public.list,
myPartners: state.partner.dashboard.myPartners, myPartners: state.partner.dashboard.myPartners,
isInvalidForm: isInvalid(DATASET_BASIC_INFO_FORM)(state), isInvalidForm: isInvalid(DATASET_BASIC_INFO_FORM)(state),
isFormDirty: isDirty(DATASET_BASIC_INFO_FORM)(state),
}); });
const mapDispatchToProps = (dispatch) => bindActionCreators({ const mapDispatchToProps = (dispatch) => bindActionCreators({
......
import * as React from 'react'; import * as React from 'react';
import { translate } from 'react-i18next'; import { translate } from 'react-i18next';
import {bindActionCreators} from 'redux'; import {bindActionCreators} from 'redux';
import {isInvalid, submit} from 'redux-form'; import {isInvalid, isDirty, submit} from 'redux-form';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
// constants // constants
import {SUBSET_FORM} from 'subsets/constants'; import {SUBSET_FORM} from 'subsets/constants';
...@@ -13,6 +13,7 @@ import StepperTemplate from 'ui/common/stepper/StepperTemplate'; ...@@ -13,6 +13,7 @@ import StepperTemplate from 'ui/common/stepper/StepperTemplate';
interface ISubsetProps extends React.ClassAttributes<any> { interface ISubsetProps extends React.ClassAttributes<any> {
isInvalidForm: boolean; isInvalidForm: boolean;
isFormDirty: boolean;
submit: any; submit: any;
crops: Crop[]; crops: Crop[];
t: any; t: any;
...@@ -23,9 +24,14 @@ class BasicInfoStep extends StepperTemplate<ISubsetProps> { ...@@ -23,9 +24,14 @@ class BasicInfoStep extends StepperTemplate<ISubsetProps> {
protected renderContent = () => (<BasicInfoForm initialValues={ this.props.item } t={ this.props.t } crops={ this.props.crops }/>); protected renderContent = () => (<BasicInfoForm initialValues={ this.props.item } t={ this.props.t } crops={ this.props.crops }/>);
protected gotoStep = (id) => { protected gotoStep = (id) => {
const {onGotoStep, submit} = this.props; const { onGotoStep, submit, isFormDirty } = this.props;
if (onGotoStep) { if (onGotoStep) {
submit(SUBSET_FORM); if (isFormDirty) {
submit(SUBSET_FORM);
} else {
console.log(`Form ${SUBSET_FORM} not dirty`);
}
setTimeout(() => onGotoStep(id)); setTimeout(() => onGotoStep(id));
} }
} }
...@@ -39,6 +45,7 @@ class BasicInfoStep extends StepperTemplate<ISubsetProps> { ...@@ -39,6 +45,7 @@ class BasicInfoStep extends StepperTemplate<ISubsetProps> {
const mapStateToProps = (state, ownProps) => ({ const mapStateToProps = (state, ownProps) => ({
crops: state.crop.public.list, crops: state.crop.public.list,
isInvalidForm: isInvalid(SUBSET_FORM)(state), isInvalidForm: isInvalid(SUBSET_FORM)(state),
isFormDirty: isDirty(SUBSET_FORM)(state),
}); });
const mapDispatchToProps = (dispatch) => bindActionCreators({ const mapDispatchToProps = (dispatch) => bindActionCreators({
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment