From 91ecdaa5dd1a65ff0ae944896945c0fe4bc23574 Mon Sep 17 00:00:00 2001 From: haishanh Date: Mon, 5 Nov 2018 18:32:16 +0800 Subject: update: convert more components to function ones includes Input, Config, SideBard. also removed react-redux Provider --- src/components/Config.js | 94 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 20 deletions(-) (limited to 'src/components/Config.js') diff --git a/src/components/Config.js b/src/components/Config.js index ca6f9e4..e8fbfba 100644 --- a/src/components/Config.js +++ b/src/components/Config.js @@ -1,4 +1,5 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; +import PropTypes from 'prop-types'; import { useComponentState, useActions } from 'm/store'; import { getConfigs, fetchConfigs, updateConfigs } from 'd/configs'; @@ -50,30 +51,76 @@ const actions = { const mapStateToProps = s => ({ configs: getConfigs(s) }); -export default function Config2() { - const { fetchConfigs, updateConfigs } = useActions(actions); +export default function ConfigContainer() { + const { fetchConfigs } = useActions(actions); const { configs } = useComponentState(mapStateToProps); useEffect(() => { fetchConfigs(); }, []); + return ; +} - function handleInputOnChange(ev) { - const target = ev.target; - const { name } = target; +function Config({ configs }) { + const { updateConfigs } = useActions(actions); + // configState to track component internal state + // prevConfigs to track external props.configs + const [configState, _setConfigState] = useState(configs); + const [prevConfigs, setPrevConfigs] = useState(configs); + // equivalent of getDerivedStateFromProps + if (prevConfigs !== configs) { + setPrevConfigs(configs); + _setConfigState(configs); + } - let value; - switch (target.type) { - case 'checkbox': + const setConfigState = (name, val) => { + _setConfigState({ + ...configState, + [name]: val + }); + }; + + function handleInputOnChange(e) { + const target = e.target; + const { name } = target; + let { value } = target; + switch (target.name) { + case 'allow-lan': value = target.checked; + setConfigState(name, value); + updateConfigs({ [name]: value }); + break; + case 'mode': + case 'log-level': + setConfigState(name, value); + updateConfigs({ [name]: value }); break; - case 'number': - value = Number(target.value); + case 'redir-port': + case 'socket-port': + case 'port': + if (target.value !== '') { + const num = parseInt(target.value, 10); + if (num < 0 || num > 65535) return; + } + setConfigState(name, value); break; default: - value = target.value; + return; + } + } + + function handleInputOnBlur(e) { + const target = e.target; + const { name, value } = target; + switch (name) { + case 'port': + case 'socket-port': + case 'redir-port': { + const num = parseInt(value, 10); + if (num < 0 || num > 65535) return; + updateConfigs({ [name]: num }); + break; + } } - if (configs[name] === value) return; - updateConfigs({ [name]: value }); } return ( @@ -84,8 +131,9 @@ export default function Config2() {
HTTP Proxy Port
@@ -93,8 +141,9 @@ export default function Config2() {
SOCKS5 Proxy Port
@@ -102,8 +151,9 @@ export default function Config2() {
Redir Port
@@ -111,7 +161,7 @@ export default function Config2() {
Allow LAN
@@ -121,7 +171,7 @@ export default function Config2() { @@ -131,7 +181,7 @@ export default function Config2() { @@ -139,3 +189,7 @@ export default function Config2() { ); } + +Config.propTypes = { + configs: PropTypes.object +}; -- cgit v1.3.1