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: Wire up design settings (button colour) #2669

Merged
merged 4 commits into from
Jan 17, 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
@@ -1,23 +1,47 @@
import Button from "@mui/material/Button";
import Link from "@mui/material/Link";
import { darken, useTheme } from "@mui/material/styles";
import { Team, TeamTheme } from "@opensystemslab/planx-core/types";
import { useFormik } from "formik";
import React from "react";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect, useState } from "react";
import { getContrastTextColor } from "styleUtils";
import ColorPicker from "ui/editor/ColorPicker";
import InputDescription from "ui/editor/InputDescription";
import InputRow from "ui/shared/InputRow";
import InputRowItem from "ui/shared/InputRowItem";

import { DesignPreview, EXAMPLE_COLOUR, SettingsForm } from ".";
import { DesignPreview, SettingsForm } from ".";

export const ButtonForm: React.FC = () => {
const formik = useFormik<{
buttonColor: string;
}>({
initialValues: {
buttonColor: EXAMPLE_COLOUR,
type FormValues = Pick<TeamTheme, "actionColour">;

export const ButtonForm: React.FC<{ team: Team; onSuccess: () => void }> = ({
team,
onSuccess,
}) => {
const theme = useTheme();

useEffect(() => {
setInitialValues({ actionColour: team.theme?.actionColour });
}, [team]);

const [initialValues, setInitialValues] = useState<FormValues>({
actionColour: "",
});

const formik = useFormik<FormValues>({
initialValues,
onSubmit: async (values, { resetForm }) => {
const isSuccess = await useStore.getState().updateTeamTheme(values);
if (isSuccess) {
onSuccess();
// Reset "dirty" status to disable Save & Reset buttons
resetForm({ values });
}
},
onSubmit: () => {},
validate: () => {},
validateOnBlur: false,
validateOnChange: false,
enableReinitialize: true,
});

return (
Expand All @@ -42,16 +66,29 @@ export const ButtonForm: React.FC = () => {
<InputRow>
<InputRowItem>
<ColorPicker
color={formik.values.buttonColor}
onChange={(color) => formik.setFieldValue("buttonColor", color)}
color={formik.values.actionColour}
onChange={(color) => formik.setFieldValue("actionColour", color)}
label="Button colour"
/>
</InputRowItem>
</InputRow>
}
preview={
<DesignPreview bgcolor="white">
<Button variant="contained" sx={{ backgroundColor: EXAMPLE_COLOUR }}>
<Button
variant="contained"
sx={{
backgroundColor: formik.values.actionColour,
color: formik.values.actionColour
? getContrastTextColor(formik.values.actionColour, "#FFF")
: "#FFF",
"&:hover": {
backgroundColor: formik.values.actionColour
? darken(formik.values.actionColour, 0.2)
: theme.palette.prompt.dark,
},
}}
>
Example primary button
</Button>
</DesignPreview>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const DesignSettings: React.FC = () => {
) : (
<>
<ThemeAndLogoForm team={team} onSuccess={() => setOpen(true)} />
<ButtonForm />
<ButtonForm team={team} onSuccess={() => setOpen(true)} />
<TextLinkForm />
<FaviconForm />
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
Expand Down
Loading