diff --git a/src/app/portfolio/[slug]/components/FormSchema.tsx b/src/app/portfolio/[slug]/components/FormSchema.tsx index fbf3a3a8..9e281fb8 100644 --- a/src/app/portfolio/[slug]/components/FormSchema.tsx +++ b/src/app/portfolio/[slug]/components/FormSchema.tsx @@ -8,18 +8,34 @@ export const baseFormSchema = z.object({ export type BaseFormValues = z.infer; -export const RemoteAssessmentFormSchema = baseFormSchema.extend({ - scenario: z.string().min(1), - goal: z.string().min(1), - goalValue: z.string().min(1), - housingType: z.string().min(1), - addressLineOne: z.string().min(1), - postcode: z.string().min(1), - uprn: z.number().min(1, "UPRN must be a valid number"), - valuation: z.number().min(1, "Valuation must be a valid number"), - propertyType: z.string().nullable(), - builtForm: z.string().nullable(), -}); +export const RemoteAssessmentFormSchema = baseFormSchema + .extend({ + scenario: z.string().min(1), + goal: z.string().min(1), + // goalValue: z.string().min(1), + goalValue: z.string().optional(), + budget: z.number().optional(), + housingType: z.string().min(1), + addressLineOne: z.string().min(1), + postcode: z.string().min(1), + uprn: z.number().min(1, "UPRN must be a valid number"), + valuation: z.number().min(1, "Valuation must be a valid number"), + propertyType: z.string().nullable(), + builtForm: z.string().nullable(), + }) + .refine((data) => data.goal !== "Increasing EPC" || !!data.goalValue, { + path: ["goalValue"], + message: "Target EPC Rating is required when goal is Increasing EPC", + }) + .refine( + (data) => + data.goal === "Increasing EPC" || + (typeof data.budget === "number" && data.budget > 0), + { + path: ["budget"], + message: "Budget is required for this goal", + } + ); export type RemoteAssessmentFormValues = z.infer< typeof RemoteAssessmentFormSchema diff --git a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx index e1b5702f..7a375390 100644 --- a/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx +++ b/src/app/portfolio/[slug]/components/RemoteAssessmentModal.tsx @@ -121,7 +121,12 @@ const selectGoalOptions = [ { label: "Reduce energy consumption", value: "Reduce energy consumption", - disabled: true, + disabled: false, + }, + { + label: "Reduce CO2 emissions", + value: "Reduce CO2 emissions", + disabled: false, }, ]; @@ -357,6 +362,11 @@ function useCreateRemoteAssessment({ async function triggerEngine(data: RemoteAssessmentFormValues) { try { + // Goal value should not be missing at this point + if (!data.goalValue) { + throw new Error("Goal value is required"); + } + const triggerBody: EngineTriggerBody = { scenario_id: scenarioId === "__new__" ? null : scenarioId, portfolio_id: portfolioId, @@ -461,10 +471,11 @@ export default function RemoteAssessmentModal({ housingType: "", goal: "", goalValue: "", + budget: undefined, addressLineOne: "", postcode: "", - uprn: undefined, // Must match expected type - valuation: undefined, // Must match expected type + uprn: undefined, + valuation: undefined, propertyType: null, builtForm: null, measures: measuresList, @@ -474,6 +485,7 @@ export default function RemoteAssessmentModal({ const { isValid, isSubmitting } = formState; const measures = form.watch("measures"); + const goal = form.watch("goal"); const { handleSubmit: triggerAssessment, @@ -657,32 +669,63 @@ export default function RemoteAssessmentModal({ )} /> - {/* Goal Value */} - ( - - - Goal Value - - - {selectedScenario === NEW_SENTINEL ? ( - - field.onChange(opt.value) - } - /> - ) : ( - - )} - - - - )} - /> + {goal && + (goal === "Increasing EPC" ? ( + ( + + + Target EPC Rating + + + {selectedScenario === NEW_SENTINEL ? ( + + field.onChange(opt.value) + } + /> + ) : ( + + )} + + + + )} + /> + ) : ( + ( + + + Budget (£) + + + + field.onChange( + e.target.value === "" + ? undefined + : Number(e.target.value) + ) + } + className="border-brandbrown focus-visible:ring-brandbrown focus-visible:border-brandbrown" + /> + + + + )} + /> + ))} )}