Skip to content

Commit

Permalink
MVJ-421 tasotarkistus to rents tab (#549)
Browse files Browse the repository at this point in the history
* tasotarkistus to rents tab: static view
  • Loading branch information
NC-jsAhonen authored Dec 5, 2024
1 parent 2c27a16 commit 242c63c
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, { Fragment, PureComponent } from 'react';
import { Row, Column } from "react-foundation";
import { getCurrentLeaseStartDate, getAttributes as getLeaseAttributes } from '@/leases/selectors';
import { flowRight } from 'lodash';
import { connect } from 'react-redux';
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
} from '@/leases/types';
import BoxItemContainer from '@/components/content/BoxItemContainer';
import { withWindowResize } from '@/components/resize/WindowResizeHandler';
import FormText from "@/components/form/FormText";
import FormTextTitle from '@/components/form/FormTextTitle';
import { LeaseFieldTitles, LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths, LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles } from '@/leases/enums';
import { getUiDataLeaseKey } from '@/uiData/helpers';
import { formatDate } from '@/util/helpers';

type Props = {
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps;
leaseStartDate: string;
};

const getLastYearsIndexPointNumber = (pointFigures: IndexPointFigureYearlyProps[]): string => {
const lastYear = new Date().getFullYear() - 1;
const lastYearIndex = pointFigures?.find((x: IndexPointFigureYearlyProps) => x.year == lastYear) || null;
return lastYearIndex ? `${lastYearIndex.year} * ${lastYearIndex.value}` : '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 OldDwellingsInHousingCompaniesPriceIndexView extends PureComponent<Props> {
render() {
const {
oldDwellingsInHousingCompaniesPriceIndex,
leaseStartDate
} = this.props;
const {
name,
point_figures: pointFigures,
source_table_label: sourceTableLabel
} = oldDwellingsInHousingCompaniesPriceIndex || {};
return <Fragment>
<BoxItemContainer>
<Row>
<Column>
<FormTextTitle uiDataKey={getUiDataLeaseKey(LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NAME)}>
{LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.NAME}
</FormTextTitle>
<FormText>{name}</FormText>
</Column>
<Column>
<FormTextTitle>
{LeaseFieldTitles.START_DATE}
</FormTextTitle>
<FormText>{formatDate(leaseStartDate)}</FormText>
</Column>
<Column>
<FormTextTitle uiDataKey={getUiDataLeaseKey(LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS)}>
{LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.NUMBERS}
</FormTextTitle>
<FormText>{getLastYearsIndexPointNumber(pointFigures)}</FormText>
<FormText>{sourceTableLabel}</FormText>
</Column>
<Column>
<FormTextTitle uiDataKey={getUiDataLeaseKey(LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS)}>
{LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.REVIEW_DAYS}
</FormTextTitle>
<>
{pointFigures && !!pointFigures.length ?
getReviewDaysSorted(pointFigures).map(
(number: IndexPointFigureYearlyProps, index: number) => {
return <FormText key={LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS + `[${index}]`}>
{`1.1.${number.year}`}
</FormText>
}
)
: ""}
</>
</Column>
</Row>
</BoxItemContainer>
</Fragment>
}
}

export default flowRight(withWindowResize, connect(state => {
return {
leaseAttributes: getLeaseAttributes(state),
leaseStartDate: getCurrentLeaseStartDate(state)
};
}))(OldDwellingsInHousingCompaniesPriceIndexView);
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React, { Fragment, PureComponent } from "react";
import { Row, Column } from "react-foundation";
import {
getCurrentLeaseStartDate,
getAttributes as getLeaseAttributes,
} from "@/leases/selectors";
import { flowRight } from "lodash";
import { connect } from "react-redux";
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
} from "@/leases/types";
import BoxItemContainer from "@/components/content/BoxItemContainer";
import { withWindowResize } from "@/components/resize/WindowResizeHandler";
import FormText from "@/components/form/FormText";
import FormTextTitle from "@/components/form/FormTextTitle";
import {
LeaseFieldTitles,
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths,
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles,
} from "@/leases/enums";
import { getUiDataLeaseKey } from "@/uiData/helpers";
import { formatDate } from "@/util/helpers";

