|
| 1 | +import { ChangeEvent, FormEvent, Fragment, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +// Redux |
| 4 | +import { useDispatch, useSelector } from 'react-redux'; |
| 5 | +import { bindActionCreators } from 'redux'; |
| 6 | +import { actionCreators } from '../../../store'; |
| 7 | + |
| 8 | +// Typescript |
| 9 | +import { Theme, ThemeSettingsForm } from '../../../interfaces'; |
| 10 | + |
| 11 | +// Components |
| 12 | +import { ThemePreview } from './ThemePreview'; |
| 13 | +import { Button, InputGroup, SettingsHeadline } from '../../UI'; |
| 14 | + |
| 15 | +// Other |
| 16 | +import classes from './Themer.module.css'; |
| 17 | +import { themes } from './themes.json'; |
| 18 | +import { State } from '../../../store/reducers'; |
| 19 | +import { inputHandler, themeSettingsTemplate } from '../../../utility'; |
| 20 | + |
| 21 | +export const Themer = (): JSX.Element => { |
| 22 | + const { |
| 23 | + auth: { isAuthenticated }, |
| 24 | + config: { loading, config }, |
| 25 | + } = useSelector((state: State) => state); |
| 26 | + |
| 27 | + const dispatch = useDispatch(); |
| 28 | + const { setTheme, updateConfig } = bindActionCreators( |
| 29 | + actionCreators, |
| 30 | + dispatch |
| 31 | + ); |
| 32 | + |
| 33 | + // Initial state |
| 34 | + const [formData, setFormData] = useState<ThemeSettingsForm>( |
| 35 | + themeSettingsTemplate |
| 36 | + ); |
| 37 | + |
| 38 | + // Get config |
| 39 | + useEffect(() => { |
| 40 | + setFormData({ |
| 41 | + ...config, |
| 42 | + }); |
| 43 | + }, [loading]); |
| 44 | + |
| 45 | + // Form handler |
| 46 | + const formSubmitHandler = async (e: FormEvent) => { |
| 47 | + e.preventDefault(); |
| 48 | + |
| 49 | + // Save settings |
| 50 | + await updateConfig(formData); |
| 51 | + }; |
| 52 | + |
| 53 | + // Input handler |
| 54 | + const inputChangeHandler = ( |
| 55 | + e: ChangeEvent<HTMLInputElement | HTMLSelectElement>, |
| 56 | + options?: { isNumber?: boolean; isBool?: boolean } |
| 57 | + ) => { |
| 58 | + inputHandler<ThemeSettingsForm>({ |
| 59 | + e, |
| 60 | + options, |
| 61 | + setStateHandler: setFormData, |
| 62 | + state: formData, |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <Fragment> |
| 68 | + <SettingsHeadline text="Set theme" /> |
| 69 | + <div className={classes.ThemerGrid}> |
| 70 | + {themes.map( |
| 71 | + (theme: Theme, idx: number): JSX.Element => ( |
| 72 | + <ThemePreview key={idx} theme={theme} applyTheme={setTheme} /> |
| 73 | + ) |
| 74 | + )} |
| 75 | + </div> |
| 76 | + |
| 77 | + {isAuthenticated && ( |
| 78 | + <form onSubmit={formSubmitHandler}> |
| 79 | + <SettingsHeadline text="Other settings" /> |
| 80 | + <InputGroup> |
| 81 | + <label htmlFor="defaultTheme">Default theme (for new users)</label> |
| 82 | + <select |
| 83 | + id="defaultTheme" |
| 84 | + name="defaultTheme" |
| 85 | + value={formData.defaultTheme} |
| 86 | + onChange={(e) => inputChangeHandler(e)} |
| 87 | + > |
| 88 | + {themes.map((theme: Theme, idx) => ( |
| 89 | + <option key={idx} value={theme.name}> |
| 90 | + {theme.name} |
| 91 | + </option> |
| 92 | + ))} |
| 93 | + </select> |
| 94 | + </InputGroup> |
| 95 | + |
| 96 | + <Button>Save changes</Button> |
| 97 | + </form> |
| 98 | + )} |
| 99 | + </Fragment> |
| 100 | + ); |
| 101 | +}; |
0 commit comments