Skip to content

Commit

Permalink
feat(web): display previously entered data in photovoltaic project si…
Browse files Browse the repository at this point in the history
…te resale forms
  • Loading branch information
stephane-ruhlmann committed Jan 16, 2025
1 parent ce7ef94 commit e9436fb
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export type PhotovoltaicProjectCreationStep =
| "CREATION_RESULT";

export const INITIAL_STATE: RenewableEneryProjectState = {
stepsHistory: ["RENEWABLE_ENERGY_TYPES"],
stepsHistory: ["STAKEHOLDERS_SITE_PURCHASE"],
creationData: {
yearlyProjectedExpenses: [],
yearlyProjectedRevenues: [],
Expand Down Expand Up @@ -353,10 +353,8 @@ const addCompleteStepActionCases = (builder: ActionReducerMapBuilder<ProjectCrea
builder.addCase(completeSitePurchaseAmounts, (state, action) => {
state.renewableEnergyProject.creationData.sitePurchaseSellingPrice =
action.payload.sellingPrice;
if (action.payload.propertyTransferDuties) {
state.renewableEnergyProject.creationData.sitePurchasePropertyTransferDuties =
action.payload.propertyTransferDuties;
}
state.renewableEnergyProject.creationData.sitePurchasePropertyTransferDuties =
action.payload.propertyTransferDuties ?? 0;
if (state.siteData?.isFriche) {
state.renewableEnergyProject.stepsHistory.push("EXPENSES_REINSTATEMENT");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ export const selectContaminatedSurfaceAreaPercentageToDecontaminate = createSele
},
);

type SitePurchaseAmounts = {
sellingPrice: number;
propertyTransferDuties: number;
};
export const selectSitePurchaseAmounts = createSelector(
[selectCreationData],
(creationData): SitePurchaseAmounts | undefined => {
if (!creationData.sitePurchaseSellingPrice) return undefined;
return {
sellingPrice: creationData.sitePurchaseSellingPrice ?? 0,
propertyTransferDuties: creationData.sitePurchasePropertyTransferDuties ?? 0,
};
},
);

export const getDefaultValuesForYearlyProjectedExpenses = createSelector(
selectCreationData,
(creationData): { rent: number; maintenance: number; taxes: number } | undefined => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { computePropertyTransferDutiesFromSellingPrice } from "shared";

Expand All @@ -21,23 +20,10 @@ export type FormValues = {
};

const SitePurchaseAmountsForm = ({ initialValues, onSubmit, onBack }: Props) => {
const { handleSubmit, register, watch, setValue, formState } = useForm<FormValues>({
const { handleSubmit, register, watch, setValue } = useForm<FormValues>({
defaultValues: initialValues,
});

const sellingPrice = watch("sellingPrice");

useEffect(() => {
// we don't want to override the default value is form has not been touched
if (!formState.isDirty && !!initialValues) return;

const propertyTransferDuties =
sellingPrice && !isNaN(sellingPrice)
? computePropertyTransferDutiesFromSellingPrice(sellingPrice)
: undefined;
setValue("propertyTransferDuties", propertyTransferDuties);
}, [sellingPrice, setValue, formState.isDirty, initialValues]);

return (
<WizardFormLayout
title="Montant de l'acquisition foncière"
Expand Down Expand Up @@ -80,7 +66,22 @@ const SitePurchaseAmountsForm = ({ initialValues, onSubmit, onBack }: Props) =>
className="!tw-pt-4"
addonText="€"
label="Prix de vente"
nativeInputProps={register("sellingPrice", optionalNumericFieldRegisterOptions)}
nativeInputProps={(() => {
const registerProps = register("sellingPrice", optionalNumericFieldRegisterOptions);
return {
...registerProps,
onChange: (e) => {
void registerProps.onChange(e);

const sellingPrice = parseFloat(e.target.value);
const propertyTransferDuties =
sellingPrice && !isNaN(sellingPrice)
? computePropertyTransferDutiesFromSellingPrice(sellingPrice)
: undefined;
setValue("propertyTransferDuties", propertyTransferDuties);
},
};
})()}
/>

<RowDecimalsNumericInput
Expand All @@ -90,7 +91,10 @@ const SitePurchaseAmountsForm = ({ initialValues, onSubmit, onBack }: Props) =>
nativeInputProps={register("propertyTransferDuties", optionalNumericFieldRegisterOptions)}
/>

<BackNextButtonsGroup onBack={onBack} nextLabel={!sellingPrice ? "Passer" : "Valider"} />
<BackNextButtonsGroup
onBack={onBack}
nextLabel={!watch("sellingPrice") ? "Passer" : "Valider"}
/>
</form>
</WizardFormLayout>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@ import {
completeSitePurchaseAmounts,
revertSitePurchaseAmounts,
} from "@/features/create-project/core/renewable-energy/actions/renewableEnergy.actions";
import { AppDispatch } from "@/shared/core/store-config/store";
import { useAppDispatch } from "@/shared/views/hooks/store.hooks";
import { selectSitePurchaseAmounts } from "@/features/create-project/core/renewable-energy/selectors/renewableEnergy.selector";
import { useAppDispatch, useAppSelector } from "@/shared/views/hooks/store.hooks";

import SitePurchaseAmountsForm, {
FormValues,
} from "../../../common-views/costs/site-purchase-amounts/SitePurchaseAmountsForm";

const mapProps = (dispatch: AppDispatch) => {
return {
onSubmit: (data: FormValues) => {
dispatch(
completeSitePurchaseAmounts({
sellingPrice: data.sellingPrice ?? 0,
propertyTransferDuties: data.propertyTransferDuties,
}),
);
},
onBack: () => {
dispatch(revertSitePurchaseAmounts());
},
};
};

function SitePurchaseAmountsContainer() {
const dispatch = useAppDispatch();
return <SitePurchaseAmountsForm {...mapProps(dispatch)} />;
const initialValues = useAppSelector(selectSitePurchaseAmounts);

return (
<SitePurchaseAmountsForm
initialValues={initialValues}
onSubmit={(data: FormValues) => {
dispatch(
completeSitePurchaseAmounts({
sellingPrice: data.sellingPrice ?? 0,
propertyTransferDuties: data.propertyTransferDuties,
}),
);
}}
onBack={() => {
dispatch(revertSitePurchaseAmounts());
}}
/>
);
}

export default SitePurchaseAmountsContainer;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import RadioButtons from "@/shared/views/components/RadioButtons/RadioButtons";
import WizardFormLayout from "@/shared/views/layout/WizardFormLayout/WizardFormLayout";

type Props = {
initialValues?: FormValues;
currentOwnerName?: string;
onSubmit: (data: FormValues) => void;
onBack: () => void;
Expand All @@ -14,8 +15,10 @@ export type FormValues = {
willSiteBePurchased: "yes" | "no" | null;
};

function SitePurchasedForm({ onSubmit, onBack, currentOwnerName }: Props) {
const { register, handleSubmit, formState, watch } = useForm<FormValues>();
function SitePurchasedForm({ initialValues, onSubmit, onBack, currentOwnerName }: Props) {
const { register, handleSubmit, formState, watch } = useForm<FormValues>({
defaultValues: initialValues,
});

return (
<WizardFormLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import SitePurchasedForm, { FormValues } from "./SitePurchasedForm";
function SitePurchasedFormContainer() {
const dispatch = useAppDispatch();
const siteOwner = useAppSelector((state) => state.projectCreation.siteData?.owner);
const initialValue = useAppSelector(
(state) => state.projectCreation.renewableEnergyProject.creationData.willSiteBePurchased,
);

const onSubmit = (data: FormValues) => {
const willSiteBePurchased = data.willSiteBePurchased === "yes";
Expand All @@ -20,7 +23,16 @@ function SitePurchasedFormContainer() {
};

return (
<SitePurchasedForm onSubmit={onSubmit} onBack={onBack} currentOwnerName={siteOwner?.name} />
<SitePurchasedForm
initialValues={
initialValue === undefined
? undefined
: { willSiteBePurchased: initialValue ? "yes" : "no" }
}
onSubmit={onSubmit}
onBack={onBack}
currentOwnerName={siteOwner?.name}
/>
);
}

Expand Down

0 comments on commit e9436fb

Please sign in to comment.