From e18fe6d21b85f365c4be29ccddfa5b9a162bd7cb Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Thu, 2 Nov 2023 17:21:37 +0100 Subject: [PATCH] =?UTF-8?q?fixed=20issue=20with=20jsonConfig=20sendTo=20?= =?UTF-8?q?=20with=20=20native=20option=20when=20returnin=E2=80=A6=20(#220?= =?UTF-8?q?2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …g multiple properties - closes #2196 --- README.md | 1 + .../JsonConfigComponent/ConfigGeneric.tsx | 2 +- .../{ConfigSendto.jsx => ConfigSendto.tsx} | 82 +++++++++++-------- 3 files changed, 48 insertions(+), 37 deletions(-) rename src/src/components/JsonConfigComponent/{ConfigSendto.jsx => ConfigSendto.tsx} (84%) diff --git a/README.md b/README.md index 0bd2cb4ba..d14ca2080 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ The icons may not be reused in other projects without the proper flaticon licens --> ## Changelog ### **WORK IN PROGRESS** +* (foxriver76) fixed issue with jsonConfig `sendTo` with `native` option when returning multiple properties * (foxriver76) fixed crash case when schema cannot be read * (klein0r) Fixed noDelete attribute of JSON config accordion diff --git a/src/src/components/JsonConfigComponent/ConfigGeneric.tsx b/src/src/components/JsonConfigComponent/ConfigGeneric.tsx index 653605bb4..343305cf9 100644 --- a/src/src/components/JsonConfigComponent/ConfigGeneric.tsx +++ b/src/src/components/JsonConfigComponent/ConfigGeneric.tsx @@ -349,7 +349,7 @@ export default class ConfigGeneric ({ fullWidth: { @@ -28,23 +28,23 @@ const styles = () => ({ }, }); -function ip2int(ip) { +function ip2int(ip: string) { // eslint-disable-next-line no-bitwise return ip.split('.').reduce((ipInt, octet) => (ipInt << 8) + parseInt(octet, 10), 0) >>> 0; } // copied from iobroker.admin/src-rx/src/Utils.js -function findNetworkAddressOfHost(obj, localIp) { +function findNetworkAddressOfHost(obj: Record, localIp: string) { const networkInterfaces = obj?.native?.hardware?.networkInterfaces; if (!networkInterfaces) { return null; } - let hostIp; + let hostIp: string | undefined; // check ipv4 addresses Object.keys(networkInterfaces).forEach(inter => - networkInterfaces[inter].forEach(ip => { + networkInterfaces[inter].forEach((ip: Record) => { if (ip.internal) { return; } @@ -71,7 +71,7 @@ function findNetworkAddressOfHost(obj, localIp) { // check ipv6 addresses if (!hostIp) { Object.keys(networkInterfaces).forEach(inter => - networkInterfaces[inter].forEach(ip => { + networkInterfaces[inter].forEach((ip: Record) => { if (ip.internal) { return; } if (localIp.includes(':') && ip.family !== 'IPv6') { @@ -95,7 +95,7 @@ function findNetworkAddressOfHost(obj, localIp) { if (!hostIp) { Object.keys(networkInterfaces).forEach(inter => { - networkInterfaces[inter].forEach(ip => { + networkInterfaces[inter].forEach((ip: Record) => { if (ip.internal) { return; } if (localIp.includes(':') && ip.family !== 'IPv6') { @@ -114,7 +114,7 @@ function findNetworkAddressOfHost(obj, localIp) { if (!hostIp) { Object.keys(networkInterfaces).forEach(inter => { - networkInterfaces[inter].forEach(ip => { + networkInterfaces[inter].forEach((ip: Record) => { if (ip.internal) { return; } @@ -126,7 +126,29 @@ function findNetworkAddressOfHost(obj, localIp) { return hostIp; } -class ConfigSendto extends ConfigGeneric { +interface ConfigSendToProps extends ConfigGenericProps { + socket: AdminConnection; + themeType: string; + themeName: string; + style: Record; + className: string; + data: Record; + schema: Record; + adapterName: string; + instance:number; + commandRunning: boolean; + onCommandRunning: (running: boolean) => void; + classes: Record; +} + +interface ConfigSendToState extends ConfigGenericState { + _error: string; + _message: string; + hostname: string; + running?: boolean; +} + +class ConfigSendto extends ConfigGeneric { async componentDidMount() { super.componentDidMount(); @@ -150,6 +172,7 @@ class ConfigSendto extends ConfigGeneric { renderErrorDialog() { if (this.state._error) { + // @ts-expect-error classes should be optional in adapter-react return this.setState({ _error: '' })} />; } return null; @@ -157,7 +180,7 @@ class ConfigSendto extends ConfigGeneric { renderMessageDialog() { if (this.state._message) { - return this.setState({ _message: '' })} />; + return this.setState({ _message: '' })} />; } return null; } @@ -191,7 +214,7 @@ class ConfigSendto extends ConfigGeneric { _originIp, }; } - let timeout; + let timeout: ReturnType | undefined; if (this.props.schema.timeout) { timeout = setTimeout(() => { this.props.onCommandRunning(false); @@ -204,16 +227,16 @@ class ConfigSendto extends ConfigGeneric { this.props.schema.command || 'send', data, ) - .then(response => { + .then(async (response: Record) => { if (timeout) { clearTimeout(timeout); - timeout = null; + timeout = undefined; } if (response?.error) { if (this.props.schema.error && this.props.schema.error[response.error]) { let error = this.getText(this.props.schema.error[response.error]); if (response.args) { - response.args.forEach(arg => error = error.replace('%s', arg)); + response.args.forEach((arg: string) => error = error.replace('%s', arg)); } this.setState({ _error: error }); } else { @@ -227,15 +250,17 @@ class ConfigSendto extends ConfigGeneric { } else if (response?.result && this.props.schema.result && this.props.schema.result[response.result]) { let text = this.getText(this.props.schema.result[response.result]); if (response.args) { - response.args.forEach(arg => text = text.replace('%s', arg)); + response.args.forEach((arg: string) => text = text.replace('%s', arg)); } window.alert(text); } if (response?.native && this.props.schema.useNative) { const attrs = Object.keys(response.native); - attrs.forEach(attr => - this.onChange(attr, response.native[attr])); + for (const attr of attrs) { + await this.onChange(attr, response.native[attr]); + } + setTimeout(() => this.props.forceUpdate(attrs, this.props.data), 300); } else if (response?.result) { window.alert(typeof response.result === 'object' ? JSON.stringify(response.result) : response.result); @@ -244,11 +269,12 @@ class ConfigSendto extends ConfigGeneric { } if (response?.saveConfig) { + // @ts-expect-error 4 values intended? this.props.onChange(null, null, null, true); } } }) - .catch(e => { + .catch((e: any) => { if (this.props.schema.error && this.props.schema.error[e.toString()]) { this.setState({ _error: this.getText(this.props.schema.error[e.toString()]) }); } else { @@ -287,7 +313,7 @@ class ConfigSendto extends ConfigGeneric { />; } - renderItem(error, disabled /* , defaultValue */) { + renderItem(error: Error | undefined, disabled: boolean /* , defaultValue */) { const icon = this.getIcon(); return
@@ -315,20 +341,4 @@ class ConfigSendto extends ConfigGeneric { } } -ConfigSendto.propTypes = { - socket: PropTypes.object.isRequired, - themeType: PropTypes.string, - themeName: PropTypes.string, - style: PropTypes.object, - className: PropTypes.string, - data: PropTypes.object.isRequired, - schema: PropTypes.object, - onError: PropTypes.func, - onChange: PropTypes.func, - adapterName: PropTypes.string, - instance: PropTypes.number, - commandRunning: PropTypes.bool, - onCommandRunning: PropTypes.func, -}; - export default withStyles(styles)(ConfigSendto);