11import { useMutation , useQuery } from "@apollo/client/react" ;
22import { Button , LinearProgress , Stack , Typography } from "@mui/material" ;
3- import { GET_THEME , MARK_DEFAULT_THEME , UPDATE_THEME } from "../../queries/themeQueries" ;
3+ import { GET_THEME , GET_THEMES , MARK_DEFAULT_THEME , UPDATE_THEME } from "../../queries/themeQueries" ;
44import { useNavigate , useParams } from "react-router-dom" ;
55import { ServerThemeData } from "../../types/site_settings/MakeTheme" ;
66import SaveIcon from '@mui/icons-material/Save' ;
77import NotInterestedIcon from '@mui/icons-material/NotInterested' ;
88import { toast } from "react-toastify" ;
99import { useMakeTheme } from "../../common/MakeThemeProvider" ;
10-
1110import StarIcon from '@mui/icons-material/Star' ;
1211import ThemeEditor from "./ThemeEditor" ;
12+ import { ComponentProps } from "react" ;
1313
1414export default function ManageThemePage ( ) {
1515 const { themeKey } = useParams < { themeKey : string } > ( ) ;
1616 const navigate = useNavigate ( ) ;
1717 const makeTheme = useMakeTheme ( ) ;
1818
19- const [ updateTheme ] = useMutation ( UPDATE_THEME , { refetchQueries : [ "GetThemes" ] , awaitRefetchQueries : true } ) ;
20- const [ markDefaultTheme ] = useMutation ( MARK_DEFAULT_THEME , { refetchQueries : [ "GetThemes" ] , awaitRefetchQueries : true } ) ;
19+ type ThemeEditorProps = ComponentProps < typeof ThemeEditor > ;
20+
21+ const [ updateTheme ] = useMutation ( UPDATE_THEME , { refetchQueries : [ { query : GET_THEMES , variables : { } } , { query : GET_THEME , variables : { key : themeKey } } ] , awaitRefetchQueries : true } ) ;
22+ const [ markDefaultTheme ] = useMutation ( MARK_DEFAULT_THEME , { refetchQueries : [ { query : GET_THEMES , variables : { } } , GET_THEME ] , awaitRefetchQueries : true } ) ;
2123
2224 const themeResult = useQuery ( GET_THEME , {
2325 variables : { key : themeKey } ,
@@ -28,15 +30,17 @@ export default function ManageThemePage() {
2830 muiThemeOptions : JSON . parse ( themeResult . data ?. getTheme . muiThemeOptions )
2931 } : undefined ;
3032
33+ const updatedTheme = currentTheme
34+
3135 async function handleUpdateTheme ( ) {
3236 try {
3337 await updateTheme ( {
3438 variables : {
3539 key : themeKey ,
36- themeName : themeName ,
37- title : siteTitle ,
38- muiThemeOptions : JSON . stringify ( muiThemeOptions ) ,
39- logo : logo
40+ themeName : updatedTheme . themeName ,
41+ title : updatedTheme . title ,
42+ muiThemeOptions : JSON . stringify ( updatedTheme . muiThemeOptions ) ,
43+ logo : updatedTheme . logo
4044 }
4145 } )
4246 toast . success ( `Updated Theme!` )
@@ -58,6 +62,36 @@ export default function ManageThemePage() {
5862 }
5963 }
6064
65+ async function themeChanged ( theme : ThemeEditorProps ) {
66+ updatedTheme . logo = theme . logo
67+ updatedTheme . muiThemeOptions = {
68+ palette : {
69+ primary : {
70+ main : theme . primary
71+ } ,
72+ secondary : {
73+ main : theme . secondary
74+ } ,
75+ error : {
76+ main : theme . error ,
77+ } ,
78+ warning : {
79+ main : theme . warning
80+ } ,
81+ info : {
82+ main : theme . info
83+ } ,
84+ success : {
85+ main : theme . success
86+ } ,
87+ mode : theme . mode == "light" ? "light" : "dark"
88+ }
89+ }
90+ updatedTheme . themeName = theme . themeName
91+ updatedTheme . title = theme . siteTitle
92+
93+ }
94+
6195 return (
6296 < Stack alignItems = { "center" } spacing = { 3 } >
6397 < title > { `Edit Theme | ${ makeTheme . title } ` } </ title >
@@ -94,8 +128,10 @@ export default function ManageThemePage() {
94128 </ Stack >
95129 { currentTheme
96130 ? < ThemeEditor
131+ onChange = { themeChanged }
132+ themeKey = { currentTheme . key }
97133 themeName = { currentTheme . themeName }
98- siteTitle = { currentTheme . themeName }
134+ siteTitle = { currentTheme . title }
99135 logo = { currentTheme . logo }
100136
101137 primary = { currentTheme . muiThemeOptions . palette ?. primary . main }
0 commit comments