Skip to content

Commit 3f34641

Browse files
committed
fe: fix projectSpecificLossRate field
1 parent 68e5edd commit 3f34641

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

client/src/containers/projects/form/setup/conservation-project-details/loss-rate.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default function LossRate() {
9292
onValueChange={async (v) =>
9393
handleFormChange("parameters.projectSpecificLossRate", v)
9494
}
95+
isPercentage
9596
/>
9697
)}
9798
/>

shared/schemas/custom-projects/create-custom-project.schema.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { z } from "zod";
22
import { ECOSYSTEM } from "@shared/entities/ecosystem.enum";
3-
import { ACTIVITY, RESTORATION_ACTIVITY_SUBTYPE } from "@shared/entities/activity.enum";
3+
import {
4+
ACTIVITY,
5+
RESTORATION_ACTIVITY_SUBTYPE,
6+
} from "@shared/entities/activity.enum";
47
import { EMISSION_FACTORS_TIER_TYPES } from "@shared/entities/carbon-inputs/emission-factors.entity";
5-
import { CARBON_REVENUES_TO_COVER, PROJECT_SPECIFIC_EMISSION } from "@shared/entities/custom-project.entity";
8+
import {
9+
CARBON_REVENUES_TO_COVER,
10+
PROJECT_SPECIFIC_EMISSION,
11+
} from "@shared/entities/custom-project.entity";
612
import { SEQUESTRATION_RATE_TIER_TYPES } from "@shared/entities/carbon-inputs/sequestration-rate.entity";
713
import { isNumber } from "lodash";
814

@@ -23,19 +29,17 @@ export const ConservationCustomProjectSchema = z.object({
2329
emissionFactorUsed: z.nativeEnum(EMISSION_FACTORS_TIER_TYPES),
2430
projectSpecificEmission: z.nativeEnum(PROJECT_SPECIFIC_EMISSION),
2531
projectSpecificLossRate: z.preprocess(
26-
(value) =>
27-
value === undefined || value === null ? undefined : parseNumber(value),
28-
z
29-
.number({
30-
required_error: "Project Specific Loss Rate is required",
31-
invalid_type_error: "Project Specific Loss Rate should be a number",
32-
})
33-
.refine(
34-
(val) => val >= -1 && val <= 0,
35-
{
36-
message: "Project Specific Loss Rate must be between -100% and 0%",
37-
}
38-
).optional(),
32+
(value) =>
33+
value === undefined || value === null ? undefined : parseNumber(value),
34+
z
35+
.number({
36+
required_error: "Project Specific Loss Rate is required",
37+
invalid_type_error: "Project Specific Loss Rate should be a number",
38+
})
39+
.refine((val) => val >= -1 && val <= 0, {
40+
message: "Project Specific Loss Rate must be between -100% and 0%",
41+
})
42+
.optional(),
3943
),
4044
projectSpecificEmissionFactor: z
4145
.number({
@@ -60,46 +64,47 @@ export const ConservationCustomProjectSchema = z.object({
6064
.optional(),
6165
});
6266

63-
export const ConservationCustomProjectSchemaFE = ConservationCustomProjectSchema.extend({
64-
projectSpecificLossRate: z.preprocess(
65-
(value) =>
66-
value === undefined || value === null
67-
? undefined
68-
: parseNumber(value),
69-
z
70-
.number({
71-
required_error: "Project Specific Loss Rate is required",
72-
invalid_type_error: "Project Specific Loss Rate should be a number",
73-
})
74-
.refine((val) => val >= -100 && val <= 0, {
75-
message: "Project Specific Loss Rate must be between -100 and 0",
76-
})
77-
.transform((val) => val / 100)
78-
.optional(),
79-
),
80-
});
67+
export const ConservationCustomProjectSchemaFE =
68+
ConservationCustomProjectSchema.extend({
69+
projectSpecificLossRate: z.preprocess(
70+
(value) =>
71+
value === undefined || value === null ? undefined : parseNumber(value),
72+
z
73+
.number({
74+
required_error: "Project Specific Loss Rate is required",
75+
invalid_type_error: "Project Specific Loss Rate should be a number",
76+
})
77+
.refine((val) => val >= -100 && val <= 0, {
78+
message: "Project Specific Loss Rate must be between -100 and 0",
79+
})
80+
.optional(),
81+
),
82+
});
8183

8284
export const RestorationPlanDTOSchema = z
8385
.array(
8486
z.object({
8587
year: z
8688
.preprocess(
8789
parseNumber,
88-
z.number({
89-
required_error: "Year should be a number",
90-
invalid_type_error: "Year must be a number",
91-
})
90+
z
91+
.number({
92+
required_error: "Year should be a number",
93+
invalid_type_error: "Year must be a number",
94+
})
9295
.int("Year must be an integer"),
9396
)
9497
.optional(),
9598

9699
annualHectaresRestored: z
97100
.preprocess(
98101
parseNumber,
99-
z.number({
100-
required_error: "Annual hectares restored should be a number",
101-
invalid_type_error: "Annual hectares restored must be a number",
102-
}).nonnegative("Annual hectares restored cannot be negative"),
102+
z
103+
.number({
104+
required_error: "Annual hectares restored should be a number",
105+
invalid_type_error: "Annual hectares restored must be a number",
106+
})
107+
.nonnegative("Annual hectares restored cannot be negative"),
103108
)
104109
.optional(),
105110
}),
@@ -265,7 +270,10 @@ export const ValidateConservationSchema = (
265270
typeof ConservationCustomProjectSchema
266271
>;
267272
if (params.lossRateUsed === LOSS_RATE_USED.PROJECT_SPECIFIC) {
268-
if (params.projectSpecificLossRate === undefined || params.projectSpecificLossRate === null) {
273+
if (
274+
params.projectSpecificLossRate === undefined ||
275+
params.projectSpecificLossRate === null
276+
) {
269277
ctx.addIssue({
270278
code: z.ZodIssueCode.custom,
271279
message: "Project Specific Loss Rate must be between -100% and 0%",
@@ -294,7 +302,7 @@ export const ValidateConservationSchema = (
294302
} else if (params.emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_3) {
295303
if (
296304
params.projectSpecificEmission ===
297-
PROJECT_SPECIFIC_EMISSION.ONE_EMISSION_FACTOR &&
305+
PROJECT_SPECIFIC_EMISSION.ONE_EMISSION_FACTOR &&
298306
!params.projectSpecificEmissionFactor
299307
) {
300308
ctx.addIssue({

0 commit comments

Comments
 (0)