Skip to content

Commit

Permalink
editor validation plus api gis query params
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicamcinchak committed Nov 25, 2024
1 parent 4147b44 commit 65e699e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 89 deletions.
34 changes: 25 additions & 9 deletions api.planx.uk/modules/gis/service/digitalLand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,30 @@ async function go(
geom: string,
extras: Record<string, string>,
): Promise<GISResponse> {
// 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 = {
Expand Down Expand Up @@ -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] = {
Expand Down
166 changes: 86 additions & 80 deletions editor.planx.uk/src/@planx/components/PlanningConstraints/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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";
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";

Expand All @@ -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<PlanningConstraints> = {};
if (!values.dataValues || values.dataValues?.length < 1) {
errors.dataValues = "Select at least one constraint";
}
return errors;
},
});

Expand All @@ -50,7 +55,6 @@ function PlanningConstraintsComponent(props: Props) {
} else {
newCheckedVals = [];
}

formik.setFieldValue("dataValues", newCheckedVals);
};

Expand Down Expand Up @@ -114,82 +118,84 @@ function PlanningConstraintsComponent(props: Props) {
</InputRow>
</InputGroup>
<InputGroup label="Which constraints do you want to check?">
<TableContainer>
<Table size="small">
<TableHead>
<TableRow>
<TableCell>
<Checkbox
color="primary"
checked={
availableDatasets.length ===
formik.values.dataValues?.length
}
onChange={changeSelectAll(formik.values.dataValues)}
inputProps={{
"aria-label": "select all constraints",
}}
/>
</TableCell>
<TableCell sx={{ fontWeight: FONT_WEIGHT_BOLD }}>
Constraints
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{availableDatasets
.sort((a, b) => a.val.localeCompare(b.val))
.map((dataset, i: number) => (
<TableRow key={i}>
<TableCell sx={{ verticalAlign: "top" }}>
<Checkbox
color="primary"
checked={formik.values.dataValues?.includes(
dataset.val,
)}
onChange={changeDataset(dataset.val)}
/>
</TableCell>
<TableCell>
<InputGroup>
<InputRow>
<Input
format="large"
name="text"
value={dataset.text}
disabled
/>
</InputRow>
<InputRow>
<Input
format="data"
name="val"
value={dataset.val}
disabled
/>
</InputRow>
</InputGroup>
<Box>
<Typography
variant="body2"
color="GrayText"
sx={{ fontSize: ".8em", pb: 1 }}
>
{`via ${dataset.source}: ${dataset.datasets
.filter(Boolean)
.join(", ")} ${
dataset?.entity
? `(entity ${dataset.entity})`
: ``
}`}
</Typography>
</Box>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<ErrorWrapper error={formik.errors.dataValues}>
<TableContainer>
<Table size="small">
<TableHead>
<TableRow>
<TableCell>
<Checkbox
color="primary"
checked={
availableDatasets.length ===
formik.values.dataValues?.length
}
onChange={changeSelectAll(formik.values.dataValues)}
inputProps={{
"aria-label": "select all constraints",
}}
/>
</TableCell>
<TableCell sx={{ fontWeight: FONT_WEIGHT_BOLD }}>
Constraints
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{availableDatasets
.sort((a, b) => a.val.localeCompare(b.val))
.map((dataset, i: number) => (
<TableRow key={i}>
<TableCell sx={{ verticalAlign: "top" }}>
<Checkbox
color="primary"
checked={formik.values.dataValues?.includes(
dataset.val,
)}
onChange={changeDataset(dataset.val)}
/>
</TableCell>
<TableCell>
<InputGroup>
<InputRow>
<Input
format="large"
name="text"
value={dataset.text}
disabled
/>
</InputRow>
<InputRow>
<Input
format="data"
name="val"
value={dataset.val}
disabled
/>
</InputRow>
</InputGroup>
<Box>
<Typography
variant="body2"
color="GrayText"
sx={{ fontSize: ".8em", pb: 1 }}
>
{`via ${dataset.source}: ${dataset.datasets
.filter(Boolean)
.join(", ")} ${
dataset?.entity
? `(entity ${dataset.entity})`
: ``
}`}
</Typography>
</Box>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</ErrorWrapper>
</InputGroup>
<InputGroup label="Planning conditions disclaimer">
<InputRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function Component(props: Props) {
siteBoundary && stringify(siteBoundary);
const planningDataParams: Record<string, string> = {
geom: wktPolygon || wktPoint,
vals: dataValues.join(","),
...params,
};

Expand Down

0 comments on commit 65e699e

Please sign in to comment.