Skip to content

Commit

Permalink
get tasotarkistus from the api through a reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Dec 13, 2024
1 parent 6fea815 commit 994d17a
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 69 deletions.
19 changes: 19 additions & 0 deletions src/leases/components/LeaseListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ import type { LeaseList } from "@/leases/types";
import type { LessorList } from "@/lessor/types";
import type { ServiceUnits } from "@/serviceUnits/types";
import type { UsersPermissions as UsersPermissionsType, UserServiceUnit } from "@/usersPermissions/types";
import {
getOldDwellingsInHousingCompaniesPriceIndex,
getIsFetching as getIsFetchingOldDwellingsInHousingCompaniesPriceIndex
} from "@/oldDwellingsInHousingCompaniesPriceIndex/selectors";
import { OldDwellingsInHousingCompaniesPriceIndex } from "@/oldDwellingsInHousingCompaniesPriceIndex/types";
import { fetchOldDwellingsInHousingCompaniesPriceIndex } from "@/oldDwellingsInHousingCompaniesPriceIndex/actions";

const VisualizationTypes = {
MAP: 'map',
TABLE: 'table'
Expand All @@ -76,6 +83,7 @@ type Props = {
fetchLeasesByBBox: (...args: Array<any>) => any;
fetchLessors: (...args: Array<any>) => any;
fetchServiceUnits: (...args: Array<any>) => any;
fetchOldDwellingsInHousingCompaniesPriceIndex: (...args: Array<any>) => any;
history: Record<string, any>;
initialize: (...args: Array<any>) => any;
isFetching: boolean;
Expand All @@ -87,6 +95,7 @@ type Props = {
leases: LeaseList;
lessors: LessorList;
location: Record<string, any>;
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndex | null;
receiveTopNavigationSettings: (...args: Array<any>) => any;
serviceUnits: ServiceUnits;
userActiveServiceUnit: UserServiceUnit;
Expand Down Expand Up @@ -130,8 +139,11 @@ class LeaseListPage extends PureComponent<Props, State> {
fetchAreaNoteList,
fetchLessors,
fetchServiceUnits,
fetchOldDwellingsInHousingCompaniesPriceIndex,
isFetchingServiceUnits,
isFetchingOldDwellingsInHousingCompaniesPriceIndex,
lessors,
oldDwellingsInHousingCompaniesPriceIndex,
receiveTopNavigationSettings,
serviceUnits,
leaseAttributes
Expand All @@ -157,6 +169,10 @@ class LeaseListPage extends PureComponent<Props, State> {
});
}

if (!isFetchingOldDwellingsInHousingCompaniesPriceIndex && !oldDwellingsInHousingCompaniesPriceIndex) {
fetchOldDwellingsInHousingCompaniesPriceIndex();
}

window.addEventListener('popstate', this.handlePopState);
this._isMounted = true;
this.setState({
Expand Down Expand Up @@ -723,10 +739,12 @@ export default flowRight(withLeaseAttributes, withUiDataList, connect(state => {
isFetching: getIsFetching(state),
isFetchingByBBox: getIsFetchingByBBox(state),
isFetchingServiceUnits: getIsFetchingServiceUnits(state),
isFetchingOldDwellingsInHousingCompaniesPriceIndex: getIsFetchingOldDwellingsInHousingCompaniesPriceIndex(state),
leases: getLeasesList(state),
lessors: getLessorList(state),
serviceUnits: getServiceUnits(state),
userActiveServiceUnit: getUserActiveServiceUnit(state),
oldDwellingsInHousingCompaniesPriceIndex: getOldDwellingsInHousingCompaniesPriceIndex(state),
usersPermissions: getUsersPermissions(state)
};
}, {
Expand All @@ -736,6 +754,7 @@ export default flowRight(withLeaseAttributes, withUiDataList, connect(state => {
fetchLeasesByBBox,
fetchLessors,
fetchServiceUnits,
fetchOldDwellingsInHousingCompaniesPriceIndex,
initialize,
receiveTopNavigationSettings
}))(LeaseListPage);
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { getCurrentLeaseStartDate, getAttributes as getLeaseAttributes } from '@
import { flowRight } from 'lodash';
import { connect } from 'react-redux';
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
OldDwellingsInHousingCompaniesPriceIndexType,
} from '@/leases/types';
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
} from '@/oldDwellingsInHousingCompaniesPriceIndex/types';
import BoxItemContainer from '@/components/content/BoxItemContainer';
import { withWindowResize } from '@/components/resize/WindowResizeHandler';
import FormText from "@/components/form/FormText";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
import { flowRight } from "lodash";
import { connect } from "react-redux";
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
OldDwellingsInHousingCompaniesPriceIndexType,
} from "@/leases/types";
import type {
OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps,
IndexPointFigureYearly as IndexPointFigureYearlyProps,
} from "@/oldDwellingsInHousingCompaniesPriceIndex/types";
import BoxItemContainer from "@/components/content/BoxItemContainer";
import { withWindowResize } from "@/components/resize/WindowResizeHandler";
import FormText from "@/components/form/FormText";
Expand Down
62 changes: 13 additions & 49 deletions src/leases/components/leaseSections/rent/RentItemEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { getUsersPermissions } from "@/usersPermissions/selectors";
import type { Attributes } from "types";
import type { UsersPermissions as UsersPermissionsType } from "@/usersPermissions/types";
import OldDwellingsInHousingCompaniesPriceIndexEdit from "./OldDwellingsInHousingCompaniesPriceIndexEdit";
import { OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps } from "@/leases/types";
import { OldDwellingsInHousingCompaniesPriceIndex as OldDwellingsInHousingCompaniesPriceIndexProps } from "@/oldDwellingsInHousingCompaniesPriceIndex/types";
import { getOldDwellingsInHousingCompaniesPriceIndex } from "@/oldDwellingsInHousingCompaniesPriceIndex/selectors";

type Props = {
change: (...args: Array<any>) => any;
contractRentsCollapseState: boolean;
Expand All @@ -36,7 +38,8 @@ type Props = {
equalizedRentsCollapseState: boolean;
errors: Record<string, any> | null | undefined;
field: string;
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps;
oldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps | null;
rentOldDwellingsInHousingCompaniesPriceIndex: OldDwellingsInHousingCompaniesPriceIndexProps | null | undefined;
oldDwellingsInHousingCompaniesPriceIndexCollapseState: boolean;
fixedInitialYearRents: Array<Record<string, any>>;
fixedInitialYearRentsCollapseState: boolean;
Expand Down Expand Up @@ -182,55 +185,15 @@ class RentItemEdit extends PureComponent<Props, State> {
addOldDwellingsInHousingCompaniesPriceIndex = () => {
const {
change,
field
field,
oldDwellingsInHousingCompaniesPriceIndex,
} = this.props;

change(
formName,
`${field}.old_dwellings_in_housing_companies_price_index`,
{
id: 1,
point_figures: [
{
id: 4,
value: "97.4",
year: 2023,
region: "pks",
comment: ""
},
{
id: 3,
value: "105.7",
year: 2022,
region: "pks",
comment: ""
},
{
id: 2,
value: "105.6",
year: 2021,
region: "pks",
comment: ""
},
{
id: 1,
value: "100.0",
year: 2020,
region: "pks",
comment: ""
}
],
created_at: "2024-11-14T15:12:50.310543+02:00",
modified_at: "2024-12-08T18:05:37.983780+02:00",
code: "ketj_P_QA_T",
name: "Indeksi (2020=100)",
comment: "Indeksi on suhdeluku, joka kuvaa jonkin muuttujan (esimerkiksi hinnan, määrän tai arvon) suhteellista muutosta perusjakson (esimerkiksi vuoden) suhteen. Kunkin ajankohdan indeksipisteluku ilmoittaa, kuinka monta prosenttia kyseisen ajankohdan tarkasteltava muuttuja on perusjakson arvosta tai määrästä. Perusjakson indeksipistelukujen keskiarvo on 100. Tilastossa julkaistavat hintaindeksit ovat laatuvakioituja ja niiden kehitys voi poiketa neliöhintojen kehityksestä.\r\n",
source: "Tilastokeskus, osakeasuntojen hinnat",
source_table_updated: "2024-05-03T08:00:00+03:00",
source_table_label: "Vanhojen osakeasuntojen hintaindeksi (2020=100) ja kauppojen lukumäärät, vuositasolla muuttujina Vuosi, Alue ja Tiedot",
url: "https://pxdata.stat.fi:443/PxWeb/api/v1/fi/StatFin/ashi/statfin_ashi_pxt_13mq.px"
},
);
oldDwellingsInHousingCompaniesPriceIndex
);
}
handleCollapseToggle = (key: string, val: boolean) => {
const {
Expand Down Expand Up @@ -287,7 +250,7 @@ class RentItemEdit extends PureComponent<Props, State> {
equalizedRentsCollapseState,
field,
fixedInitialYearRents,
oldDwellingsInHousingCompaniesPriceIndex,
rentOldDwellingsInHousingCompaniesPriceIndex,
oldDwellingsInHousingCompaniesPriceIndexCollapseState,
fixedInitialYearRentsCollapseState,
indexAdjustedRentsCollapseState,
Expand Down Expand Up @@ -335,7 +298,7 @@ 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}
oldDwellingsInHousingCompaniesPriceIndex={rentOldDwellingsInHousingCompaniesPriceIndex}
oldDwellingsInHousingCompaniesPriceIndexType={oldDwellingsInHousingCompaniesPriceIndexType}
addOldDwellingsInHousingCompaniesPriceIndex={this.addOldDwellingsInHousingCompaniesPriceIndex}
field={field}
Expand Down Expand Up @@ -395,7 +358,8 @@ export default connect((state: State, props: Props) => {
fixedInitialYearRents: selector(state, `${props.field}.fixed_initial_year_rents`),
isSaveClicked: getIsSaveClicked(state),
leaseAttributes: getLeaseAttributes(state),
oldDwellingsInHousingCompaniesPriceIndex: selector(state, `${props.field}.old_dwellings_in_housing_companies_price_index`),
oldDwellingsInHousingCompaniesPriceIndex: getOldDwellingsInHousingCompaniesPriceIndex(state),
rentOldDwellingsInHousingCompaniesPriceIndex: selector(state, `${props.field}.old_dwellings_in_housing_companies_price_index`),
rentAdjustments: selector(state, `${props.field}.rent_adjustments`),
rentId: id,
rentType: selector(state, `${props.field}.type`),
Expand Down
17 changes: 1 addition & 16 deletions src/leases/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,7 @@ export type IntendedUse = {
name: string;
service_unit: ServiceUnit["id"];
};
export type IndexPointFigureYearly = {
value: number;
year: number;
region: string;
comment: string;
}
export type OldDwellingsInHousingCompaniesPriceIndex = {
code: string;
name: string;
comment: string;
source: string;
source_table_updated?: string;
source_table_label: string;
url: string;
point_figures: IndexPointFigureYearly[];
}

export type OldDwellingsInHousingCompaniesPriceIndexType = "TASOTARKISTUS_20_20" | "TASOTARKISTUS_20_10";

export type FetchAttributesAction = Action<string, void>;
Expand Down
19 changes: 19 additions & 0 deletions src/oldDwellingsInHousingCompaniesPriceIndex/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createAction } from "redux-actions";
import { FETCH_ACTION_STRING, NOT_FOUND_ACTION_STRING, RECEIVE_ACTION_STRING } from "./constants";
import {
FetchOldDwellingsInHousingCompaniesPriceIndexAction,
OldDwellingsInHousingCompaniesPriceIndex,
OldDwellingsInHousingCompaniesPriceIndexNotFoundAction,
ReceiveOldDwellingsInHousingCompaniesPriceIndexAction,
} from "./types";

export const fetchOldDwellingsInHousingCompaniesPriceIndex =
(): FetchOldDwellingsInHousingCompaniesPriceIndexAction => createAction(FETCH_ACTION_STRING)();

export const receiveOldDwellingsInHousingCompaniesPriceIndex = (
payload: OldDwellingsInHousingCompaniesPriceIndex,
): ReceiveOldDwellingsInHousingCompaniesPriceIndexAction =>
createAction(RECEIVE_ACTION_STRING)(payload);

export const notFound = (): OldDwellingsInHousingCompaniesPriceIndexNotFoundAction =>
createAction(NOT_FOUND_ACTION_STRING)();
6 changes: 6 additions & 0 deletions src/oldDwellingsInHousingCompaniesPriceIndex/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const DEFAULT_OLD_DWELLINGS_IN_HOUSING_COMPANIES_PRICE_INDEX_ID = 1;

// Action type strings
export const FETCH_ACTION_STRING = "mvj/oldDwellingsInHousingCompaniesPriceIndex/FETCH";
export const RECEIVE_ACTION_STRING = "mvj/oldDwellingsInHousingCompaniesPriceIndex/RECEIVE";
export const NOT_FOUND_ACTION_STRING = "mvj/oldDwellingsInHousingCompaniesPriceIndex/NOT_FOUND";
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, it } from "vitest";
import {
receiveOldDwellingsInHousingCompaniesPriceIndex,
fetchOldDwellingsInHousingCompaniesPriceIndex,
notFound,
} from "./actions";
import oldDwellingsInHousingCompaniesPriceIndexReducer from "./reducer";
import type {
OldDwellingsInHousingCompaniesPriceIndex,
OldDwellingsInHousingCompaniesPriceIndexState,
} from "./types";
const defaultState: OldDwellingsInHousingCompaniesPriceIndexState = {
isFetching: false,
latest: null,
};

describe("oldDwellingsInHousingCompaniesPriceIndex", () => {
describe("Reducer", () => {
describe("oldDwellingsInHousingCompaniesPriceIndexReducer", () => {
it("should update oldDwellingsInHousingCompaniesPriceIndex", () => {
const dummy: OldDwellingsInHousingCompaniesPriceIndex = {
point_figures: [
{
value: 97.4,
year: 2023,
region: "pks",
comment: "",
},
{
value: 105.7,
year: 2022,
region: "pks",
comment: "",
},
{
value: 105.6,
year: 2021,
region: "pks",
comment: "",
},
{
value: 100.0,
year: 2020,
region: "pks",
comment: "",
},
],
created_at: "2024-11-14T15:12:50.310543+02:00",
modified_at: "2024-12-08T18:05:37.983780+02:00",
code: "ketj_P_QA_T",
name: "Indeksi (2020=100)",
comment:
"Indeksi on suhdeluku, joka kuvaa jonkin muuttujan (esimerkiksi hinnan, määrän tai arvon) suhteellista muutosta perusjakson (esimerkiksi vuoden) suhteen. Kunkin ajankohdan indeksipisteluku ilmoittaa, kuinka monta prosenttia kyseisen ajankohdan tarkasteltava muuttuja on perusjakson arvosta tai määrästä. Perusjakson indeksipistelukujen keskiarvo on 100. Tilastossa julkaistavat hintaindeksit ovat laatuvakioituja ja niiden kehitys voi poiketa neliöhintojen kehityksestä.\r\n",
source: "Tilastokeskus, osakeasuntojen hinnat",
source_table_updated: "2024-05-03T08:00:00+03:00",
source_table_label:
"Vanhojen osakeasuntojen hintaindeksi (2020=100) ja kauppojen lukumäärät, vuositasolla muuttujina Vuosi, Alue ja Tiedot",
url: "https://pxdata.stat.fi:443/PxWeb/api/v1/fi/StatFin/ashi/statfin_ashi_pxt_13mq.px",
};

const newState = { ...defaultState, latest: dummy };
const state = oldDwellingsInHousingCompaniesPriceIndexReducer(
{},
receiveOldDwellingsInHousingCompaniesPriceIndex(dummy),
);
expect(state).to.deep.equal(newState);
});
it("should update isFetching flag to true when fetching vats", () => {
const newState = { ...defaultState, isFetching: true };
const state = oldDwellingsInHousingCompaniesPriceIndexReducer(
{},
fetchOldDwellingsInHousingCompaniesPriceIndex(),
);
expect(state).to.deep.equal(newState);
});
it("should update isFetching flag to false by notFound", () => {
const newState = { ...defaultState };
let state = oldDwellingsInHousingCompaniesPriceIndexReducer(
{},
fetchOldDwellingsInHousingCompaniesPriceIndex(),
);
state = oldDwellingsInHousingCompaniesPriceIndexReducer(state, notFound());
expect(state).to.deep.equal(newState);
});
});
});
});
36 changes: 36 additions & 0 deletions src/oldDwellingsInHousingCompaniesPriceIndex/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { combineReducers } from "redux";
import { handleActions } from "redux-actions";
import type { Reducer } from "@/types";
import {
FETCH_ACTION_STRING,
NOT_FOUND_ACTION_STRING,
RECEIVE_ACTION_STRING,
} from "./constants";
import {
OldDwellingsInHousingCompaniesPriceIndex,
ReceiveOldDwellingsInHousingCompaniesPriceIndexAction,
} from "./types";

const isFetchingReducer: Reducer<boolean> = handleActions(
{
[FETCH_ACTION_STRING]: () => true,
[NOT_FOUND_ACTION_STRING]: () => false,
[RECEIVE_ACTION_STRING]: () => false,
},
false,
);
const latestReducer: Reducer<OldDwellingsInHousingCompaniesPriceIndex> = handleActions(
{
[RECEIVE_ACTION_STRING]: (
state: OldDwellingsInHousingCompaniesPriceIndex,
{ payload }: ReceiveOldDwellingsInHousingCompaniesPriceIndexAction,
) => {
return payload;
},
},
null,
);
export default combineReducers<Record<string, any>, any>({
isFetching: isFetchingReducer,
latest: latestReducer,
});
Loading

0 comments on commit 994d17a

Please sign in to comment.