index.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { useReducer, useEffect, useState, Fragment } from 'react'
  2. import { RouteComponentProps, useRouteMatch } from 'react-router'
  3. import { reducer, initialState, getLayersAction, updateLayerAction, saveLayersAction, Item } from './reducer'
  4. import VectorShow from '../../components/VectorShow'
  5. import Color from '../../components/Color'
  6. import styles from './style.module.css'
  7. import { useSelector } from 'react-redux'
  8. import { StoreState } from '../../store'
  9. import { Hots } from '../../store/hots'
  10. type Attr = 'lineColor' | 'lineWidth' | 'fillColor' | 'show'
  11. type EvAttr = 'value' | 'checked'
  12. function StyleEdit(props: RouteComponentProps) {
  13. const match = useRouteMatch()
  14. const sid = (match?.params as any).sid
  15. let [state, dispatch] = useReducer(reducer, initialState)
  16. let layers = Object.keys(state)
  17. let [layer, setLayer] = useState('')
  18. let params = props.match.params as any
  19. let style = (layer && state[layer]) as Item
  20. const hots = useSelector<StoreState, Hots>(state => state.hots)
  21. useEffect(() => { getLayersAction(dispatch, sid, params.id) }, [params.id, sid])
  22. useEffect(() => { layer || setLayer(layers[0]) }, [layer, layers])
  23. const changeHandle = (attr: Attr, evAttr: EvAttr = 'value', init = false) => (ev: any) => {
  24. updateLayerAction(dispatch, layer, { ...style, [attr]: init ? ev : ev.target[evAttr] })
  25. }
  26. const saveHandle = async () => {
  27. if (await saveLayersAction(style, sid, params.id)) {
  28. alert('成功发布')
  29. } else {
  30. alert('发布失败')
  31. }
  32. }
  33. const verctorInfo = style && { ...style, url: style.getUrl }
  34. return (
  35. <div className={styles.layout}>
  36. <div className={styles.filter}>
  37. <div className={styles.inputitem}>
  38. <label>图层选择</label>
  39. <select value={layer} onChange={ev => setLayer(ev.target.value)}>
  40. {layers.map(layer => <option value={layer} key={layer}>{layer}</option>)}
  41. </select>
  42. </div>
  43. {style && (
  44. <Fragment>
  45. <div className={styles.inputitem}>
  46. <label>填充颜色</label>
  47. <Color color={style.fillColor} onChange={changeHandle('fillColor', void 0, true)} />
  48. </div>
  49. <div className={styles.inputitem}>
  50. <label>线条颜色</label>
  51. <Color color={style.lineColor} onChange={changeHandle('lineColor', void 0, true)} />
  52. </div>
  53. <div className={styles.inputitem}>
  54. <label>线条厚度</label>
  55. <input type="number" value={style.lineWidth} onChange={changeHandle('lineWidth')} />
  56. </div>
  57. <div className={styles.inputitem}>
  58. <label>是否显示</label>
  59. <input type="checkbox" checked={style.show} onChange={changeHandle('show', 'checked')} />
  60. </div>
  61. <div className={styles.inputitem}>
  62. <button onClick={saveHandle} >发布</button>
  63. </div>
  64. </Fragment>
  65. )}
  66. </div>
  67. {style &&
  68. <div className={styles.content}>
  69. <VectorShow {...verctorInfo} height={2500} hots={hots} />
  70. </div>
  71. }
  72. </div>
  73. )
  74. }
  75. export default StyleEdit