Skip to content

Commit

Permalink
chore: Add types, use helper function for turf
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Jul 8, 2024
1 parent 21cfa53 commit 74e7089
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,6 +12,17 @@ import * as Yup from "yup";
import { SettingsForm } from "../shared/SettingsForm";
import { FormProps } from ".";

export type PlanningDataEntity = Feature<
Polygon | MultiPolygon,
Record<string, unknown>
>;

/**
* Convert a complex local authority boundary to a simplified bounding box
*/
const convertToBoundingBox = (feature: PlanningDataEntity): Feature<Polygon> =>
bboxPolygon(bbox(feature));

export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) {
const planningDataURLRegex =
/^https:\/\/www\.planning\.data\.gov\.uk\/entity\/\d{1,7}$/;
Expand All @@ -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<PlanningDataEntity>(
`${values.boundaryUrl}.geojson`,
);

const isUpdateSuccess = await useStore.getState().updateTeamSettings({
boundaryUrl: values.boundaryUrl,
boundaryBbox: bboxFeature,
boundaryBbox: convertToBoundingBox(data),
});
if (isUpdateSuccess) {
onSuccess();
Expand Down

0 comments on commit 74e7089

Please sign in to comment.