Completed
Push — master ( dab75a...25c67f )
by Alejandro
13s queued 11s
created

src/servers/helpers/ServerForm.js

Complexity

Total Complexity 0
Complexity/F 0

Size

Lines of Code 42
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 0
eloc 36
mnd 0
bc 0
fnc 0
dl 0
loc 42
ccs 14
cts 14
cp 1
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useEffect, useState } from 'react';
2
import PropTypes from 'prop-types';
3
import { HorizontalFormGroup } from '../../utils/HorizontalFormGroup';
4
5 3
const propTypes = {
6
  onSubmit: PropTypes.func.isRequired,
7
  initialValues: PropTypes.shape({
8
    name: PropTypes.string.isRequired,
9
    url: PropTypes.string.isRequired,
10
    apiKey: PropTypes.string.isRequired,
11
  }),
12
  children: PropTypes.node.isRequired,
13
};
14
15 3
export const ServerForm = ({ onSubmit, initialValues, children }) => {
16 6
  const [ name, setName ] = useState('');
17 6
  const [ url, setUrl ] = useState('');
18 6
  const [ apiKey, setApiKey ] = useState('');
19 6
  const handleSubmit = (e) => {
20 2
    e.preventDefault();
21 2
    onSubmit({ name, url, apiKey });
22
  };
23
24 6
  useEffect(() => {
25 2
    initialValues && setName(initialValues.name);
26 2
    initialValues && setUrl(initialValues.url);
27 2
    initialValues && setApiKey(initialValues.apiKey);
28
  }, [ initialValues ]);
29
30 6
  return (
31
    <form onSubmit={handleSubmit}>
32
      <HorizontalFormGroup value={name} onChange={setName}>Name</HorizontalFormGroup>
33
      <HorizontalFormGroup type="url" value={url} onChange={setUrl}>URL</HorizontalFormGroup>
34
      <HorizontalFormGroup value={apiKey} onChange={setApiKey}>API key</HorizontalFormGroup>
35
36
      <div className="text-right">{children}</div>
37
    </form>
38
  );
39
};
40
41
ServerForm.propTypes = propTypes;
42