2019-12-11 21:09:27 +01:00
|
|
|
// @flow
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import Button from 'component/button';
|
2019-12-13 20:47:50 +01:00
|
|
|
import { Form, FormField } from 'component/common/form';
|
2019-12-11 21:09:27 +01:00
|
|
|
|
|
|
|
type Props = {
|
2019-12-13 20:47:50 +01:00
|
|
|
update: ([string, string]) => void,
|
2019-12-11 21:09:27 +01:00
|
|
|
};
|
|
|
|
|
2019-12-13 20:47:50 +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])$'
|
|
|
|
);
|
2019-12-12 21:18:13 +01:00
|
|
|
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-13 20:47:50 +01:00
|
|
|
setValidServerString(
|
|
|
|
(VALID_IPADDRESS_REGEX.test(hostString) || VALID_HOSTNAME_REGEX.test(hostString)) &&
|
|
|
|
VALID_PORT_REGEX.test(portString)
|
|
|
|
);
|
2019-12-12 21:18:13 +01:00
|
|
|
}, [hostString, portString, validServerString, setValidServerString]);
|
2019-12-11 21:09:27 +01:00
|
|
|
|
2019-12-13 20:47:50 +01:00
|
|
|
function onSubmit() {
|
2019-12-11 21:09:27 +01:00
|
|
|
update([hostString, portString]);
|
|
|
|
setHostString('');
|
|
|
|
setPortString('');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-12-13 20:47:50 +01:00
|
|
|
<Form onSubmit={onSubmit}>
|
|
|
|
<div className="section__actions">
|
|
|
|
<FormField
|
|
|
|
type="text"
|
|
|
|
label={__('Host')}
|
|
|
|
placeholder={'code.freezepeach.fun'}
|
|
|
|
value={hostString}
|
|
|
|
onChange={e => setHostString(e.target.value)}
|
|
|
|
/>
|
|
|
|
<span className="form-field__conjuction">:</span>
|
|
|
|
<FormField
|
|
|
|
type="text"
|
|
|
|
label={__('Port')}
|
|
|
|
placeholder={'50001'}
|
|
|
|
value={portString}
|
|
|
|
onChange={e => setPortString(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button type="submit" button="primary" label={__('Add')} disabled={!validServerString} />
|
|
|
|
</div>
|
|
|
|
</Form>
|
2019-12-11 21:09:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ServerInputRow;
|