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

chore: add error feedback for editors if using List with "map" field without FindProperty first #3556

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
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
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
Loading