Skip to content

Commit

Permalink
MVJ-421 Fix review days in edit mode (#553)
Browse files Browse the repository at this point in the history
rent tab: tasotarkistus review days according to index type and enum name fixes
  • Loading branch information
NC-jsAhonen authored Dec 11, 2024
1 parent 4795038 commit 9445bd7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { connect } from "react-redux";
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
OldDwellingsInHousingCompaniesPriceIndexType,
} from "@/leases/types";
import BoxItemContainer from "@/components/content/BoxItemContainer";
import { withWindowResize } from "@/components/resize/WindowResizeHandler";
Expand All @@ -24,9 +25,11 @@ import { getUiDataLeaseKey } from "@/uiData/helpers";
import { formatDate, getFieldAttributes } from "@/util/helpers";
import FormField from "@/components/form/FormField";
import { Attributes } from "@/types";
import { getReviewDays } from "@/leases/helpers";

type Props = {
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps;
oldDwellingsInHousingCompaniesPriceIndexType: OldDwellingsInHousingCompaniesPriceIndexType;
leaseAttributes: Attributes;
leaseStartDate: string;
isSaveClicked: boolean;
Expand All @@ -45,37 +48,24 @@ const getLastYearsIndexPointNumber = (
: "Indeksipisteluvut puuttuvat";
};

const getReviewDaysSorted = (
pointFigures: IndexPointFigureYearlyProps[],
): IndexPointFigureYearlyProps[] => {
// deep copy
const sortedNumbers = JSON.parse(JSON.stringify(pointFigures));

sortedNumbers.sort(
(a: IndexPointFigureYearlyProps, b: IndexPointFigureYearlyProps) =>
a.year - b.year,
);
return sortedNumbers;
};

class OldDwellingsInHousingCompaniesPriceIndexViewEdit extends PureComponent<Props> {
class OldDwellingsInHousingCompaniesPriceIndexEdit extends PureComponent<Props> {
render() {
const {
oldDwellingsInHousingCompaniesPriceIndex,
oldDwellingsInHousingCompaniesPriceIndexType,
leaseAttributes,
leaseStartDate,
isSaveClicked,
} = this.props;

if (!oldDwellingsInHousingCompaniesPriceIndex) {
return <p>Lisää tasotarkistus</p>;
return null;
}

const {
point_figures: pointFigures,
source_table_label: sourceTableLabel,
} = oldDwellingsInHousingCompaniesPriceIndex || {};
console.log("leaseAttributes", leaseAttributes);
return (
<Fragment>
<BoxItemContainer>
Expand All @@ -90,7 +80,6 @@ class OldDwellingsInHousingCompaniesPriceIndexViewEdit extends PureComponent<Pro
name="old_dwellings_in_housing_companies_price_index_type"
overrideValues={{
label: LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.TYPE,
type: "choice",
}}
// enableUiDataEdit
// uiDataKey={getUiDataLeaseKey(LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.OLD_DWELLINGS_IN_HOUSING_COMPANIES_PRICE_INDEX_TYPE)}
Expand Down Expand Up @@ -124,22 +113,15 @@ class OldDwellingsInHousingCompaniesPriceIndexViewEdit extends PureComponent<Pro
}
</FormTextTitle>
<>
{pointFigures && !!pointFigures.length
? getReviewDaysSorted(pointFigures).map(
(number: IndexPointFigureYearlyProps, index: number) => {
return (
<FormText
key={
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS +
`[${index}]`
}
>
{`1.1.${number.year}`}
</FormText>
);
},
)
: ""}
{leaseStartDate ?
getReviewDays(leaseStartDate, oldDwellingsInHousingCompaniesPriceIndexType).map(
(date: string, index: number) => {
return <FormText key={LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS + `[${index}]`}>
{date}
</FormText>
}
)
: ""}
</>
</Column>
</Row>
Expand All @@ -157,4 +139,4 @@ export default flowRight(
leaseStartDate: getCurrentLeaseStartDate(state),
};
}),
)(OldDwellingsInHousingCompaniesPriceIndexViewEdit);
)(OldDwellingsInHousingCompaniesPriceIndexEdit);
6 changes: 5 additions & 1 deletion src/leases/components/leaseSections/rent/RentItemEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class RentItemEdit extends PureComponent<Props, State> {
rentTypeIsManual = rentType === RentTypes.MANUAL,
rentTypeIsFixed = rentType === RentTypes.FIXED;
const oldDwellingsInHousingCompaniesPriceIndex = get(savedRent, 'old_dwellings_in_housing_companies_price_index');
const oldDwellingsInHousingCompaniesPriceIndexType = get(savedRent, 'old_dwellings_in_housing_companies_price_index_type');
return <Collapse archived={archived} defaultOpen={rentCollapseState !== undefined ? rentCollapseState : active || rents.length === 1 && !archived} hasErrors={isSaveClicked && !isEmpty(rentErrors)} headerTitle={<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.TYPE)}>
{getLabelOfOption(typeOptions, get(savedRent, 'type')) || '-'}
</Authorization>} headerSubtitles={<Column small={6} medium={8} large={10}>
Expand All @@ -278,7 +279,10 @@ class RentItemEdit extends PureComponent<Props, State> {

<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OLD_DWELLINGS_IN_HOUSING_COMPANIES_PRICE_INDEX)}>
<Collapse className='collapse__secondary' defaultOpen={oldDwellingsInHousingCompaniesPriceIndexCollapseState !== undefined ? oldDwellingsInHousingCompaniesPriceIndexCollapseState : true} hasErrors={/*TODO: Error handling*/false} headerTitle={`${LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.OLD_DWELLINGS_IN_HOUSING_COMPANIES_PRICE_INDEX}`} onToggle={this.handleFixedInitialYearRentsCollapseToggle}>
<OldDwellingsInHousingCompaniesPriceIndexEdit oldDwellingsInHousingCompaniesPriceIndex={oldDwellingsInHousingCompaniesPriceIndex} />
<OldDwellingsInHousingCompaniesPriceIndexEdit
oldDwellingsInHousingCompaniesPriceIndex={oldDwellingsInHousingCompaniesPriceIndex}
oldDwellingsInHousingCompaniesPriceIndexType={oldDwellingsInHousingCompaniesPriceIndexType}
/>
</Collapse>
</Authorization>

