From 82c7b70afa887fa49976e41f6fd08a38053c62f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 16 Jan 2024 17:14:56 +0000 Subject: [PATCH] feat: Wire up design settings (link colour) --- .../Settings/DesignSettings/TextLinkForm.tsx | 62 +++++++++++++++---- .../Settings/DesignSettings/index.tsx | 2 +- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/TextLinkForm.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/TextLinkForm.tsx index f89d7c0223..7977aabcd5 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/TextLinkForm.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/TextLinkForm.tsx @@ -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; + +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({ + linkColour: "", + }); + + const formik = useFormik({ + 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 ( @@ -40,8 +74,8 @@ export const TextLinkForm: React.FC = () => { formik.setFieldValue("linkColor", color)} + color={formik.values.linkColour} + onChange={(color) => formik.setFieldValue("linkColour", color)} label="Text link colour" /> @@ -49,7 +83,9 @@ export const TextLinkForm: React.FC = () => { } preview={ - Example text link + + Example text link + } /> diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/index.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/index.tsx index 4bc13a3951..503ef61fac 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/index.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/index.tsx @@ -138,7 +138,7 @@ const DesignSettings: React.FC = () => { <> setOpen(true)} /> - + setOpen(true)} />