Skip to content

Commit

Permalink
WIP don't merge
Browse files Browse the repository at this point in the history
  • Loading branch information
juho-kettunen-nc committed Nov 14, 2024
1 parent 8770f9b commit 182ceae
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 57 deletions.
53 changes: 30 additions & 23 deletions src/leases/components/leaseSections/rent/BasicInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import FormText from "@/components/form/FormText";
import FormTextTitle from "@/components/form/FormTextTitle";
import { LeaseRentDueDatesFieldPaths, LeaseRentDueDatesFieldTitles, LeaseRentsFieldPaths, LeaseRentsFieldTitles, RentCycles, RentTypes, RentDueDateTypes, LeaseFieldPaths } from "@/leases/enums";
import { formatDueDates, formatSeasonalDate, sortDueDates } from "@/leases/helpers";
import { showOverrideReceivableTypeField } from "@/leases/helpers";
import { getUiDataLeaseKey } from "@/uiData/helpers";
import { formatDate, formatNumber, getFieldOptions, getLabelOfOption, isEmptyValue, isFieldAllowedToRead } from "@/util/helpers";
import { getAttributes as getLeaseAttributes } from "@/leases/selectors";
import type { Attributes } from "types";
import { getReceivableTypes } from "@/leaseCreateCharge/selectors";
type SeasonalDatesProps = {
leaseAttributes: Attributes;
rent: Record<string, any>;
Expand All @@ -36,17 +36,19 @@ const SeasonalDates = ({

type Props = {
leaseAttributes: Attributes;
receivableTypes?: Array<any> | null | undefined;
receivableTypeOptions?: Array<any> | null | undefined;
rent: Record<string, any>;
rentType: string | null | undefined;
};

type PropsWithServiceUnitId = Props & {
serviceUnitId: number;
}

const BasicInfoIndexOrManual = ({
leaseAttributes,
receivableTypeOptions,
rent
}: Props) => {
rent,
serviceUnitId,
}: PropsWithServiceUnitId) => {
const areOldInfoVisible = () => {
return !isEmptyValue(rent.elementary_index) || !isEmptyValue(rent.index_rounding) || !isEmptyValue(rent.x_value) || !isEmptyValue(rent.y_value) || !isEmptyValue(rent.y_value_start) || !isEmptyValue(rent.equalization_start_date) || !isEmptyValue(rent.equalization_end_date);
};
Expand All @@ -55,6 +57,7 @@ const BasicInfoIndexOrManual = ({
const cycleOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.CYCLE);
const indexTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.INDEX_TYPE);
const dueDatesTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.DUE_DATES_TYPE);
const receivableTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE);
const oldValuesVisible = areOldInfoVisible();
return <Fragment>
<Row>
Expand Down Expand Up @@ -191,17 +194,18 @@ const BasicInfoIndexOrManual = ({
</Authorization>
</Column>
</Row>

{ showOverrideReceivableTypeField(serviceUnitId) &&
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
<Row>
<Column>
<FormTextTitle uiDataKey={getUiDataLeaseKey(LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
{LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE}
</FormTextTitle>
<FormText>{getLabelOfOption(receivableTypeOptions, rent?.override_receivable_type) || '-'}</FormText>
<FormText>{getLabelOfOption(receivableTypeOptions, rent.override_receivable_type) || '-'}</FormText>
</Column>
</Row>
</Authorization>
</Authorization>
}

{oldValuesVisible && <Row>
<Column small={12} medium={4} large={2}>
Expand Down Expand Up @@ -335,12 +339,13 @@ const BasicInfoOneTime = ({

const BasicInfoFixed = ({
leaseAttributes,
receivableTypeOptions,
rent
}: Props) => {
rent,
serviceUnitId,
}: PropsWithServiceUnitId) => {
const dueDatesTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.DUE_DATES_TYPE);
const typeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.TYPE);

const receivableTypeOptions = getFieldOptions(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE);

return <Fragment>
<Row>
<Column small={6} medium={4} large={2}>
Expand Down Expand Up @@ -433,16 +438,19 @@ const BasicInfoFixed = ({
</Authorization>
</Column>
</Row>

{ showOverrideReceivableTypeField(serviceUnitId) &&
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
<Row>
<Column>
<FormTextTitle uiDataKey={getUiDataLeaseKey(LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
{LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE}
</FormTextTitle>
<FormText>{getLabelOfOption(receivableTypeOptions, rent?.override_receivable_type) || '-'}</FormText>
<FormText>{getLabelOfOption(receivableTypeOptions, rent.override_receivable_type) || '-'}</FormText>
</Column>
</Row>
</Authorization>
}
</Fragment>;
};

Expand Down Expand Up @@ -498,25 +506,24 @@ const BasicInfoFree = ({
</Fragment>;
};


const BasicInfo = ({
leaseAttributes,
receivableTypes,
rent,
rentType
}: Props) => {
const receivableTypeOptions = receivableTypes?.map((rt) => ({ value: rt.id, label: rt.name })) || [];
rentType,
serviceUnitId,
}: PropsWithServiceUnitId) => {
return <Fragment>
{!rentType && <FormText>Vuokralajia ei ole valittu</FormText>}
{(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && <BasicInfoIndexOrManual leaseAttributes={leaseAttributes} receivableTypeOptions={receivableTypeOptions} rent={rent} rentType={rentType} />}
{rentType === RentTypes.ONE_TIME && <BasicInfoOneTime leaseAttributes={leaseAttributes} receivableTypeOptions={receivableTypeOptions} rent={rent} rentType={rentType} />}
{rentType === RentTypes.FIXED && <BasicInfoFixed leaseAttributes={leaseAttributes} receivableTypeOptions={receivableTypeOptions} rent={rent} rentType={rentType} />}
{(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && <BasicInfoIndexOrManual leaseAttributes={leaseAttributes} rent={rent} rentType={rentType} serviceUnitId={serviceUnitId}/>}
{rentType === RentTypes.ONE_TIME && <BasicInfoOneTime leaseAttributes={leaseAttributes} rent={rent} rentType={rentType} />}
{rentType === RentTypes.FIXED && <BasicInfoFixed leaseAttributes={leaseAttributes} rent={rent} rentType={rentType} serviceUnitId={serviceUnitId} />}
{rentType === RentTypes.FREE && <BasicInfoFree leaseAttributes={leaseAttributes} rent={rent} rentType={rentType} />}
</Fragment>;
};

export default connect(state => {
return {
leaseAttributes: getLeaseAttributes(state),
receivableTypes: getReceivableTypes(state)
};
})(BasicInfo);
})(BasicInfo);
74 changes: 52 additions & 22 deletions src/leases/components/leaseSections/rent/BasicInfoEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import { rentCustomDateOptions } from "@/leases/constants";
import { FieldTypes, FormNames } from "@/enums";
import { DueDatesPositions, FixedDueDates, LeaseRentDueDatesFieldPaths, LeaseRentDueDatesFieldTitles, LeaseRentsFieldPaths, LeaseRentsFieldTitles, RentCycles, RentTypes, RentDueDateTypes } from "@/leases/enums";
import { UsersPermissions } from "@/usersPermissions/enums";
import { formatDueDates, formatSeasonalDate } from "@/leases/helpers";
import { formatDueDates, formatSeasonalDate, showOverrideReceivableTypeField } from "@/leases/helpers";
import { getUiDataLeaseKey } from "@/uiData/helpers";
import { getFieldAttributes, hasPermissions, isFieldAllowedToEdit, isFieldAllowedToRead, isFieldRequired } from "@/util/helpers";
import { getAttributes as getLeaseAttributes, getCurrentLease } from "@/leases/selectors";
import { getReceivableTypes } from "@/leaseCreateCharge/selectors";
import { getLeaseTypeList } from "@/leaseType/selectors";
import { getUsersPermissions } from "@/usersPermissions/selectors";
import { PlotSearchFieldPaths } from "@/plotSearch/enums";
Expand Down Expand Up @@ -182,7 +183,7 @@ type BasicInfoEmptyProps = {

const BasicInfoEmpty = ({
isSaveClicked,
leaseAttributes
leaseAttributes,
}: BasicInfoEmptyProps) => {
return <Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.TYPE)}>
<Row>
Expand All @@ -203,6 +204,8 @@ type BasicInfoIndexOrManualProps = {
rentType: string;
isSaveClicked: boolean;
leaseAttributes: Attributes;
receivableTypeOptions: Array<Record<string, any>>;
serviceUnitId: number;
usersPermissions: UsersPermissionsType;
yearlyDueDates: Array<DueDate>;
};
Expand All @@ -215,6 +218,8 @@ const BasicInfoIndexOrManual = ({
rentType,
isSaveClicked,
leaseAttributes,
receivableTypeOptions,
serviceUnitId,
usersPermissions,
yearlyDueDates
}: BasicInfoIndexOrManualProps) => {
Expand Down Expand Up @@ -324,17 +329,23 @@ const BasicInfoIndexOrManual = ({
</Authorization>
</Column>
</Row>

{ showOverrideReceivableTypeField(serviceUnitId) &&
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
<Row>
<Column small={12} medium={6} large={4}>
<FormField
disableTouched={isSaveClicked}
fieldAttributes={getFieldAttributes(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}
name="override_receivable_type"
name="override_receivable_type" overrideValues={{
label: LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE,
options: receivableTypeOptions,
}}
/>
</Column>
</Row>
</Authorization>
}
</Fragment>;
};

Expand All @@ -348,7 +359,7 @@ type BasicInfoOneTimeProps = {

const BasicInfoOneTime = ({
isSaveClicked,
leaseAttributes
leaseAttributes,
}: BasicInfoOneTimeProps) => {
return <Fragment>
<Row>
Expand Down Expand Up @@ -381,7 +392,6 @@ const BasicInfoOneTime = ({
</Authorization>
</Column>
</Row>

<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.NOTE)}>
<Row>
<Column>
Expand All @@ -391,17 +401,7 @@ const BasicInfoOneTime = ({
</Column>
</Row>
</Authorization>
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
<Row>
<Column small={12} medium={6} large={4}>
<FormField
disableTouched={isSaveClicked}
fieldAttributes={getFieldAttributes(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}
name="override_receivable_type"
/>
</Column>
</Row>
</Authorization>

</Fragment>;
};

Expand All @@ -411,6 +411,8 @@ type BasicInfoFixedProps = {
field: string;
isSaveClicked: boolean;
leaseAttributes: Attributes;
receivableTypeOptions: Array<Record<string, any>>;
serviceUnitId: number;
usersPermissions: UsersPermissionsType;
yearlyDueDates: Array<DueDate>;
};
Expand All @@ -421,6 +423,8 @@ const BasicInfoFixed = ({
field,
isSaveClicked,
leaseAttributes,
receivableTypeOptions,
serviceUnitId,
usersPermissions,
yearlyDueDates
}: BasicInfoFixedProps) => {
Expand Down Expand Up @@ -496,18 +500,23 @@ const BasicInfoFixed = ({
</Authorization>
</Column>
</Row>

{ showOverrideReceivableTypeField(serviceUnitId) &&
<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}>
<Row>
<Column small={12} medium={6} large={4}>
<FormField
disableTouched={isSaveClicked}
fieldAttributes={getFieldAttributes(leaseAttributes, LeaseRentsFieldPaths.OVERRIDE_RECEIVABLE_TYPE)}
name="override_receivable_type"
name="override_receivable_type" overrideValues={{
label: LeaseRentsFieldTitles.OVERRIDE_RECEIVABLE_TYPE,
options: receivableTypeOptions,
}}
/>
</Column>
</Row>
</Authorization>

}
</Fragment>;
};

Expand Down Expand Up @@ -557,6 +566,24 @@ const BasicInfoFree = ({
</Fragment>;
};

/**
* Get receivable type options for override receivable type select.
* ReceivableTypes must be fetched separately from API, because receivabletype
* choices from leaseAttributes are not filtered by service unit.
*
* @param receivableTypes Receivable types filtered by lease's service unit
* @returns Array Receivabletype options for select element
*/
export const getOverrideReceivableTypeOptions = (receivableTypes: Array<Record<string, any>>) => {
const options = receivableTypes.map((rt) => ({
label: rt.name,
value: rt.id
}));
const sortedOptions = options.sort((a, b) => a.label.localeCompare(b.label));
const emptyItem = { label: "", value: "" };
return [emptyItem, ...sortedOptions];
}

type Props = {
change: (...args: Array<any>) => any;
currentLease: Lease;
Expand All @@ -568,6 +595,7 @@ type Props = {
isSaveClicked: boolean;
leaseAttributes: Attributes;
leaseTypes: LeaseTypeList;
receivableTypes: Array<Record<string, any>>;
rentType: string | null | undefined;
usersPermissions: UsersPermissionsType;
};
Expand All @@ -582,6 +610,7 @@ const BasicInfoEdit = ({
isSaveClicked,
leaseAttributes,
leaseTypes,
receivableTypes,
rentType,
usersPermissions
}: Props) => {
Expand All @@ -591,13 +620,13 @@ const BasicInfoEdit = ({
if (!dueDatesPerYear || !leaseType || dueDatesType !== RentDueDateTypes.FIXED) return [];
return FixedDueDates[rentType === RentTypes.FIXED ? DueDatesPositions.START_OF_MONTH : leaseType.due_dates_position][dueDatesPerYear];
};

const yearlyDueDates = getYearlyDueDates();
const receivableTypeOptions=getOverrideReceivableTypeOptions(receivableTypes);
return <Fragment>
{!rentType && <BasicInfoEmpty isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} />}
{(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && <BasicInfoIndexOrManual cycle={cycle} dueDates={dueDates} dueDatesType={dueDatesType} field={field} rentType={rentType} isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} usersPermissions={usersPermissions} yearlyDueDates={yearlyDueDates} />}
{(rentType === RentTypes.INDEX || rentType === RentTypes.INDEX2022 || rentType === RentTypes.MANUAL) && <BasicInfoIndexOrManual cycle={cycle} dueDates={dueDates} dueDatesType={dueDatesType} field={field} rentType={rentType} isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} usersPermissions={usersPermissions} yearlyDueDates={yearlyDueDates} receivableTypeOptions={receivableTypeOptions} serviceUnitId={currentLease.service_unit.id} />}
{rentType === RentTypes.ONE_TIME && <BasicInfoOneTime dueDates={dueDates} dueDatesType={dueDatesType} isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} usersPermissions={usersPermissions} />}
{rentType === RentTypes.FIXED && <BasicInfoFixed dueDates={dueDates} dueDatesType={dueDatesType} field={field} isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} usersPermissions={usersPermissions} yearlyDueDates={yearlyDueDates} />}
{rentType === RentTypes.FIXED && <BasicInfoFixed dueDates={dueDates} dueDatesType={dueDatesType} field={field} isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} usersPermissions={usersPermissions} yearlyDueDates={yearlyDueDates} receivableTypeOptions={receivableTypeOptions} serviceUnitId={currentLease.service_unit.id} />}
{rentType === RentTypes.FREE && <BasicInfoFree isSaveClicked={isSaveClicked} leaseAttributes={leaseAttributes} />}
</Fragment>;
};
Expand All @@ -617,8 +646,9 @@ export default connect((state, props: BasicInfoEditProps) => {
dueDates: selector(state, `${props.field}.due_dates`),
leaseAttributes: getLeaseAttributes(state),
leaseTypes: getLeaseTypeList(state),
receivableTypes: getReceivableTypes(state),
usersPermissions: getUsersPermissions(state)
};
}, {
change
})(BasicInfoEdit);
})(BasicInfoEdit);
8 changes: 5 additions & 3 deletions src/leases/components/leaseSections/rent/RentItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Props = {
rents: Array<Record<string, any>>;
rentAdjustmentsCollapseState: boolean;
rentCollapseState: boolean;
serviceUnitId: number;
};

const RentItem = ({
Expand All @@ -45,7 +46,8 @@ const RentItem = ({
rent,
rents,
rentAdjustmentsCollapseState,
rentCollapseState
rentCollapseState,
serviceUnitId,
}: Props) => {
const handleCollapseToggle = (key: string, val: boolean) => {
receiveCollapseStates({
Expand Down Expand Up @@ -108,7 +110,7 @@ const RentItem = ({
</Column>} headerTitle={<Authorization allow={isFieldAllowedToRead(leaseAttributes, LeaseRentsFieldPaths.TYPE)}>
{getLabelOfOption(typeOptions, rentType) || '-'}
</Authorization>} onToggle={handleRentCollapseToggle}>
<BasicInfo rent={rent} rentType={rentType} />
<BasicInfo rent={rent} rentType={rentType} serviceUnitId={serviceUnitId} />

<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 @@ -162,4 +164,4 @@ export default connect((state, props) => {
};
}, {
receiveCollapseStates
})(RentItem);
})(RentItem);
Loading

0 comments on commit 182ceae

Please sign in to comment.