[web] Update OptionMaster.

This commit is contained in:
Matthew Shao 2017-07-17 15:32:52 +08:00
parent c19a184ba2
commit 4a02822b34
2 changed files with 44 additions and 73 deletions

View File

@ -10,14 +10,12 @@ PureBooleanOption.PropTypes = {
} }
function PureBooleanOption({ value, onChange, ...props}) { function PureBooleanOption({ value, onChange, ...props}) {
let onMouseEnter = () => { props.onMouseEnter() },
onMouseLeave = () => { props.onMouseLeave() }
return ( return (
<input type="checkbox" <input type="checkbox"
checked={value} checked={value}
onChange={onChange} onChange={onChange}
onMouseOver={onMouseEnter} onMouseOver={props.onMouseEnter}
onMouseLeave={onMouseLeave} onMouseLeave={props.onMouseLeave}
/> />
) )
} }
@ -28,11 +26,7 @@ PureStringOption.PropTypes = {
} }
function PureStringOption( { value, onChange, ...props }) { function PureStringOption( { value, onChange, ...props }) {
let onKeyDown = (e) => {e.stopPropagation()}, let onKeyDown = (e) => {e.stopPropagation()}
onFocus = () => { props.onFocus() },
onBlur = () => { props.onBlur() },
onMouseEnter = () => { props.onMouseEnter() },
onMouseLeave = () => { props.onMouseLeave() }
return ( return (
<div className={classnames('input-group', {'has-error': props.error})}> <div className={classnames('input-group', {'has-error': props.error})}>
<input type="text" <input type="text"
@ -40,10 +34,10 @@ function PureStringOption( { value, onChange, ...props }) {
className='form-control' className='form-control'
onChange={onChange} onChange={onChange}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
onFocus={onFocus} onFocus={props.onFocus}
onBlur={onBlur} onBlur={props.onBlur}
onMouseOver={onMouseEnter} onMouseOver={props.onMouseEnter}
onMouseLeave={onMouseLeave} onMouseLeave={props.onMouseLeave}
/> />
</div> </div>
) )
@ -55,11 +49,7 @@ PureNumberOption.PropTypes = {
} }
function PureNumberOption( {value, onChange, ...props }) { function PureNumberOption( {value, onChange, ...props }) {
let onKeyDown = (e) => {e.stopPropagation()}, let onKeyDown = (e) => {e.stopPropagation()}
onFocus = () => { props.onFocus() },
onBlur = () => { props.onBlur() },
onMouseEnter = () => { props.onMouseEnter() },
onMouseLeave = () => { props.onMouseLeave() }
return ( return (
<input type="number" <input type="number"
@ -67,10 +57,10 @@ function PureNumberOption( {value, onChange, ...props }) {
value={value} value={value}
onChange={onChange} onChange={onChange}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
onFocus={onFocus} onFocus={props.onFocus}
onBlur={onBlur} onBlur={props.onBlur}
onMouseOver={onMouseEnter} onMouseOver={props.onMouseEnter}
onMouseLeave={onMouseLeave} onMouseLeave={props.onMouseLeave}
/> />
) )
} }
@ -81,20 +71,16 @@ PureChoicesOption.PropTypes = {
} }
function PureChoicesOption( { value, onChange, name, choices, ...props}) { function PureChoicesOption( { value, onChange, name, choices, ...props}) {
let onFocus = () => { props.onFocus() },
onBlur = () => { props.onBlur() },
onMouseEnter = () => { props.onMouseEnter() },
onMouseLeave = () => { props.onMouseLeave() }
return ( return (
<select <select
name={name} name={name}
className="form-control" className="form-control"
onChange={onChange} onChange={onChange}
selected={value} selected={value}
onFocus={onFocus} onFocus={props.onFocus}
onBlur={onBlur} onBlur={props.onBlur}
onMouseOver={onMouseEnter} onMouseOver={props.onMouseEnter}
onMouseLeave={onMouseLeave} onMouseLeave={props.onMouseLeave}
> >
{ choices.map((choice, index) => ( { choices.map((choice, index) => (
<option key={index} value={choice}> {choice} </option> <option key={index} value={choice}> {choice} </option>
@ -112,8 +98,6 @@ class PureStringSequenceOption extends Component {
this.onBlur = this.onBlur.bind(this) this.onBlur = this.onBlur.bind(this)
this.onKeyDown = this.onKeyDown.bind(this) this.onKeyDown = this.onKeyDown.bind(this)
this.onChange = this.onChange.bind(this) this.onChange = this.onChange.bind(this)
this.onMouseEnter = this.onMouseEnter.bind(this)
this.onMouseLeave = this.onMouseLeave.bind(this)
} }
onFocus() { onFocus() {
@ -126,14 +110,6 @@ class PureStringSequenceOption extends Component {
this.props.onBlur() this.props.onBlur()
} }
onMouseEnter() {
this.props.onMouseEnter()
}
onMouseLeave() {
this.props.onMouseLeave()
}
onKeyDown(e) { onKeyDown(e) {
e.stopPropagation() e.stopPropagation()
} }
@ -146,7 +122,7 @@ class PureStringSequenceOption extends Component {
render() { render() {
const {height, value} = this.state const {height, value} = this.state
const {error} = this.props const {error, onMouseEnter, onMouseLeave} = this.props
return ( return (
<div className={classnames('input-group', {'has-error': error})}> <div className={classnames('input-group', {'has-error': error})}>
<textarea <textarea
@ -157,8 +133,8 @@ class PureStringSequenceOption extends Component {
onKeyDown={this.onKeyDown} onKeyDown={this.onKeyDown}
onFocus={this.onFocus} onFocus={this.onFocus}
onBlur={this.onBlur} onBlur={this.onBlur}
onMouseEnter={this.onMouseEnter} onMouseEnter={onMouseEnter}
onMouseLeave={this.onMouseLeave} onMouseLeave={onMouseLeave}
/> />
</div> </div>
) )
@ -183,7 +159,6 @@ class OptionMaster extends Component {
mousefocus: false, mousefocus: false,
focus: false, focus: false,
error: false, error: false,
} }
if (props.option.choices) { if (props.option.choices) {
this.WrappedComponent = PureChoicesOption this.WrappedComponent = PureChoicesOption
@ -198,17 +173,12 @@ class OptionMaster extends Component {
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (nextProps.option.value !== this.state.option.value){ this.setState({ option: nextProps.option })
this.setState({ error: true })
}
else{
this.setState({ option: nextProps.option, error: false })
}
} }
onChange(e) { onChange(e) {
const { option, name } = this.state const { option, name } = this.state
this.setState({ option: {...option, value: e.target.value}}) const { updateOptions } = this.props
switch (option.type) { switch (option.type) {
case 'bool' : case 'bool' :
updateOptions({[name]: !option.value}) updateOptions({[name]: !option.value})
@ -242,9 +212,11 @@ class OptionMaster extends Component {
} }
render() { render() {
const { name, children } = this.props const { name, children, client_options } = this.props
const { option, focus, mousefocus, error } = this.state const { option, focus, mousefocus } = this.state
const WrappedComponent = this.WrappedComponent const WrappedComponent = this.WrappedComponent
let error = (name in client_options) ? client_options[name].error : false,
value = (name in client_options) ? client_options[name].value : option.value
return ( return (
<div className="row"> <div className="row">
<div className="col-sm-8"> <div className="col-sm-8">
@ -253,7 +225,7 @@ class OptionMaster extends Component {
<div className="col-sm-4"> <div className="col-sm-4">
<WrappedComponent <WrappedComponent
children={children} children={children}
value={option.value} value={value}
onChange={this.onChange} onChange={this.onChange}
name={name} name={name}
choices={option.choices} choices={option.choices}
@ -276,8 +248,10 @@ class OptionMaster extends Component {
} }
export default connect( export default connect(
null, state => ({
client_options: state.ui.option
}),
{ {
updateOptions: updateOptions updateOptions
} }
)(OptionMaster) )(OptionMaster)

View File

@ -1,10 +1,10 @@
import { fetchApi } from '../utils' import { fetchApi } from '../utils'
import * as optionActions from './ui/option'
export const RECEIVE = 'OPTIONS_RECEIVE' export const RECEIVE = 'OPTIONS_RECEIVE'
export const UPDATE = 'OPTIONS_UPDATE' export const UPDATE = 'OPTIONS_UPDATE'
export const REQUEST_UPDATE = 'REQUEST_UPDATE' export const REQUEST_UPDATE = 'REQUEST_UPDATE'
export const UNKNOWN_CMD = 'OPTIONS_UNKNOWN_CMD' export const UNKNOWN_CMD = 'OPTIONS_UNKNOWN_CMD'
export const ERROR = 'OPTIONS_ERROR'
const defaultState = { const defaultState = {
@ -22,26 +22,23 @@ export default function reducer(state = defaultState, action) {
...action.data, ...action.data,
} }
case ERROR:
return {
...state,
...action.data,
}
default: default:
return state return state
} }
} }
export function update(options) { export function update(options) {
let error = '' return dispatch => {
fetchApi.put('/options', options).then( let option = Object.keys(options)[0]
(response) => { dispatch({ type: optionActions.OPTION_UPDATE_START, option, value: options[option] })
response.text().then(errorMsg => { fetchApi.put('/options', options).then(response => {
error = errorMsg if (response.status === 200) {
console.log(error) dispatch({ type: optionActions.OPTION_UPDATE_SUCCESS, option})
} else {
response.text().then( text => {
dispatch({type: optionActions.OPTION_UPDATE_ERROR, error: text, option})
})
}
}) })
} }
)
return {type: ERROR, error}
} }