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: Make SettingsForm shared and generic #3308

Merged
merged 1 commit into from
Jun 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import InputDescription from "ui/editor/InputDescription";
import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";

import { DesignPreview, FormProps, SettingsForm } from ".";
import { DesignPreview, FormProps } from ".";
import { SettingsForm } from "../shared/SettingsForm";

export const ButtonForm: React.FC<FormProps> = ({
formikConfig,
Expand All @@ -33,7 +34,7 @@ export const ButtonForm: React.FC<FormProps> = ({
});

return (
<SettingsForm
<SettingsForm<TeamTheme>
formik={formik}
legend="Button colour"
description={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";
import InputRowLabel from "ui/shared/InputRowLabel";

import { FormProps, SettingsForm } from ".";
import { FormProps } from ".";
import { SettingsForm } from "../shared/SettingsForm";

export const FaviconForm: React.FC<FormProps> = ({
formikConfig,
Expand All @@ -36,7 +37,7 @@ export const FaviconForm: React.FC<FormProps> = ({
: formik.setFieldValue("favicon", null);

return (
<SettingsForm
<SettingsForm<TeamTheme>
formik={formik}
legend="Favicon"
description={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import InputDescription from "ui/editor/InputDescription";
import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";

import { DesignPreview, FormProps, SettingsForm } from ".";
import { DesignPreview, FormProps } from ".";
import { SettingsForm } from "../shared/SettingsForm";

export const TextLinkForm: React.FC<FormProps> = ({
formikConfig,
Expand Down Expand Up @@ -43,7 +44,7 @@ export const TextLinkForm: React.FC<FormProps> = ({
});

return (
<SettingsForm
<SettingsForm<TeamTheme>
formik={formik}
legend="Text link colour"
description={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";
import InputRowLabel from "ui/shared/InputRowLabel";

import { DesignPreview, FormProps, SettingsForm } from ".";
import { DesignPreview, FormProps } from ".";
import { SettingsForm } from "../shared/SettingsForm";

export const ThemeAndLogoForm: React.FC<FormProps> = ({
formikConfig,
Expand Down Expand Up @@ -54,7 +55,7 @@ export const ThemeAndLogoForm: React.FC<FormProps> = ({
: formik.setFieldValue("logo", null);

return (
<SettingsForm
<SettingsForm<TeamTheme>
formik={formik}
legend="Theme colour & logo"
description={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import Alert from "@mui/material/Alert";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Snackbar from "@mui/material/Snackbar";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import { TeamTheme } from "@opensystemslab/planx-core/types";
import { FormikConfig, FormikProps } from "formik";
import { FormikConfig } from "formik";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect, useState } from "react";
import EditorRow from "ui/editor/EditorRow";
import InputGroup from "ui/editor/InputGroup";
import InputLegend from "ui/editor/InputLegend";
import ErrorWrapper from "ui/shared/ErrorWrapper";

import { ButtonForm } from "./ButtonForm";
import { FaviconForm } from "./FaviconForm";
Expand All @@ -26,67 +22,11 @@ export const DesignPreview = styled(Box)(({ theme }) => ({

export const EXAMPLE_COLOUR = "#007078";

type SettingsFormProps = {
legend: string;
description: React.ReactElement;
input: React.ReactElement;
formik: FormikProps<TeamTheme>;
preview?: React.ReactElement;
};

export interface FormProps {
formikConfig: FormikConfig<TeamTheme>;
onSuccess: () => void;
}

export const SettingsForm: React.FC<SettingsFormProps> = ({
formik,
legend,
description,
input,
preview,
}) => {
return (
<EditorRow background>
<form onSubmit={formik.handleSubmit}>
<InputGroup flowSpacing>
<InputLegend>{legend}</InputLegend>
{description}
{input}
</InputGroup>
{preview && (
<Box>
<Typography variant="h4" my={1}>
Preview:
</Typography>
{preview}
</Box>
)}
<ErrorWrapper
error={Object.values(formik.errors).join(", ")}
id="design-settings-theme-error"
>
<Box>
<Button type="submit" variant="contained" disabled={!formik.dirty}>
Save
</Button>
<Button
onClick={() => formik.resetForm()}
type="reset"
variant="contained"
disabled={!formik.dirty}
color="secondary"
sx={{ ml: 1.5 }}
>
Reset changes
</Button>
</Box>
</ErrorWrapper>
</form>
</EditorRow>
);
};

const DesignSettings: React.FC = () => {
const [formikConfig, setFormikConfig] = useState<
FormikConfig<TeamTheme> | undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import { FormikProps } from "formik";
import React from "react";
import EditorRow from "ui/editor/EditorRow";
import InputGroup from "ui/editor/InputGroup";
import InputLegend from "ui/editor/InputLegend";
import ErrorWrapper from "ui/shared/ErrorWrapper";

type SettingsFormProps<TFormikValues> = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the core difference in this PR.

The previous props took a hardcoded TeamTheme, we're now using a generic type here.

legend: string;
description: React.ReactElement;
input: React.ReactElement;
formik: FormikProps<TFormikValues>;
preview?: React.ReactElement;
};

export const SettingsForm = <TFormikValues,>({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from changes to types and an update of the ErrorWrapper id, this component is copy/pasted from /DesignSettings/index.tsx

formik,
legend,
description,
input,
preview,
}: SettingsFormProps<TFormikValues>) => {
return (
<EditorRow background>
<form onSubmit={formik.handleSubmit}>
<InputGroup flowSpacing>
<InputLegend>{legend}</InputLegend>
{description}
{input}
</InputGroup>
{preview && (
<Box>
<Typography variant="h4" my={1}>
Preview:
</Typography>
{preview}
</Box>
)}
<ErrorWrapper
error={Object.values(formik.errors).join(", ")}
id="settings-error"
>
<Box>
<Button type="submit" variant="contained" disabled={!formik.dirty}>
Save
</Button>
<Button
onClick={() => formik.resetForm()}
type="reset"
variant="contained"
disabled={!formik.dirty}
color="secondary"
sx={{ ml: 1.5 }}
>
Reset changes
</Button>
</Box>
</ErrorWrapper>
</form>
</EditorRow>
);
};
Loading