type Props = {
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps;
leaseStartDate: string;
};

const getLastYearsIndexPointNumber = (
pointFigures: IndexPointFigureYearlyProps[]
): string => {
const lastYear = new Date().getFullYear() - 1;
const lastYearIndex =
pointFigures?.find(
(x: IndexPointFigureYearlyProps) => x.year == lastYear
) || null;
return lastYearIndex
? `${lastYearIndex.year} * ${lastYearIndex.value}`
: "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> {
render() {
const { oldDwellingsInHousingCompaniesPriceIndex, leaseStartDate } =
this.props;

console.log("oldDwellingsInHousingCompaniesPriceIndex", oldDwellingsInHousingCompaniesPriceIndex);

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

const {
name,
point_figures: pointFigures,
source_table_label: sourceTableLabel,
} = oldDwellingsInHousingCompaniesPriceIndex || {};
return (
<Fragment>
<BoxItemContainer>
<Row>
<Column>
<FormTextTitle
uiDataKey={getUiDataLeaseKey(
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NAME
)}
>
{
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.NAME
}
</FormTextTitle>
<FormText>{name}</FormText>
</Column>
<Column>
<FormTextTitle>{LeaseFieldTitles.START_DATE}</FormTextTitle>
<FormText>{formatDate(leaseStartDate)}</FormText>
</Column>
<Column>
<FormTextTitle
uiDataKey={getUiDataLeaseKey(
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS
)}
>
{
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.NUMBERS
}
</FormTextTitle>
<FormText>{getLastYearsIndexPointNumber(pointFigures)}</FormText>
<FormText>{sourceTableLabel}</FormText>
</Column>
<Column>
<FormTextTitle
uiDataKey={getUiDataLeaseKey(
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS
)}
>
{
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldTitles.REVIEW_DAYS
}
</FormTextTitle>
<>
{pointFigures && !!pointFigures.length
? getReviewDaysSorted(pointFigures).map(
(number: IndexPointFigureYearlyProps, index: number) => {
return (
<FormText
key={
LeaseRentOldDwellingsInHousingCompaniesPriceIndexFieldPaths.NUMBERS +
`[${index}]`
}
>
{`1.1.${number.year}`}
</FormText>
);
}
)
: ""}
</>
</Column>
</Row>
</BoxItemContainer>
</Fragment>
);
}
}

