diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Settings/GeneralSettings/BoundaryForm.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Settings/GeneralSettings/BoundaryForm.tsx index fd10862a99..7e8bf9837d 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Settings/GeneralSettings/BoundaryForm.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Settings/GeneralSettings/BoundaryForm.tsx @@ -1,8 +1,8 @@ import { bbox } from "@turf/bbox"; import { bboxPolygon } from "@turf/bbox-polygon"; -import { feature } from "@turf/helpers"; import axios from "axios"; import { useFormik } from "formik"; +import type { Feature, MultiPolygon,Polygon } from "geojson"; import { useStore } from "pages/FlowEditor/lib/store"; import React, { ChangeEvent } from "react"; import InputLabel from "ui/editor/InputLabel"; @@ -12,6 +12,17 @@ import * as Yup from "yup"; import { SettingsForm } from "../shared/SettingsForm"; import { FormProps } from "."; +export type PlanningDataEntity = Feature< + Polygon | MultiPolygon, + Record +>; + +/** + * Convert a complex local authority boundary to a simplified bounding box + */ +const convertToBoundingBox = (feature: PlanningDataEntity): Feature => + bboxPolygon(bbox(feature)); + export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) { const planningDataURLRegex = /^https:\/\/www\.planning\.data\.gov\.uk\/entity\/\d{1,7}$/; @@ -30,13 +41,13 @@ export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) { validationSchema: formSchema, onSubmit: async (values, { resetForm }) => { try { - const { data } = await axios.get(`${values.boundaryUrl}.geojson`); - const bboxPoly = bboxPolygon(bbox(data)); - const bboxFeature = feature(bboxPoly.geometry); + const { data } = await axios.get( + `${values.boundaryUrl}.geojson`, + ); const isUpdateSuccess = await useStore.getState().updateTeamSettings({ boundaryUrl: values.boundaryUrl, - boundaryBbox: bboxFeature, + boundaryBbox: convertToBoundingBox(data), }); if (isUpdateSuccess) { onSuccess();