Skip to content

Commit

Permalink
chore: Rename types
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Aug 22, 2024
1 parent d0bdced commit acfce5c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 41 deletions.
14 changes: 7 additions & 7 deletions editor.planx.uk/src/@planx/components/List/Public/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useSchema } from "@planx/components/shared/Schema/hook";
import {
Schema,
SchemaData,
SchemaResponse,
} from "@planx/components/shared/Schema/model"
SchemaUserData,
SchemaUserResponse,
} from "@planx/components/shared/Schema/model";
import {
getPreviouslySubmittedData,
makeData,
Expand Down Expand Up @@ -32,7 +32,7 @@ interface ListContextValue {
removeItem: (index: number) => void;
editItem: (index: number) => void;
cancelEditItem: () => void;
formik: FormikProps<SchemaData>;
formik: FormikProps<SchemaUserData>;
validateAndSubmitForm: () => void;
listProps: PublicProps<List>;
/**
Expand Down Expand Up @@ -62,7 +62,7 @@ export const ListProvider: React.FC<ListProviderProps> = (props) => {
previousValues: getPreviouslySubmittedData(props),
});

const formik = useFormik<SchemaData>({
const formik = useFormik<SchemaUserData>({
...formikConfig,
onSubmit: (values) => {
// defaultPassportData (array) is used when coming "back"
Expand Down Expand Up @@ -100,14 +100,14 @@ export const ListProvider: React.FC<ListProviderProps> = (props) => {
},
});
},
})
});

const [activeIndex, setActiveIndex] = useState<number>(
props.previouslySubmittedData ? -1 : 0,
);

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

const [addItemError, setAddItemError] = useState<boolean>(false);
Expand Down
3 changes: 1 addition & 2 deletions editor.planx.uk/src/@planx/components/List/model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Feature } from "geojson";
import { cloneDeep } from "lodash";

import { MoreInformation, parseMoreInformation } from "../shared";
import { Schema } from "../shared/Schema/model"
import { Schema } from "../shared/Schema/model";
import { SCHEMAS } from "./Editor";

export interface List extends MoreInformation {
Expand Down
4 changes: 2 additions & 2 deletions editor.planx.uk/src/@planx/components/List/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaResponse } from "./../shared/Schema/model";
import { SchemaUserResponse } from "./../shared/Schema/model";
import {
flatten,
sumIdenticalUnits,
Expand Down Expand Up @@ -63,7 +63,7 @@ describe("passport data shape", () => {
identicalUnits: 2,
},
],
} as unknown as Record<string, SchemaResponse[]>;
} as unknown as Record<string, SchemaUserResponse[]>;

expect(
sumIdenticalUnits("proposal.units.residential", defaultPassportData),
Expand Down
6 changes: 3 additions & 3 deletions editor.planx.uk/src/@planx/components/List/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { styled } from "@mui/material/styles";
import React from "react";

import { Field, SchemaResponse } from "./../shared/Schema/model";
import { Field, SchemaUserResponse } from "./../shared/Schema/model";

const List = styled("ul")(() => ({
listStylePosition: "inside",
Expand Down Expand Up @@ -54,7 +54,7 @@ export function formatSchemaDisplayValue(
*/
export function sumIdenticalUnits(
fn: string,
passportData: Record<string, SchemaResponse[]>,
passportData: Record<string, SchemaUserResponse[]>,
): number {
let sum = 0;
passportData[`${fn}`].map((item) => {
Expand All @@ -73,7 +73,7 @@ export function sumIdenticalUnits(
*/
export function sumIdenticalUnitsByDevelopmentType(
fn: string,
passportData: Record<string, SchemaResponse[]>,
passportData: Record<string, SchemaUserResponse[]>,
): Record<string, number> {
// Sum identical units by development type (@todo read all possible option `val` from Schema in future)
const baseSums: Record<string, number> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Field, SchemaData } from "@planx/components/shared/Schema/model";
import type {
Field,
SchemaUserData,
} from "@planx/components/shared/Schema/model";
import { FormikProps } from "formik";
import { get } from "lodash";
import React from "react";
Expand All @@ -12,7 +15,7 @@ import { SelectFieldInput } from "./SelectInputField";
import { TextFieldInput } from "./TextFieldInput";

export type Props<T extends Field> = {
formik: FormikProps<SchemaData>;
formik: FormikProps<SchemaUserData>;
activeIndex: number;
} & T;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import React from "react";
import InputRow from "ui/shared/InputRow";

import { InputFields } from "./InputFields";
import { Schema, SchemaData } from "./model";
import { Schema, SchemaUserData } from "./model";

interface SchemaFieldsProps {
/**
* Optional index of currently active schema instance.
* Only required if multiple user responses are allowed, e.g. in the List component.
* Defaults to 0 as `SchemaData` always holds an array of responses
* Defaults to 0 as `SchemaUserData` always holds an array of responses
*/
activeIndex?: number;
/** Formik instance generated from config provided by useSchema hook */
formik: FormikProps<SchemaData>;
formik: FormikProps<SchemaUserData>;
schema: Schema;
}

Expand Down
35 changes: 19 additions & 16 deletions editor.planx.uk/src/@planx/components/shared/Schema/hook.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import { FormikConfig } from "formik";

import { generateInitialValues, generateValidationSchema, Schema, SchemaData, SchemaResponse } from "./model";
import {
generateInitialValues,
generateValidationSchema,
Schema,
SchemaUserData,
SchemaUserResponse,
} from "./model";

type UseSchema = (props: {
schema: Schema;
previousValues?: SchemaResponse[];
previousValues?: SchemaUserResponse[];
}) => {
/**
* Extensible Formik config which allows custom form state and submission logic
* @example const formik = useFormik<SchemaData & MyCustomState>(...)
* @example const formik = useFormik<SchemaData>(...formikConfig, onSubmit: () => {...})
*/
formikConfig: Omit<FormikConfig<SchemaData>, "onSubmit">,
/**
* @example const formik = useFormik<SchemaUserData | MyCustomState>(...)
* @example const formik = useFormik<SchemaUserData>(...formikConfig, onSubmit: () => {...})
*/
formikConfig: Omit<FormikConfig<SchemaUserData>, "onSubmit">;
/**
* A blank set of initial values matching the schema
* Can be if multiple responses are allowed (e.g. in the List component)
*/
initialValues: SchemaResponse,
}
*/
initialValues: SchemaUserResponse;
};

/**
* Hook which allows you to embed a group of fields, described by a schema, within another component
* Form state and custom logic is stored and managed within the parent component
*/
export const useSchema: UseSchema = ({
schema,
previousValues,
}) => {
export const useSchema: UseSchema = ({ schema, previousValues }) => {
const validationSchema = generateValidationSchema(schema);
const initialValues = generateInitialValues(schema);

Expand All @@ -43,7 +46,7 @@ export const useSchema: UseSchema = ({
validateOnBlur: false,
validateOnChange: false,
validationSchema,
}
};

return { formikConfig, initialValues };
}
};
24 changes: 18 additions & 6 deletions editor.planx.uk/src/@planx/components/shared/Schema/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
DateInput,
dateRangeSchema as dateValidationSchema,
} from "../../DateInput/model";
import { NumberInput, numberInputValidationSchema } from "../../NumberInput/model";
import {
NumberInput,
numberInputValidationSchema,
} from "../../NumberInput/model";
import {
TextInput,
userDataSchema as textInputValidationSchema,
Expand Down Expand Up @@ -95,7 +98,13 @@ export type MapField = {
* Represents the input types available in the List component
* Existing models are used to allow to us to re-use existing components, maintaining consistend UX/UI
*/
export type Field = TextField | NumberField | QuestionField | ChecklistField | DateField | MapField;
export type Field =
| TextField
| NumberField
| QuestionField
| ChecklistField
| DateField
| MapField;

/**
* Models the form displayed to the user
Expand All @@ -107,12 +116,15 @@ export interface Schema {
max?: number;
}

export type SchemaResponse = Record<Field["data"]["fn"], string | string[] | any[] >; // string | string[] | Feature[]
export type SchemaUserResponse = Record<
Field["data"]["fn"],
string | string[] | any[]
>; // string | string[] | Feature[]

/**
* Output data from a form using the useSchema hook
*/
export type SchemaData = { schemaData: SchemaResponse[] };
export type SchemaUserData = { schemaData: SchemaUserResponse[] };

/**
* For each field in schema, return a map of Yup validation schema
Expand Down Expand Up @@ -166,8 +178,8 @@ export const generateValidationSchema = (schema: Schema) => {
return validationSchema;
};

export const generateInitialValues = (schema: Schema): SchemaResponse => {
const initialValues: SchemaResponse = {};
export const generateInitialValues = (schema: Schema): SchemaUserResponse => {
const initialValues: SchemaUserResponse = {};
schema.fields.forEach((field) => {
["checklist", "map"].includes(field.type)
? (initialValues[field.data.fn] = [])
Expand Down

0 comments on commit acfce5c

Please sign in to comment.