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: Run pnpm lint:fix in Editor #3313

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions editor.planx.uk/src/@planx/components/DateInput/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ describe("dateSchema", () => {
expect(await dateSchema().isValid("2021-23-03")).toBe(false);
});

const validate = async (date?: string) => await dateSchema().validate(date).catch((err) => err.errors);
const validate = async (date?: string) =>
await dateSchema()
.validate(date)
.catch((err) => err.errors);

it("throws an error for an undefined value (empty form)", async () => {
const errors = await validate(undefined);
Expand All @@ -58,12 +61,12 @@ describe("dateSchema", () => {
const errors = await validate("ab-cd-efgh");
expect(errors[0]).toMatch(/Date must include a day/);
});

it("throws an error for a missing day", async () => {
const errors = await validate("2024-12-");
expect(errors[0]).toMatch(/Date must include a day/);
});

it("throws an error for a missing month", async () => {
const errors = await validate("2024--25");
expect(errors[0]).toMatch(/Date must include a month/);
Expand Down Expand Up @@ -112,7 +115,7 @@ describe("dateRangeSchema", () => {
"1980-06-15",
),
).toBe(false);
})
});
});

test("padding on input", () => {
Expand Down Expand Up @@ -145,4 +148,4 @@ test("padding on blur", () => {
// Leaves single 0 alone
expect(paddedDate("2021-0-2", "blur")).toBe("2021-0-02");
expect(paddedDate("2021-10-0", "blur")).toBe("2021-10-0");
});
});
98 changes: 43 additions & 55 deletions editor.planx.uk/src/@planx/components/DateInput/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,47 +60,33 @@ const displayDate = (date: string): string | undefined => {
};

export const parseDate = (date?: string) => {
const [year, month, day] = date?.split("-").map((val) => parseInt(val) || undefined) || [];
const [year, month, day] =
date?.split("-").map((val) => parseInt(val) || undefined) || [];
return { year, month, day };
}
};

