-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Team Settings Form Fetching Boundary GeoJSON using Boundary URL #3378
Changes from 10 commits
8b6ff65
0612245
8744aa6
b75b3d5
34e5744
a3195e5
6a1a0e9
b0cf22a
c4f1e3d
21cfa53
74e7089
7c1d9fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
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 { useStore } from "pages/FlowEditor/lib/store"; | ||
import React, { ChangeEvent } from "react"; | ||
|
@@ -9,10 +13,14 @@ import { SettingsForm } from "../shared/SettingsForm"; | |
import { FormProps } from "."; | ||
|
||
export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) { | ||
const planningDataURLRegex = | ||
/^https:\/\/www\.planning\.data\.gov\.uk\/entity\/\d{1,7}$/; | ||
|
||
const formSchema = Yup.object().shape({ | ||
boundaryUrl: Yup.string() | ||
.url( | ||
"Enter a boundary URL in the correct format, https://www.planning.data.gov.uk/", | ||
.matches( | ||
planningDataURLRegex, | ||
"Enter a boundary URL in the correct format, https://www.planning.data.gov.uk/entity/1234567", | ||
) | ||
.required("Enter a boundary URL"), | ||
}); | ||
|
@@ -21,12 +29,23 @@ export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) { | |
...formikConfig, | ||
validationSchema: formSchema, | ||
onSubmit: async (values, { resetForm }) => { | ||
const isSuccess = await useStore.getState().updateTeamSettings({ | ||
boundaryUrl: values.boundaryUrl, | ||
}); | ||
if (isSuccess) { | ||
onSuccess(); | ||
resetForm({ values }); | ||
try { | ||
const { data } = await axios.get(`${values.boundaryUrl}.geojson`); | ||
const bboxPoly = bboxPolygon(bbox(data)); | ||
const bboxFeature = feature(bboxPoly.geometry); | ||
|
||
const isUpdateSuccess = await useStore.getState().updateTeamSettings({ | ||
boundaryUrl: values.boundaryUrl, | ||
boundaryBbox: bboxFeature, | ||
}); | ||
if (isUpdateSuccess) { | ||
onSuccess(); | ||
resetForm({ values }); | ||
} | ||
} catch (error) { | ||
formik.errors.boundaryUrl = | ||
"We are unable to retrieve your boundary, check your boundary URL and try again"; | ||
console.error(error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good! We might want slightly more fine-grained checking of the error type (i.e. using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
}, | ||
}); | ||
|
@@ -49,7 +68,7 @@ export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) { | |
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
https://www.planning.data.gov.uk/ | ||
https://www.planning.data.gov.uk/entity/1234567 | ||
</a> | ||
</p> | ||
</> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) { | |
.email( | ||
"Enter an email address in the correct format, like [email protected]", | ||
) | ||
.required("Enter a help email address"), | ||
helpPhone: Yup.string().required("Enter a help phone number"), | ||
.required("Enter a contact email address"), | ||
helpPhone: Yup.string().required("Enter a phone number"), | ||
helpOpeningHours: Yup.string().required("Enter your opening hours"), | ||
homepage: Yup.string() | ||
.url( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure if this is the simplest way to achieve this. If we add types and maybe wrap this in a descriptive helper function it may help clear this up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed here - 74e7089
The confusion may have come from the fact that there's a
bbox
property on the generated GeoJSON feature, which sort of looks wrong if you're not expecting it. This is actually fine and is an optional part of the GeoJSON spec (docs).