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 (text link colour) #2670

Merged
merged 4 commits into from
Jan 18, 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,22 +1,56 @@
import Link from "@mui/material/Link";
import { getContrastRatio, 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 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 TextLinkForm: React.FC = () => {
const formik = useFormik<{
linkColor: string;
}>({
initialValues: {
linkColor: EXAMPLE_COLOUR,
type FormValues = Pick<TeamTheme, "linkColour">;

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

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

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

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 });
}
},
validateOnBlur: false,
validateOnChange: false,
enableReinitialize: true,
validate: ({ linkColour }) => {
const isContrastThresholdMet =
getContrastRatio("#FFF", linkColour) > theme.palette.contrastThreshold;

if (!isContrastThresholdMet) {
return {
linkColour:
"Colour does not meet accessibility contrast requirements (3:1)",
};
}
},
onSubmit: () => {},
validate: () => {},
});

return (
Expand All @@ -40,16 +74,18 @@ export const TextLinkForm: React.FC = () => {
<InputRow>
<InputRowItem>
<ColorPicker
color={formik.values.linkColor}
onChange={(color) => formik.setFieldValue("linkColor", color)}
color={formik.values.linkColour}
onChange={(color) => formik.setFieldValue("linkColour", color)}
label="Text link colour"
/>
</InputRowItem>
</InputRow>
}
preview={
<DesignPreview bgcolor="white">
<Link sx={{ color: EXAMPLE_COLOUR }}>Example text link</Link>
<Link sx={{ color: formik.values.linkColour }}>
Example text link
</Link>
</DesignPreview>
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const SettingsForm: React.FC<SettingsFormProps> = ({
</Box>
)}
<ErrorWrapper
error={getIn(formik.errors, "primaryColour")}
error={Object.values(formik.errors).join(", ")}
id="design-settings-theme-error"
>
<Box>
Expand Down Expand Up @@ -138,7 +138,7 @@ const DesignSettings: React.FC = () => {
<>
<ThemeAndLogoForm team={team} onSuccess={() => setOpen(true)} />
<ButtonForm team={team} onSuccess={() => setOpen(true)} />
<TextLinkForm />
<TextLinkForm team={team} onSuccess={() => setOpen(true)} />
<FaviconForm />
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
<Alert
Expand Down
Loading