Skip to content

Commit

Permalink
chore: add error feedback for editors if using List with "map" field …
Browse files Browse the repository at this point in the history
…without FindProperty first (#3556)
  • Loading branch information
jessicamcinchak authored Aug 26, 2024
1 parent 0ce9607 commit 0dbb6d4
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions editor.planx.uk/src/@planx/components/List/Public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ 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 ErrorWrapper from "ui/shared/ErrorWrapper";
Expand Down Expand Up @@ -40,8 +43,15 @@ const CardButton = styled(Button)(({ theme }) => ({
const ActiveListCard: React.FC<{
index: number;
}> = ({ index: i }) => {
const { schema, saveItem, cancelEditItem, errors, isPageComponent, formik, activeIndex } =
useListContext();
const {
schema,
saveItem,
cancelEditItem,
errors,
isPageComponent,
formik,
activeIndex,
} = useListContext();

const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
Expand All @@ -59,7 +69,11 @@ const ActiveListCard: React.FC<{
{schema.type}
{!isPageComponent && ` ${i + 1}`}
</Typography>
<SchemaFields schema={schema} activeIndex={activeIndex} formik={formik}/>
<SchemaFields
schema={schema}
activeIndex={activeIndex}
formik={formik}
/>
<Box display="flex" gap={2}>
<Button
variant="contained"
Expand Down Expand Up @@ -130,7 +144,8 @@ const Root = () => {
listProps,
} = useListContext();

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

const rootError: string =
(errors.min && `You must provide at least ${schema.min} response(s)`) ||
Expand All @@ -141,6 +156,33 @@ 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>
);
}

return (
<Card handleSubmit={validateAndSubmitForm} isValid>
<CardHeader
Expand Down

0 comments on commit 0dbb6d4

Please sign in to comment.