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

feat: List component - submission and back navigation #3220

Merged
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
50 changes: 34 additions & 16 deletions editor.planx.uk/src/@planx/components/List/Public/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import {
getPreviouslySubmittedData,
makeData,
} from "@planx/components/shared/utils";
import { PublicProps } from "@planx/components/ui";
import { FormikProps, useFormik } from "formik";
import React, { createContext, ReactNode, useContext, useState } from "react";
import React, {
createContext,
PropsWithChildren,
useContext,
useState,
} from "react";

import {
generateInitialValues,
generateValidationSchema,
List,
Schema,
UserData,
} from "../model";
Expand All @@ -17,7 +28,8 @@ interface ListContextValue {
editItem: (index: number) => void;
cancelEditItem: () => void;
formik: FormikProps<UserData>;
handleSubmit: () => void;
validateAndSubmitForm: () => void;
listProps: PublicProps<List>;
errors: {
addItem: boolean;
unsavedItem: boolean;
Expand All @@ -26,18 +38,16 @@ interface ListContextValue {
};
}

interface ListProviderProps {
children: ReactNode;
schema: Schema;
}
type ListProviderProps = PropsWithChildren<PublicProps<List>>;

const ListContext = createContext<ListContextValue | undefined>(undefined);

export const ListProvider: React.FC<ListProviderProps> = ({
children,
schema,
}) => {
const [activeIndex, setActiveIndex] = useState<number>(0);
export const ListProvider: React.FC<ListProviderProps> = (props) => {
const { schema, children, handleSubmit } = props;

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

const [addItemError, setAddItemError] = useState<boolean>(false);
const [unsavedItemError, setUnsavedItemError] = useState<boolean>(false);
Expand Down Expand Up @@ -91,7 +101,7 @@ export const ListProvider: React.FC<ListProviderProps> = ({
);
};

const handleSubmit = () => {
const validateAndSubmitForm = () => {
// Do not allow submissions with an unsaved item
if (activeIndex !== -1) return setUnsavedItemError(true);

Expand All @@ -106,15 +116,22 @@ export const ListProvider: React.FC<ListProviderProps> = ({
};

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

const editItem = (index: number) => setActiveIndex(index);

const getInitialValues = () => {
const previousValues = getPreviouslySubmittedData(props);
if (previousValues) return previousValues;

return schema.min ? [generateInitialValues(schema)] : [];
};

const formik = useFormik<UserData>({
initialValues: {
userData: schema.min ? [generateInitialValues(schema)] : [],
userData: getInitialValues(),
},
onSubmit: (values) => {
console.log("Submit!");
console.log({ values });
handleSubmit?.(makeData(props, values.userData));
},
validateOnBlur: false,
validateOnChange: false,
Expand All @@ -128,11 +145,12 @@ export const ListProvider: React.FC<ListProviderProps> = ({
addNewItem,
saveItem,
schema,
listProps: props,
editItem,
removeItem,
cancelEditItem,
formik,
handleSubmit,
validateAndSubmitForm,
errors: {
addItem: addItemError,
unsavedItem: unsavedItemError,
Expand Down
Loading
Loading