Expand Down
8 changes: 4 additions & 4 deletions src/leases/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1615,11 +1615,11 @@ export const calculatorTypeOptions = [{
}];

export const oldDwellingsInHousingCompaniesPriceIndexTypes = {
["TASOTARKISTUS_20_20"]: "Tasotarkistus 20v/20v",
["TASOTARKISTUS_20_10"]: "Tasotarkistus 20v/10v",
TASOTARKISTUS_20_20: "Tasotarkistus 20v/20v",
TASOTARKISTUS_20_10: "Tasotarkistus 20v/10v",
}

export const oldDwellingsInHousingCompaniesPriceIndexTypeOptions = {
TYPE_20_20: "TASOTARKISTUS_20_20",
TYPE_20_10: "TASOTARKISTUS_20_10",
TASOTARKISTUS_20_20: "TASOTARKISTUS_20_20",
TASOTARKISTUS_20_10: "TASOTARKISTUS_20_10",
};
17 changes: 9 additions & 8 deletions src/leases/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2943,23 +2943,24 @@ export const sortRelatedHistoryItems = (a: Record<string, any>, b: Record<string
*/
export const getReviewDays = (startDate: string, priceIndexType: OldDwellingsInHousingCompaniesPriceIndexType): Array<string> => {
const checkDays = [];
let increment: number;
let increments: Array<number>;

if (priceIndexType === oldDwellingsInHousingCompaniesPriceIndexTypeOptions.TYPE_20_10) {
increment = 10;
} else if (priceIndexType === oldDwellingsInHousingCompaniesPriceIndexTypeOptions.TYPE_20_20) {
increment = 20;
if (priceIndexType === oldDwellingsInHousingCompaniesPriceIndexTypeOptions.TASOTARKISTUS_20_10) {
increments = [20, 10, 10];
} else if (priceIndexType === oldDwellingsInHousingCompaniesPriceIndexTypeOptions.TASOTARKISTUS_20_20) {
increments = [20, 20, 20];
} else {
return [];
}

for (let i = 1; i <= 3; i++) {
increments.forEach((_, i) => {
const date = new Date(startDate);
date.setDate(1);
date.setMonth(6);
date.setFullYear(date.getFullYear() + i * increment);
const totalCurrentIncrements = increments.slice(0, i + 1).reduce((a, b) => a + b, 0);
date.setFullYear(date.getFullYear() + totalCurrentIncrements);
checkDays.push(date.toLocaleDateString('fi-FI'));
}
})

return checkDays;
}

0 comments on commit 9445bd7

Please sign in to comment.