import React, { useEffect, useState } from "react"; import { useTheme } from "next-themes"; import { useForm } from "react-hook-form"; // hooks import useUser from "hooks/use-user"; // ui import { PrimaryButton } from "components/ui"; import { ColorPickerInput } from "components/core"; // services import userService from "services/user.service"; // helper import { applyTheme } from "helpers/theme.helper"; // types import { ICustomTheme } from "types"; type Props = { preLoadedData?: Partial | null; }; export const CustomThemeSelector: React.FC = ({ preLoadedData }) => { const [darkPalette, setDarkPalette] = useState(false); const defaultValues = { accent: preLoadedData?.accent ?? "#FE5050", bgBase: preLoadedData?.bgBase ?? "#FFF7F7", bgSurface1: preLoadedData?.bgSurface1 ?? "#FFE0E0", bgSurface2: preLoadedData?.bgSurface2 ?? "#FFF7F7", border: preLoadedData?.border ?? "#FFC9C9", darkPalette: preLoadedData?.darkPalette ?? false, palette: preLoadedData?.palette ?? "", sidebar: preLoadedData?.sidebar ?? "#FFFFFF", textBase: preLoadedData?.textBase ?? "#430000", textSecondary: preLoadedData?.textSecondary ?? "#323232", }; const { register, formState: { errors, isSubmitting }, handleSubmit, watch, setValue, reset, } = useForm({ defaultValues, }); const { setTheme } = useTheme(); const { mutateUser } = useUser(); const handleFormSubmit = async (formData: any) => { await userService .updateUser({ theme: { accent: formData.accent, bgBase: formData.bgBase, bgSurface1: formData.bgSurface1, bgSurface2: formData.bgSurface2, border: formData.border, darkPalette: darkPalette, palette: `${formData.bgBase},${formData.bgSurface1},${formData.bgSurface2},${formData.border},${formData.sidebar},${formData.accent},${formData.textBase},${formData.textSecondary}`, sidebar: formData.sidebar, textBase: formData.textBase, textSecondary: formData.textSecondary, }, }) .then((res) => { mutateUser((prevData) => { if (!prevData) return prevData; return { ...prevData, user: res }; }, false); applyTheme(formData.palette, darkPalette); setTheme("custom"); }) .catch((err) => console.log(err)); }; const handleUpdateTheme = async (formData: any) => { await handleFormSubmit({ ...formData, darkPalette }); reset({ ...defaultValues, }); }; useEffect(() => { reset({ ...defaultValues, ...preLoadedData, }); }, [preLoadedData, reset]); return (

Customize your theme

Background

Background surface 1

Background surface 2

Border

Sidebar

Accent

Text primary

Text secondary

{isSubmitting ? "Creating Theme..." : "Set Theme"}
); };