2019-12-11 21:09:27 +01:00
|
|
|
// @flow
|
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import { FormField } from 'component/common/form';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
update: (string) => void,
|
|
|
|
};
|
|
|
|
|
2019-12-12 21:18:13 +01:00
|
|
|
const VALID_IPADDRESS_REGEX = new RegExp('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\.)){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$');
|
|
|
|
const VALID_HOSTNAME_REGEX = new RegExp('^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])(\\.))+([A-Za-z]|[A-Za-z][A-Za-z]*[A-Za-z])$');
|
|
|
|
const VALID_PORT_REGEX = new RegExp('^([0-9]){1,5}$');
|
|
|
|
|
2019-12-11 21:09:27 +01:00
|
|
|
function ServerInputRow(props: Props) {
|
|
|
|
const { update } = props;
|
|
|
|
|
|
|
|
const [hostString, setHostString] = useState('');
|
|
|
|
const [portString, setPortString] = useState('');
|
2019-12-12 21:18:13 +01:00
|
|
|
const [validServerString, setValidServerString] = useState(false);
|
2019-12-11 21:09:27 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-12-12 21:18:13 +01:00
|
|
|
setValidServerString((VALID_IPADDRESS_REGEX.test(hostString) || VALID_HOSTNAME_REGEX.test(hostString)) && VALID_PORT_REGEX.test(portString));
|
|
|
|
}, [hostString, portString, validServerString, setValidServerString]);
|
2019-12-11 21:09:27 +01:00
|
|
|
|
|
|
|
function onClick() {
|
|
|
|
update([hostString, portString]);
|
|
|
|
setHostString('');
|
|
|
|
setPortString('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr>
|
2019-12-12 21:18:13 +01:00
|
|
|
<td>
|
|
|
|
<FormField type="text" value={hostString} onChange={e => setHostString(e.target.value)} />
|
2019-12-11 21:09:27 +01:00
|
|
|
</td>
|
2019-12-12 21:18:13 +01:00
|
|
|
<td>
|
|
|
|
<FormField type="text" value={portString} onChange={e => setPortString(e.target.value)} />
|
2019-12-11 21:09:27 +01:00
|
|
|
</td>
|
|
|
|
<td />
|
|
|
|
<td>
|
2019-12-12 21:18:13 +01:00
|
|
|
<Button button={'link'} icon={ICONS.ADD} disabled={!validServerString} onClick={onClick} />
|
2019-12-11 21:09:27 +01:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ServerInputRow;
|