-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): display previously entered data in urban project installat…
…ion expenses form
- Loading branch information
1 parent
bfa2492
commit edcfe4e
Showing
8 changed files
with
153 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../create-project/views/urban-project/custom-forms/costs/installation-costs/mappers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ComputedInstallationCosts, UrbanProjectDevelopmentExpense } from "shared"; | ||
|
||
import { mapFormValuesToReinstatementExpenses } from "@/features/create-project/views/common-views/costs/reinstatement-costs/mappers"; | ||
|
||
import { mapFormValuesToExpenses, mapInitialValues } from "./mappers"; | ||
|
||
describe("Urban project installation costs form mappers", () => { | ||
describe("mapFormValuesToExpenses", () => { | ||
it("should return an empty when form values are empty", () => { | ||
const result = mapFormValuesToReinstatementExpenses({}); | ||
expect(result).toEqual([]); | ||
}); | ||
it("should filter out undefined and zero values", () => { | ||
const result = mapFormValuesToExpenses({ | ||
technicalStudyAmount: 0, | ||
worksAmount: undefined, | ||
otherAmount: 50, | ||
}); | ||
expect(result).toEqual([{ purpose: "other", amount: 50 }]); | ||
}); | ||
}); | ||
describe("mapInitialValues", () => { | ||
it("should return mapped given pre-entered data over default values", () => { | ||
const preEnteredData: UrbanProjectDevelopmentExpense[] = [ | ||
{ purpose: "technical_studies", amount: 100 }, | ||
{ purpose: "development_works", amount: 200 }, | ||
]; | ||
const defaultValues: ComputedInstallationCosts = { | ||
technicalStudies: 430, | ||
developmentWorks: 540, | ||
other: 50, | ||
}; | ||
const result = mapInitialValues(preEnteredData, defaultValues); | ||
expect(result).toEqual({ | ||
technicalStudyAmount: 100, | ||
worksAmount: 200, | ||
}); | ||
}); | ||
|
||
it("should return mapped given default values when no pre-entered data", () => { | ||
const defaultValues: ComputedInstallationCosts = { | ||
technicalStudies: 430, | ||
developmentWorks: 540, | ||
other: 50, | ||
}; | ||
const result = mapInitialValues(undefined, defaultValues); | ||
expect(result).toEqual({ | ||
technicalStudyAmount: 430, | ||
worksAmount: 540, | ||
otherAmount: 50, | ||
}); | ||
}); | ||
it("should return undefined when no pre-entered data nor default values", () => { | ||
const result = mapInitialValues(undefined, undefined); | ||
expect(result).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
...tures/create-project/views/urban-project/custom-forms/costs/installation-costs/mappers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { | ||
ComputedInstallationCosts, | ||
typedObjectEntries, | ||
UrbanProjectDevelopmentExpense, | ||
} from "shared"; | ||
|
||
import { FormValues } from "@/features/create-project/views/common-views/costs/installation-costs/InstallationCostsForm"; | ||
|
||
const purposeMapKeys = { | ||
technicalStudyAmount: "technical_studies", | ||
worksAmount: "development_works", | ||
otherAmount: "other", | ||
} as const satisfies Record<keyof FormValues, UrbanProjectDevelopmentExpense["purpose"]>; | ||
|
||
const expensesToFormValuesMap = { | ||
technical_studies: "technicalStudyAmount", | ||
development_works: "worksAmount", | ||
other: "otherAmount", | ||
} as const satisfies Record<UrbanProjectDevelopmentExpense["purpose"], keyof FormValues>; | ||
|
||
export const mapFormValuesToExpenses = ( | ||
formValues: FormValues, | ||
): UrbanProjectDevelopmentExpense[] => { | ||
return typedObjectEntries(formValues) | ||
.filter(([, amount]) => amount && amount > 0) | ||
.map(([purpose, amount]) => ({ | ||
amount: amount as number, | ||
purpose: purposeMapKeys[purpose], | ||
})); | ||
}; | ||
|
||
const mapExpensesToFormValues = (expenses: UrbanProjectDevelopmentExpense[]): FormValues => { | ||
return expenses.reduce<FormValues>((acc, cur) => { | ||
return { ...acc, [expensesToFormValuesMap[cur.purpose]]: cur.amount }; | ||
}, {}); | ||
}; | ||
|
||
const mapDefaultValuesToFormValues = (defaultValues: ComputedInstallationCosts): FormValues => { | ||
const { developmentWorks, technicalStudies, other } = defaultValues; | ||
return { | ||
worksAmount: developmentWorks && Math.round(developmentWorks), | ||
technicalStudyAmount: technicalStudies && Math.round(technicalStudies), | ||
otherAmount: other && Math.round(other), | ||
}; | ||
}; | ||
|
||
export const mapInitialValues = ( | ||
preEnteredData: UrbanProjectDevelopmentExpense[] | undefined, | ||
defaultValues: ComputedInstallationCosts | undefined, | ||
): FormValues | undefined => { | ||
if (preEnteredData) return mapExpensesToFormValues(preEnteredData); | ||
|
||
if (defaultValues) return mapDefaultValuesToFormValues(defaultValues); | ||
|
||
return undefined; | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/shared/src/reconversion-projects/urban-project/installationCosts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters