diff --git a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/ThemeAndLogoForm.tsx b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/ThemeAndLogoForm.tsx index a8a2f3f15c..20937873ec 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/ThemeAndLogoForm.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/Settings/DesignSettings/ThemeAndLogoForm.tsx @@ -1,7 +1,10 @@ import Link from "@mui/material/Link"; +import { getContrastRatio, useTheme } from "@mui/material/styles"; import Typography from "@mui/material/Typography"; +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"; @@ -9,17 +12,42 @@ import InputRowItem from "ui/shared/InputRowItem"; import InputRowLabel from "ui/shared/InputRowLabel"; import PublicFileUploadButton from "ui/shared/PublicFileUploadButton"; -import { DesignPreview, EXAMPLE_COLOUR, SettingsForm } from "."; +import { DesignPreview, SettingsForm } from "."; -export const ThemeAndLogoForm: React.FC = () => { - const formik = useFormik<{ - themeColor: string; - }>({ - initialValues: { - themeColor: EXAMPLE_COLOUR, +type FormValues = Pick; + +export const ThemeAndLogoForm: React.FC<{ team: Team }> = ({ team }) => { + const theme = useTheme(); + + useEffect(() => { + setInitialValues({ + primaryColour: team.theme?.primaryColour, + logo: team.theme?.logo, + }) + }, [team]) + + const [initialValues, setInitialValues] = useState({ + primaryColour: "", + logo: "", + }) + + const formik = useFormik({ + initialValues, + onSubmit: async (values, { resetForm }) => { + await useStore.getState().updateTeamTheme(values); + // Reset "dirty" status to disable Save & Reset buttons + resetForm({ values }); + }, + validateOnBlur: false, + validateOnChange: false, + enableReinitialize: true, + validate: ({ primaryColour }) => { + const isContrastThresholdMet = getContrastRatio("#FFF", primaryColour) > theme.palette.contrastThreshold; + + if (!isContrastThresholdMet) { + return { primaryColour: "Theme colour does not meet accessability contrast requirements (3:1)"} + }; }, - onSubmit: () => {}, - validate: () => {}, }); return ( @@ -46,8 +74,8 @@ export const ThemeAndLogoForm: React.FC = () => { formik.setFieldValue("themeColor", color)} + color={formik.values.primaryColour} + onChange={(color) => formik.setFieldValue("primaryColour", color)} label="Theme colour" /> @@ -55,7 +83,7 @@ export const ThemeAndLogoForm: React.FC = () => { Logo: - + formik.setFieldValue("logo", newUrl)}/> { } preview={ - - council logo + + { formik.values.logo + ? council logo + : {team?.name} + } } /> 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 43ab2b13fd..b2c565a4ad 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 @@ -2,9 +2,11 @@ import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; import { styled } from "@mui/material/styles"; import Typography from "@mui/material/Typography"; +import { Team } from "@opensystemslab/planx-core/types"; import { FormikProps, getIn } from "formik"; import { hasFeatureFlag } from "lib/featureFlags"; -import React from "react"; +import { useStore } from "pages/FlowEditor/lib/store"; +import React, { useEffect, useState } from "react"; import EditorRow from "ui/editor/EditorRow"; import { FeaturePlaceholder } from "ui/editor/FeaturePlaceholder"; import InputGroup from "ui/editor/InputGroup"; @@ -32,6 +34,27 @@ type SettingsFormProps = { preview?: React.ReactElement; }; +const useTeam = () => { + const [team, setTeam] = useState({} as Team); + + useEffect(() => { + console.log("fetching team!") + const fetchTeam = async () => { + try { + const fetchedTeam = await useStore.getState().fetchCurrentTeam(); + if (!fetchedTeam) throw Error("Unable to find team"); + setTeam(fetchedTeam); + } catch (error) { + console.error("Error fetching team:", error); + } + }; + + fetchTeam(); + }, []); + + return team; +}; + export const SettingsForm: React.FC = ({ formik, legend, @@ -79,6 +102,7 @@ export const SettingsForm: React.FC = ({ const DesignSettings: React.FC = () => { const isUsingFeatureFlag = hasFeatureFlag("SHOW_TEAM_SETTINGS"); + const team = useTeam(); return ( <> @@ -96,7 +120,7 @@ const DesignSettings: React.FC = () => { ) : ( <> - +