From 65e699ec6c826172ac781b59deb0d816b8f97d3f Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Mon, 25 Nov 2024 15:50:38 +0100 Subject: [PATCH] editor validation plus api gis query params --- .../modules/gis/service/digitalLand.ts | 34 +++- .../components/PlanningConstraints/Editor.tsx | 166 +++++++++--------- .../components/PlanningConstraints/Public.tsx | 1 + 3 files changed, 112 insertions(+), 89 deletions(-) diff --git a/api.planx.uk/modules/gis/service/digitalLand.ts b/api.planx.uk/modules/gis/service/digitalLand.ts index d96115c4c4..f1a715d143 100644 --- a/api.planx.uk/modules/gis/service/digitalLand.ts +++ b/api.planx.uk/modules/gis/service/digitalLand.ts @@ -71,15 +71,30 @@ async function go( geom: string, extras: Record, ): Promise { - // generate list of digital land datasets we should query based on 'active' planx schema variables + // generate list of digital land datasets we should query and their associated passport values const activeDatasets: string[] = []; - Object.keys(baseSchema).forEach((key) => { - if (baseSchema[key]["active"]) { - baseSchema[key]["digital-land-datasets"]?.forEach((dataset: string) => { - activeDatasets.push(dataset); - }); - } - }); + let activeDataValues: string[] = []; + if (extras?.vals) { + // if the editor selected constraints, prioritise their selection + activeDataValues = extras.vals.split(","); + Object.keys(baseSchema).forEach((key) => { + if (activeDataValues.includes(key)) { + baseSchema[key]["digital-land-datasets"]?.forEach((dataset: string) => { + activeDatasets.push(dataset); + }); + } + }); + } else { + // else default to the internally maintained list of all "active" datasets + Object.keys(baseSchema).forEach((key) => { + if (baseSchema[key]["active"]) { + activeDataValues.push(key); + baseSchema[key]["digital-land-datasets"]?.forEach((dataset: string) => { + activeDatasets.push(dataset); + }); + } + }); + } // set up request query params per https://www.planning.data.gov.uk/docs const options = { @@ -159,7 +174,8 @@ async function go( // TODO followup with digital land about how to return 'nots' via API (currently assumes any "active" metadata was successfully queried) const nots = Object.keys(baseSchema).filter( (key) => - baseSchema[key]["active"] && !Object.keys(formattedResult).includes(key), + activeDataValues.includes(key) && + !Object.keys(formattedResult).includes(key), ); nots.forEach((not) => { formattedResult[not] = { diff --git a/editor.planx.uk/src/@planx/components/PlanningConstraints/Editor.tsx b/editor.planx.uk/src/@planx/components/PlanningConstraints/Editor.tsx index 6f1215cdfe..3ba28fa76b 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/Editor.tsx +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/Editor.tsx @@ -9,7 +9,7 @@ import TableRow from "@mui/material/TableRow"; import Typography from "@mui/material/Typography"; import { ComponentType as TYPES } from "@opensystemslab/planx-core/types"; import { EditorProps } from "@planx/components/shared/types"; -import { useFormik } from "formik"; +import { FormikErrors, useFormik } from "formik"; import React, { ChangeEvent } from "react"; import { FONT_WEIGHT_BOLD } from "theme"; import InputGroup from "ui/editor/InputGroup"; @@ -17,6 +17,7 @@ import { ModalFooter } from "ui/editor/ModalFooter"; import ModalSection from "ui/editor/ModalSection"; import ModalSectionContent from "ui/editor/ModalSectionContent"; import RichTextInput from "ui/editor/RichTextInput/RichTextInput"; +import ErrorWrapper from "ui/shared/ErrorWrapper"; import Input from "ui/shared/Input/Input"; import InputRow from "ui/shared/InputRow"; @@ -36,8 +37,12 @@ function PlanningConstraintsComponent(props: Props) { data: newValues, }); }, - validate: () => { - // TODO must select at least one formik.values.dataValues + validate: (values) => { + const errors: FormikErrors = {}; + if (!values.dataValues || values.dataValues?.length < 1) { + errors.dataValues = "Select at least one constraint"; + } + return errors; }, }); @@ -50,7 +55,6 @@ function PlanningConstraintsComponent(props: Props) { } else { newCheckedVals = []; } - formik.setFieldValue("dataValues", newCheckedVals); }; @@ -114,82 +118,84 @@ function PlanningConstraintsComponent(props: Props) { - - - - - - - - - Constraints - - - - - {availableDatasets - .sort((a, b) => a.val.localeCompare(b.val)) - .map((dataset, i: number) => ( - - - - - - - - - - - - - - - - {`via ${dataset.source}: ${dataset.datasets - .filter(Boolean) - .join(", ")} ${ - dataset?.entity - ? `(entity ${dataset.entity})` - : `` - }`} - - - - - ))} - -
-
+ + + + + + + + + + Constraints + + + + + {availableDatasets + .sort((a, b) => a.val.localeCompare(b.val)) + .map((dataset, i: number) => ( + + + + + + + + + + + + + + + + {`via ${dataset.source}: ${dataset.datasets + .filter(Boolean) + .join(", ")} ${ + dataset?.entity + ? `(entity ${dataset.entity})` + : `` + }`} + + + + + ))} + +
+
+
diff --git a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.tsx b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.tsx index bd855f6f8a..209cd1e93c 100644 --- a/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.tsx +++ b/editor.planx.uk/src/@planx/components/PlanningConstraints/Public.tsx @@ -83,6 +83,7 @@ function Component(props: Props) { siteBoundary && stringify(siteBoundary); const planningDataParams: Record = { geom: wktPolygon || wktPoint, + vals: dataValues.join(","), ...params, };