Skip to content
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

fix: Move "invalid graph" warning to MapFieldInput #3689

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 4 additions & 40 deletions editor.planx.uk/src/@planx/components/List/Public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableRow from "@mui/material/TableRow";
import Typography from "@mui/material/Typography";
import { SiteAddress } from "@planx/components/FindProperty/model";
import { ErrorSummaryContainer } from "@planx/components/shared/Preview/ErrorSummaryContainer";
import { SchemaFields } from "@planx/components/shared/Schema/SchemaFields";
import { PublicProps } from "@planx/components/ui";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect, useRef } from "react";
import { FONT_WEIGHT_SEMI_BOLD } from "theme";
import FullWidthWrapper from "ui/public/FullWidthWrapper";
Expand Down Expand Up @@ -56,14 +53,8 @@ const InactiveListCardLayout = styled(Box)(({ theme }) => ({
const ActiveListCard: React.FC<{
index: number;
}> = ({ index: i }) => {
const {
schema,
saveItem,
cancelEditItem,
errors,
formik,
activeIndex,
} = useListContext();
const { schema, saveItem, cancelEditItem, errors, formik, activeIndex } =
useListContext();

const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
Expand Down Expand Up @@ -119,8 +110,7 @@ const ActiveListCard: React.FC<{
const InactiveListCard: React.FC<{
index: number;
}> = ({ index: i }) => {
const { schema, formik, removeItem, editItem } =
useListContext();
const { schema, formik, removeItem, editItem } = useListContext();

const mapPreview = schema.fields.find((field) => field.type === "map");

Expand Down Expand Up @@ -189,8 +179,7 @@ const Root = () => {
listProps,
} = useListContext();

const { title, description, info, policyRef, howMeasured, handleSubmit } =
listProps;
const { title, description, info, policyRef, howMeasured } = listProps;

const rootError: string =
(errors.min && `You must provide at least ${schema.min} response(s)`) ||
Expand All @@ -201,32 +190,7 @@ const Root = () => {
const shouldShowAddAnotherButton =
schema.max !== 1 || formik.values.schemaData.length < 1;

// If the selected schema has a "map" field, ensure there's a FindProperty component preceding it (eg address data in state to position map view)
const hasMapField = schema.fields.some((field) => field.type === "map");
const { longitude, latitude } = useStore(
(state) =>
(state.computePassport()?.data?.["_address"] as SiteAddress) || {},
);

if (hasMapField && (!longitude || !latitude)) {
return (
<Card handleSubmit={handleSubmit} isValid>
<CardHeader title={title} description={description} />
<ErrorSummaryContainer
role="status"
data-testid="error-summary-invalid-graph"
>
<Typography variant="h4" component="h2" gutterBottom>
Invalid graph
</Typography>
<Typography variant="body2">
Edit this flow so that "List" is positioned after "FindProperty"; an
address is required for schemas that include a "map" field.
</Typography>
</ErrorSummaryContainer>
</Card>
);
}

const listContent = (
<ErrorWrapper error={rootError}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Box from "@mui/material/Box";
import { SiteAddress } from "@opensystemslab/planx-core/types";
import { MapContainer } from "@planx/components/shared/Preview/MapContainer";
import type { MapField } from "@planx/components/shared/Schema/model";
import { Feature } from "geojson";
Expand All @@ -11,6 +12,19 @@ import { getFieldProps, Props } from ".";
import { FieldInputDescription } from "./shared";

export const MapFieldInput: React.FC<Props<MapField>> = (props) => {
// Ensure there's a FindProperty component preceding this field (eg address data in state to position map view)
const { longitude, latitude } = useStore(
(state) =>
(state.computePassport()?.data?.["_address"] as SiteAddress) || {},
);

if (!longitude || !latitude) {
throw Error(
'Edit this flow so that this component is positioned after "FindProperty"; an address is required for schemas that include a "map" field.',
{ cause: "Invalid graph" },
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'm not sure I'm sold on throwing an error here rather than rendering a component as before - do we want this noise coming through Airbrake?

Understand we can't easily render the whole component any longer from this shared position, but just wondering if there's a mechanism to exclude from Airbrake here etc?

}

const {
formik,
data: { title, description, mapOptions },
Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/components/ErrorFallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function ErrorFallback(props: { error: Error }) {
<Card>
<ErrorSummaryContainer role="alert">
<Typography variant="h4" component="h1" gutterBottom>
Something went wrong
{(props.error.cause as string) || "Something went wrong"}
</Typography>
<Typography>
{props.error?.message && (
Expand Down
Loading