diff --git a/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js b/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js index 99aed37b1..cb7baabea 100644 --- a/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js +++ b/src/leases/components/leaseSections/rent/RentAdjustmentEdit.js @@ -40,6 +40,8 @@ import { calculateReLeaseDiscountPercent, calculateRentAdjustmentSubventionPercentCumulative, getDecisionById, + hasSubventionDataChanged, + hasSubventionValues, } from '$src/leases/helpers'; import {getUiDataLeaseKey} from '$src/uiData/helpers'; import { @@ -260,24 +262,27 @@ type Props = { usersPermissions: UsersPermissionsType, } -type State = { - showSubventions: boolean, -} - -class RentAdjustmentsEdit extends PureComponent { - state = { - showSubventions: this.props.subventionType ? true : false, - } +class RentAdjustmentsEdit extends PureComponent { componentDidUpdate(prevProps: Props) { - if(this.props.subventionType !== prevProps.subventionType || - this.props.subventionBasePercent !== prevProps.subventionBasePercent || - this.props.subventionGraduatedPercent !== prevProps.subventionGraduatedPercent || - this.props.managementSubventions !== prevProps.managementSubventions || - this.props.temporarySubventions !== prevProps.temporarySubventions) { - const {change, field} = this.props; - - change(formName, `${field}.full_amount`, formatNumber(this.calculateTotalSubventionPercent())); // TODO + if (this.props && hasSubventionDataChanged(prevProps, this.props)) { + const { + change, + field, + fullAmount, + managementSubventions, + temporarySubventions, + subventionBasePercent, + subventionGraduatedPercent + } = this.props; + + let newFullAmount = fullAmount + + if (hasSubventionValues(managementSubventions, temporarySubventions, subventionBasePercent, subventionGraduatedPercent)) { + newFullAmount = this.calculateTotalSubventionPercent() + } + + change(formName, `${field}.full_amount`, formatNumber(newFullAmount)); } } @@ -299,7 +304,10 @@ class RentAdjustmentsEdit extends PureComponent { }; handleAddSubventions = () => { - this.setState({showSubventions: true}); + const {change, field} = this.props; + + // To make the subvention form visible: null => "" + change(formName, `${field}.subvention_type`, ""); } handleRemoveSubventions = () => { @@ -307,9 +315,7 @@ class RentAdjustmentsEdit extends PureComponent { change(formName, `${field}.subvention_type`, null); change(formName, `${field}.management_subventions`, []); - change(formName, `${field}.temporary_subventions`, []); - - this.setState({showSubventions: false}); + change(formName, `${field}.temporary_subventions`, []); } calculateReLeaseDiscountPercent = () => { @@ -341,7 +347,7 @@ class RentAdjustmentsEdit extends PureComponent { type, usersPermissions, } = this.props; - const {showSubventions} = this.state; + const showSubventions = typeof subventionType === "string" const totalSubventionPercent = this.calculateTotalSubventionPercent(); return ( diff --git a/src/leases/helpers.js b/src/leases/helpers.js index b6e48375e..c9c5b47d8 100644 --- a/src/leases/helpers.js +++ b/src/leases/helpers.js @@ -1096,6 +1096,31 @@ export const getContentEqualizedRents = (rent: Object): Array => }; }); +/** + * Check if subvention data has changed for rent adjustments. + * If current props are undefined, they are not considered changed. + * @param {Object} prevProps + * @param {Object} props + * @return {boolean} + */ +export const hasSubventionDataChanged = (prevProps: Object, props: Object): boolean => { + if ( + typeof props.subventionType === "undefined" || + typeof props.subventionBasePercent === "undefined" || + typeof props.subventionGraduatedPercent === "undefined" || + typeof props.managementSubventions === "undefined" || + typeof props.temporarySubventions === "undefined" + ) { + return false + } + + return props.subventionType !== prevProps.subventionType || + props.subventionBasePercent !== prevProps.subventionBasePercent || + props.subventionGraduatedPercent !== prevProps.subventionGraduatedPercent || + props.managementSubventions !== prevProps.managementSubventions || + props.temporarySubventions !== prevProps.temporarySubventions +}; + /** * Calculate re-lease discount percent for rent adjustment subvention * @param {string} subventionBasePercent @@ -1524,6 +1549,19 @@ export const calculateSubventionDiscountTotal = (initialYearRent: number, manage return Number(initialYearRent); }; +/** + * Check if subventions have values for calculation + * @param {number} initialYearRent + * @param {Object[]} managementSubventions + * @param {number} currentAmountPerArea + * @return {number} + */ +export const hasSubventionValues = (managementSubventions: ?Array, temporarySubventions: ?Array, subventionBasePercent: ?string, subventionGraduatedPercent: ?string): boolean => { + let msWithValues = managementSubventions.filter((subvention) => !!subvention.subvention_amount) + let tsWithValues = temporarySubventions.filter((subvention) => !!subvention.subvention_percent) + return !!(msWithValues.length || tsWithValues.length || subventionBasePercent || subventionGraduatedPercent) +}; + /** * Calculate basis of rent subvention percent * @param {number} initialYearRent