mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-25 18:03:50 +00:00
[web] Add simple validation and improve UI.
This commit is contained in:
parent
ca8364404d
commit
80091c859a
@ -1,5 +1,8 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
import { connect } from 'react-redux'
|
||||||
|
import classnames from 'classnames'
|
||||||
|
import { update as updateOptions } from '../../ducks/options'
|
||||||
|
|
||||||
PureBooleanOption.PropTypes = {
|
PureBooleanOption.PropTypes = {
|
||||||
value: PropTypes.bool.isRequired,
|
value: PropTypes.bool.isRequired,
|
||||||
@ -7,16 +10,12 @@ PureBooleanOption.PropTypes = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function PureBooleanOption({ value, onChange, ...props}) {
|
function PureBooleanOption({ value, onChange, ...props}) {
|
||||||
let onFocus = () => { props.onFocus() },
|
let onMouseEnter = () => { props.onMouseEnter() },
|
||||||
onBlur = () => { props.onBlur() },
|
|
||||||
onMouseEnter = () => { props.onMouseEnter() },
|
|
||||||
onMouseLeave = () => { props.onMouseLeave() }
|
onMouseLeave = () => { props.onMouseLeave() }
|
||||||
return (
|
return (
|
||||||
<input type="checkbox"
|
<input type="checkbox"
|
||||||
checked={value}
|
checked={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
onFocus={onFocus}
|
|
||||||
onBlur={onBlur}
|
|
||||||
onMouseOver={onMouseEnter}
|
onMouseOver={onMouseEnter}
|
||||||
onMouseLeave={onMouseLeave}
|
onMouseLeave={onMouseLeave}
|
||||||
/>
|
/>
|
||||||
@ -35,8 +34,10 @@ function PureStringOption( { value, onChange, ...props }) {
|
|||||||
onMouseEnter = () => { props.onMouseEnter() },
|
onMouseEnter = () => { props.onMouseEnter() },
|
||||||
onMouseLeave = () => { props.onMouseLeave() }
|
onMouseLeave = () => { props.onMouseLeave() }
|
||||||
return (
|
return (
|
||||||
|
<div className={classnames('input-group', {'has-error': props.error})}>
|
||||||
<input type="text"
|
<input type="text"
|
||||||
value={value}
|
value={value}
|
||||||
|
className='form-control'
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
onKeyDown={onKeyDown}
|
onKeyDown={onKeyDown}
|
||||||
onFocus={onFocus}
|
onFocus={onFocus}
|
||||||
@ -44,6 +45,7 @@ function PureStringOption( { value, onChange, ...props }) {
|
|||||||
onMouseOver={onMouseEnter}
|
onMouseOver={onMouseEnter}
|
||||||
onMouseLeave={onMouseLeave}
|
onMouseLeave={onMouseLeave}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +63,7 @@ function PureNumberOption( {value, onChange, ...props }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<input type="number"
|
<input type="number"
|
||||||
|
className="form-control"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
onKeyDown={onKeyDown}
|
onKeyDown={onKeyDown}
|
||||||
@ -85,6 +88,7 @@ function PureChoicesOption( { value, onChange, name, choices, ...props}) {
|
|||||||
return (
|
return (
|
||||||
<select
|
<select
|
||||||
name={name}
|
name={name}
|
||||||
|
className="form-control"
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
selected={value}
|
selected={value}
|
||||||
onFocus={onFocus}
|
onFocus={onFocus}
|
||||||
@ -108,6 +112,8 @@ 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() {
|
||||||
@ -120,28 +126,41 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
onChange(e) {
|
onChange(e) {
|
||||||
const value = e.target.value.split("\n")
|
const value = e.target.value.split("\n")
|
||||||
console.log(value)
|
|
||||||
this.props.onChange(e)
|
this.props.onChange(e)
|
||||||
this.setState({ value })
|
this.setState({ value })
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {height, value} = this.state
|
const {height, value} = this.state
|
||||||
|
const {error} = this.props
|
||||||
return (
|
return (
|
||||||
|
<div className={classnames('input-group', {'has-error': error})}>
|
||||||
<textarea
|
<textarea
|
||||||
rows={height}
|
rows={height}
|
||||||
value={value}
|
value={value}
|
||||||
|
className="form-control"
|
||||||
onChange={this.onChange}
|
onChange={this.onChange}
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
onFocus={this.onFocus}
|
onFocus={this.onFocus}
|
||||||
onBlur={this.onBlur}
|
onBlur={this.onBlur}
|
||||||
|
onMouseEnter={this.onMouseEnter}
|
||||||
|
onMouseLeave={this.onMouseLeave}
|
||||||
/>
|
/>
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -154,16 +173,16 @@ const OptionTypes = {
|
|||||||
"sequence of str": PureStringSequenceOption,
|
"sequence of str": PureStringSequenceOption,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class OptionMaster extends Component {
|
class OptionMaster extends Component {
|
||||||
|
|
||||||
constructor(props, context) {
|
constructor(props, context) {
|
||||||
super(props, context)
|
super(props, context)
|
||||||
this.state = {
|
this.state = {
|
||||||
updateOptions: this.props.updateOptions,
|
|
||||||
option: this.props.option,
|
option: this.props.option,
|
||||||
name: this.props.name,
|
name: this.props.name,
|
||||||
mousefocus: false,
|
mousefocus: false,
|
||||||
focus: false,
|
focus: false,
|
||||||
|
error: false,
|
||||||
|
|
||||||
}
|
}
|
||||||
if (props.option.choices) {
|
if (props.option.choices) {
|
||||||
@ -179,11 +198,17 @@ export default class OptionMaster extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
this.setState({ option: nextProps.option })
|
if (nextProps.option.value !== this.state.option.value){
|
||||||
|
this.setState({ error: true })
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.setState({ option: nextProps.option, error: false })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onChange(e) {
|
onChange(e) {
|
||||||
const { updateOptions, option, name } = this.state
|
const { option, name } = this.state
|
||||||
|
this.setState({ option: {...option, value: e.target.value}})
|
||||||
switch (option.type) {
|
switch (option.type) {
|
||||||
case 'bool' :
|
case 'bool' :
|
||||||
updateOptions({[name]: !option.value})
|
updateOptions({[name]: !option.value})
|
||||||
@ -201,7 +226,6 @@ export default class OptionMaster extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMouseEnter() {
|
onMouseEnter() {
|
||||||
console.log(this.state)
|
|
||||||
this.setState({ mousefocus: true })
|
this.setState({ mousefocus: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +234,6 @@ export default class OptionMaster extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onFocus() {
|
onFocus() {
|
||||||
console.log(this.state)
|
|
||||||
this.setState({ focus: true })
|
this.setState({ focus: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +243,7 @@ export default class OptionMaster extends Component {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { name, children } = this.props
|
const { name, children } = this.props
|
||||||
const { option, focus, mousefocus } = this.state
|
const { option, focus, mousefocus, error } = this.state
|
||||||
const WrappedComponent = this.WrappedComponent
|
const WrappedComponent = this.WrappedComponent
|
||||||
return (
|
return (
|
||||||
<div className="row">
|
<div className="row">
|
||||||
@ -238,6 +261,7 @@ export default class OptionMaster extends Component {
|
|||||||
onBlur={this.onBlur}
|
onBlur={this.onBlur}
|
||||||
onMouseEnter={this.onMouseEnter}
|
onMouseEnter={this.onMouseEnter}
|
||||||
onMouseLeave={this.onMouseLeave}
|
onMouseLeave={this.onMouseLeave}
|
||||||
|
error={error}
|
||||||
/>
|
/>
|
||||||
{(focus || mousefocus) && (
|
{(focus || mousefocus) && (
|
||||||
<div className="tooltip tooltip-bottom" role="tooltip" style={{opacity: 1}}>
|
<div className="tooltip tooltip-bottom" role="tooltip" style={{opacity: 1}}>
|
||||||
@ -250,3 +274,10 @@ export default class OptionMaster extends Component {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
updateOptions: updateOptions
|
||||||
|
}
|
||||||
|
)(OptionMaster)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import { connect } from 'react-redux'
|
import { connect } from 'react-redux'
|
||||||
import * as modalAction from '../../ducks/ui/modal'
|
import * as modalAction from '../../ducks/ui/modal'
|
||||||
import { update as updateOptions } from '../../ducks/options'
|
|
||||||
import Option from './OptionMaster'
|
import Option from './OptionMaster'
|
||||||
|
|
||||||
class PureOptionModal extends Component {
|
class PureOptionModal extends Component {
|
||||||
@ -37,7 +36,6 @@ class PureOptionModal extends Component {
|
|||||||
<Option
|
<Option
|
||||||
key={index}
|
key={index}
|
||||||
name={key}
|
name={key}
|
||||||
updateOptions={updateOptions}
|
|
||||||
option={option}
|
option={option}
|
||||||
/>)
|
/>)
|
||||||
})
|
})
|
||||||
@ -58,6 +56,5 @@ export default connect(
|
|||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
hideModal: modalAction.hideModal,
|
hideModal: modalAction.hideModal,
|
||||||
updateOptions: updateOptions,
|
|
||||||
}
|
}
|
||||||
)(PureOptionModal)
|
)(PureOptionModal)
|
||||||
|
@ -4,6 +4,7 @@ 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 = {
|
||||||
|
|
||||||
@ -21,12 +22,26 @@ 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) {
|
||||||
fetchApi.put('/options', options)
|
let error = ''
|
||||||
return { type: REQUEST_UPDATE }
|
fetchApi.put('/options', options).then(
|
||||||
|
(response) => {
|
||||||
|
response.text().then(errorMsg => {
|
||||||
|
error = errorMsg
|
||||||
|
console.log(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return {type: ERROR, error}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user