Skip to content

Commit

Permalink
fix removing rent adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Apr 30, 2024
1 parent a72e31c commit 9db9013
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/leases/components/leaseSections/rent/RentAdjustmentEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
calculateReLeaseDiscountPercent,
calculateRentAdjustmentSubventionPercentCumulative,
getDecisionById,
hasSubventionDataChanged,
hasSubventionValues,
} from '$src/leases/helpers';
import {getUiDataLeaseKey} from '$src/uiData/helpers';
import {
Expand Down Expand Up @@ -260,24 +262,27 @@ type Props = {
usersPermissions: UsersPermissionsType,
}

type State = {
showSubventions: boolean,
}

class RentAdjustmentsEdit extends PureComponent<Props, State> {
state = {
showSubventions: this.props.subventionType ? true : false,
}
class RentAdjustmentsEdit extends PureComponent<Props> {

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));
}
}

Expand All @@ -299,17 +304,18 @@ class RentAdjustmentsEdit extends PureComponent<Props, State> {
};

handleAddSubventions = () => {
this.setState({showSubventions: true});
const {change, field} = this.props;

// To make the subvention form visible: null => ""
change(formName, `${field}.subvention_type`, "");
}

handleRemoveSubventions = () => {
const {change, field} = this.props;

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 = () => {
Expand Down Expand Up @@ -341,7 +347,7 @@ class RentAdjustmentsEdit extends PureComponent<Props, State> {
type,
usersPermissions,
} = this.props;
const {showSubventions} = this.state;
const showSubventions = typeof subventionType === "string"
const totalSubventionPercent = this.calculateTotalSubventionPercent();

return (
Expand Down
38 changes: 38 additions & 0 deletions src/leases/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,31 @@ export const getContentEqualizedRents = (rent: Object): Array<Object> =>
};
});

/**
* 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
Expand Down Expand Up @@ -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<Object>, temporarySubventions: ?Array<Object>, 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
Expand Down

0 comments on commit 9db9013

Please sign in to comment.