export default flowRight(
withWindowResize,
connect((state) => {
return {
leaseAttributes: getLeaseAttributes(state),
leaseStartDate: getCurrentLeaseStartDate(state),
};
})
)(OldDwellingsInHousingCompaniesPriceIndexViewEdit);
17 changes: 15 additions & 2 deletions src/leases/components/leaseSections/rent/RentItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import { formatDateRange, getFieldOptions, getLabelOfOption, isActive, isArchive
import { getAttributes as getLeaseAttributes, getCollapseStateByKey } from "@/leases/selectors";
import type { Attributes } from "types";
import type { ServiceUnit } from "@/serviceUnits/types";
import OldDwellingsInHousingCompaniesPriceIndexView from "./OldDwellingsInHousingCompaniesPriceIndex";

const formName = FormNames.LEASE_RENTS;
type Props = {
contractRentsCollapseState: boolean;
equalizedRentsCollapseState: boolean;
oldDwellingsInHousingCompaniesPriceIndexCollapseState: boolean;
fixedInitialYearRentsCollapseState: boolean;
indexAdjustedRentsCollapseState: boolean;
leaseAttributes: Attributes;
Expand All @@ -40,6 +42,7 @@ type Props = {
const RentItem = ({
contractRentsCollapseState,
equalizedRentsCollapseState,
oldDwellingsInHousingCompaniesPriceIndexCollapseState,
fixedInitialYearRentsCollapseState,
indexAdjustedRentsCollapseState,
leaseAttributes,
Expand Down Expand Up @@ -94,6 +97,7 @@ const RentItem = ({
const active = isActive(rent),
archived = isArchived(rent),
rentType = get(rent, 'type'),
oldDwellingsInHousingCompaniesPriceIndex = get(rent, 'old_dwellings_in_housing_companies_price_index', {}),
fixedInitialYearRents = get(rent, 'fixed_initial_year_rents', []),
contractRents = get(rent, 'contract_rents', []),
indexAdjustedRents = get(rent, 'index_adjusted_rents', []),
Expand All @@ -105,14 +109,22 @@ const RentItem = ({
rentTypeIsIndex2022 = rentType === RentTypes.INDEX2022,
rentTypeIsManual = rentType === RentTypes.MANUAL,
rentTypeIsFixed = rentType === RentTypes.FIXED;

return <Collapse archived={archived} defaultOpen={rentCollapseState !== undefined ? rentCollapseState : active || rents.length === 1} headerSubtitles={<Column small={6} medium={8} large={10}>
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.START_DATE) || isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.END_DATE)}>
<CollapseHeaderSubtitle>{formatDateRange(rent.start_date, rent.end_date) || '-'}</CollapseHeaderSubtitle>
</Authorization>
</Column>} headerTitle={<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.TYPE)}>
{getLabelOfOption(typeOptions, rentType) || '-'}
</Authorization>} onToggle={handleRentCollapseToggle}>
<BasicInfo rent={rent} rentType={rentType} serviceUnit={serviceUnit} />
<BasicInfo rent={rent} rentType={rentType} serviceUnit={serviceUnit} />

<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OLD_DWELLINGS_IN_HOUSING_COMPANIES_PRICE_INDEX)}>
{oldDwellingsInHousingCompaniesPriceIndex &&
<Collapse className='collapse__secondary' defaultOpen={oldDwellingsInHousingCompaniesPriceIndexCollapseState !== undefined ? oldDwellingsInHousingCompaniesPriceIndexCollapseState : true} headerTitle='Tasotarkistus'>
<OldDwellingsInHousingCompaniesPriceIndexView oldDwellingsInHousingCompaniesPriceIndex={oldDwellingsInHousingCompaniesPriceIndex} />
</Collapse>}
</Authorization>

<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentFixedInitialYearRentsFieldPaths.FIXED_INITIAL_YEAR_RENTS)}>
{(rentTypeIsIndex || rentTypeIsIndex2022 || rentTypeIsManual) && <Collapse className='collapse__secondary' defaultOpen={fixedInitialYearRentsCollapseState !== undefined ? fixedInitialYearRentsCollapseState : true} headerTitle={`${LeaseRentFixedInitialYearRentsFieldTitles.FIXED_INITIAL_YEAR_RENTS} (${fixedInitialYearRents.length})`} onToggle={handleFixedInitialYearRentsCollapseToggle} uiDataKey={getUiDataLeaseKey(LeaseRentFixedInitialYearRentsFieldPaths.FIXED_INITIAL_YEAR_RENTS)}>
Expand Down Expand Up @@ -152,11 +164,12 @@ const RentItem = ({
</Collapse>;
};

export default connect((state, props) => {
export default connect((state, props: Props) => {
const id = props.rent.id;
return {
contractRentsCollapseState: getCollapseStateByKey(state, `${ViewModes.READONLY}.${formName}.${id}.contract_rents`),
equalizedRentsCollapseState: getCollapseStateByKey(state, `${ViewModes.READONLY}.${formName}.${id}.equalized_rents`),
oldDwellingsInHousingCompaniesPriceIndexCollapseState: getCollapseStateByKey(state, `${ViewModes.READONLY}.${formName}.${id}.old_dwellings_in_housing_companies_price_index`),
fixedInitialYearRentsCollapseState: getCollapseStateByKey(state, `${ViewModes.READONLY}.${formName}.${id}.fixed_initial_year_rents`),
indexAdjustedRentsCollapseState: getCollapseStateByKey(state, `${ViewModes.READONLY}.${formName}.${id}.index_adjusted_rents`),
leaseAttributes: getLeaseAttributes(state),
Expand Down
Loading

0 comments on commit 242c63c

Please sign in to comment.