export const dateSchema = () => {
return string()
.test(
"missing day",
"Date must include a day",
(date?: string) => {
const { day } = parseDate(date);
return day !== undefined;
})
.test(
"missing month",
"Date must include a month",
(date?: string) => {
const { month } = parseDate(date);
return month !== undefined;
})
.test(
"missing year",
"Date must include a year",
(date?: string) => {
const { year } = parseDate(date);
return year !== undefined;
})
.test(
"invalid day",
"Day must be valid",
(date?: string) => {
const { day } = parseDate(date);
return Boolean(day && day <= 31)
})
.test(
"invalid month",
"Month must be valid",
(date?: string) => {
const { month } = parseDate(date);
return Boolean(month && month <= 12);
})
.test("missing day", "Date must include a day", (date?: string) => {
const { day } = parseDate(date);
return day !== undefined;
})
.test("missing month", "Date must include a month", (date?: string) => {
const { month } = parseDate(date);
return month !== undefined;
})
.test("missing year", "Date must include a year", (date?: string) => {
const { year } = parseDate(date);
return year !== undefined;
})
.test("invalid day", "Day must be valid", (date?: string) => {
const { day } = parseDate(date);
return Boolean(day && day <= 31);
})
.test("invalid month", "Month must be valid", (date?: string) => {
const { month } = parseDate(date);
return Boolean(month && month <= 12);
})
.test(
"valid",
"Enter a valid date in DD.MM.YYYY format",
Expand All @@ -115,24 +101,26 @@ export const dateRangeSchema: (params: {
min?: string;
max?: string;
}) => SchemaOf<string> = (params) =>
dateSchema()
.required("Enter a valid date in DD.MM.YYYY format")
.test({
name: "too soon",
message: `Enter a date later than ${params.min && displayDate(params.min)
}`,
test: (date: string | undefined) => {
return Boolean(date && !(params.min && date < params.min));
},
})
.test({
name: "too late",
message: `Enter a date earlier than ${params.max && displayDate(params.max)
}`,
test: (date: string | undefined) => {
return Boolean(date && !(params.max && date > params.max));
},
});
dateSchema()
.required("Enter a valid date in DD.MM.YYYY format")
.test({
name: "too soon",
message: `Enter a date later than ${
params.min && displayDate(params.min)
}`,
test: (date: string | undefined) => {
return Boolean(date && !(params.min && date < params.min));
},
})
.test({
name: "too late",
message: `Enter a date earlier than ${
params.max && displayDate(params.max)
}`,
test: (date: string | undefined) => {
return Boolean(date && !(params.max && date > params.max));
},
});

export const parseDateInput = (
data: Record<string, any> | undefined,
Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/@planx/components/List/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import InputRowLabel from "ui/shared/InputRowLabel";
import { EditorProps, ICONS, InternalNotes, MoreInformation } from "../ui";
import { List, parseContent } from "./model";
import { ProposedAdvertisements } from "./schemas/Adverts";
import { NonResidentialFloorspace } from "./schemas/Floorspace";
import { BuildingDetailsGLA } from "./schemas/GLA/BuildingDetails";
import { CommunalSpaceGLA } from "./schemas/GLA/CommunalSpace";
import { ExistingAndProposedUsesGLA } from "./schemas/GLA/ExistingAndProposedUses";
Expand All @@ -24,7 +25,6 @@ import { ResidentialUnitsExisting } from "./schemas/ResidentialUnits/Existing";
import { ResidentialUnitsGLAGained } from "./schemas/ResidentialUnits/GLA/Gained";
import { ResidentialUnitsGLALost } from "./schemas/ResidentialUnits/GLA/Lost";
import { ResidentialUnitsProposed } from "./schemas/ResidentialUnits/Proposed";
import { NonResidentialFloorspace } from "./schemas/Floorspace";

type Props = EditorProps<TYPES.List, List>;

Expand Down
19 changes: 9 additions & 10 deletions editor.planx.uk/src/@planx/components/List/Public/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export const ListProvider: React.FC<ListProviderProps> = (props) => {
props.previouslySubmittedData ? -1 : 0,
);

const [activeItemInitialState, setActiveItemInitialState] = useState<UserResponse | undefined>(undefined);
const [activeItemInitialState, setActiveItemInitialState] = useState<
UserResponse | undefined
>(undefined);

const [addItemError, setAddItemError] = useState<boolean>(false);
const [unsavedItemError, setUnsavedItemError] = useState<boolean>(false);
Expand Down Expand Up @@ -132,16 +134,16 @@ export const ListProvider: React.FC<ListProviderProps> = (props) => {
activeItemInitialState
? resetItemToPreviousState()
: removeItem(activeIndex);

setActiveItemInitialState(undefined);

exitEditMode();
}
};

const editItem = (index: number) => {
setActiveItemInitialState(formik.values.userData[index]);
setActiveIndex(index);
}
};

const getInitialValues = () => {
const previousValues = getPreviouslySubmittedData(props);
Expand All @@ -151,12 +153,9 @@ export const ListProvider: React.FC<ListProviderProps> = (props) => {
};

const exitEditMode = () => setActiveIndex(-1);

const resetItemToPreviousState = () =>
formik.setFieldValue(
`userData[${activeIndex}]`,
activeItemInitialState
)

const resetItemToPreviousState = () =>
formik.setFieldValue(`userData[${activeIndex}]`, activeItemInitialState);

const isPageComponent = schema.max === 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe("Building a list", () => {
expect(cards).toHaveLength(1);

const cancelButton = getByText(/Cancel/, { selector: "button" });
await user.click(cancelButton)
await user.click(cancelButton);

cards = queryAllByTestId(/list-card/);
expect(cards).toHaveLength(0);
Expand Down Expand Up @@ -376,7 +376,7 @@ describe("Building a list", () => {
expect(getByText("[email protected]")).toBeInTheDocument();
});
});

describe("Form validation and error handling", () => {
test.todo("form validation is triggered when saving an item");
test.todo("text fields use existing validation schemas");
Expand Down
6 changes: 4 additions & 2 deletions editor.planx.uk/src/@planx/components/List/Public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const InputField: React.FC<Field> = (props) => {
const ActiveListCard: React.FC<{
index: number;
}> = ({ index: i }) => {
const { schema, saveItem, cancelEditItem, errors, isPageComponent } = useListContext();
const { schema, saveItem, cancelEditItem, errors, isPageComponent } =
useListContext();

return (
<ErrorWrapper
Expand Down Expand Up @@ -99,7 +100,8 @@ const ActiveListCard: React.FC<{
const InactiveListCard: React.FC<{
index: number;
}> = ({ index: i }) => {
const { schema, formik, removeItem, editItem, isPageComponent } = useListContext();
const { schema, formik, removeItem, editItem, isPageComponent } =
useListContext();

return (
<ListCard data-testid={`list-card-${i}`}>
Expand Down
61 changes: 48 additions & 13 deletions editor.planx.uk/src/@planx/components/List/schemas/Floorspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,71 @@ export const NonResidentialFloorspace: Schema = {
},
{
id: "eAShops",
data: { text: "E(a) - Retail (other than hot food): Shops", val: "eAShops" },
data: {
text: "E(a) - Retail (other than hot food): Shops",
val: "eAShops",
},
},
{
id: "eANetTradeableArea",
data: { text: "E(a) - Retail (other than hot food): Net tradeable area", val: "eANetTradeableArea" },
data: {
text: "E(a) - Retail (other than hot food): Net tradeable area",
val: "eANetTradeableArea",
},
},
{
id: "eB",
data: { text: "E(b) - Sale of food and drink (mostly consumed on the premises)", val: "eB" },
data: {
text: "E(b) - Sale of food and drink (mostly consumed on the premises)",
val: "eB",
},
},
{
id: "eCI",
data: { text: "E(c)(i) - Financial services", val: "eCI" },
},
{
id: "eCII",
data: { text: "E(c)(ii) - Professional services (other than health or medical)", val: "eCII" },
data: {
text: "E(c)(ii) - Professional services (other than health or medical)",
val: "eCII",
},
},
{
id: "eCIII",
data: { text: "E(c)(iii) - Any other service", val: "eCIII" },
},
{
id: "eD",
data: { text: "E(d) - Indoor sports, recreation or fitness", val: "eD" },
data: {
text: "E(d) - Indoor sports, recreation or fitness",
val: "eD",
},
},
{
id: "eF",
data: { text: "E(f) - Creche or day nursery", val: "eF" },
},
{
id: "eGI",
data: { text: "E(g)(i) - Office (to carry out operational or administrative functions)", val: "eGI" },
data: {
text: "E(g)(i) - Office (to carry out operational or administrative functions)",
val: "eGI",
},
},
{
id: "eGII",
data: { text: "E(g)(ii) - Research and development of products or processes", val: "eGII" },
data: {
text: "E(g)(ii) - Research and development of products or processes",
val: "eGII",
},
},
{
id: "eGIII",
data: { text: "E(g)(iii) - Any industrial process (can be carried out within a residential area)", val: "eGIII" },
data: {
text: "E(g)(iii) - Any industrial process (can be carried out within a residential area)",
val: "eGIII",
},
},
{
id: "fOneA",
Expand Down Expand Up @@ -121,19 +145,28 @@ export const NonResidentialFloorspace: Schema = {
},
{
id: "fTwoA",
data: { text: "F2(a) - Shop selling essential goods (not over 280sqm and no other such facility in 1000m radius)", val: "fTwoA" },
data: {
text: "F2(a) - Shop selling essential goods (not over 280sqm and no other such facility in 1000m radius)",
val: "fTwoA",
},
},
{
id: "fTwoB",
data: { text: "F2(b) - Hall or meeting place for local community (principal use)", val: "fTwoB" },
data: {
text: "F2(b) - Hall or meeting place for local community (principal use)",
val: "fTwoB",
},
},
{
id: "fTwoC",
data: { text: "F2(c) - Outdoor sport or recreation", val: "fTwoC" },
},
{
id: "fTwoD",
data: { text: "F2(d) - Indoor or outdoor swimming pool or skating rink", val: "fTwoD" },
data: {
text: "F2(d) - Indoor or outdoor swimming pool or skating rink",
val: "fTwoD",
},
},
{ id: "other", data: { text: "Other", val: "other" } },
],
Expand All @@ -151,7 +184,8 @@ export const NonResidentialFloorspace: Schema = {
{
type: "number",
data: {
title: "What is the gross internal floor area to be lost by change of use or demolition?",
title:
"What is the gross internal floor area to be lost by change of use or demolition?",
units: "m²",
fn: "area.loss",
allowNegatives: false,
Expand All @@ -160,7 +194,8 @@ export const NonResidentialFloorspace: Schema = {
{
type: "number",
data: {
title: "What is the total gross internal floor area proposed (including change of use)?",
title:
"What is the total gross internal floor area proposed (including change of use)?",
units: "m²",
fn: "area.proposed",
allowNegatives: false,
Expand Down
Loading
Loading