[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, useState, useEffect } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import capitalize from 'lodash/capitalize'
  4. import { useTheme } from '../../hooks'
  5. import { setFieldValue } from '../../actions/forms'
  6. import { setTheme } from '../../actions/theme'
  7. import { getFieldValue } from '../../selectors/forms'
  8. import { AppState, Theme, ColorScheme } from '../../types'
  9. import FieldLabel from '../../components/controls/field-label'
  10. import themes from '../../themes'
  11. export interface Props {
  12. name: string
  13. label: string
  14. }
  15. const ThemeField: FC<Props> = ({ name, label }) => {
  16. const theme = useTheme()
  17. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
  18. const [displayedName, setDisplayedName] = useState(value)
  19. const dispatch = useDispatch()
  20. const themeList = Object.entries(themes).map(([name, schemes]) => {
  21. return [name, schemes[ColorScheme.Light]] as [string, Theme]
  22. })
  23. const handleMouseEnter = (name: string) => {
  24. dispatch(setTheme(name))
  25. setDisplayedName(name)
  26. }
  27. const handleMouseLeave = () => {
  28. dispatch(setTheme(value))
  29. setDisplayedName(value)
  30. }
  31. useEffect(() => {
  32. setDisplayedName(value)
  33. }, [value])
  34. return (
  35. <div className="field">
  36. <FieldLabel>{label}</FieldLabel>
  37. <div className="control">
  38. <div className="theme-picker">
  39. {themeList.map(([themeName, t]) => (
  40. <div
  41. style={{ backgroundColor: t.primary, borderColor: themeName === value ? t.red : '#222' }}
  42. onMouseEnter={() => handleMouseEnter(themeName)}
  43. onMouseLeave={() => handleMouseLeave()}
  44. onClick={() => dispatch(setFieldValue(name, themeName))}></div>
  45. ))}
  46. </div>
  47. <div style={{ color: theme.primary }}>{capitalize(displayedName)}</div>
  48. </div>
  49. </div>
  50. )
  51. }
  52. export default ThemeField