/
index.js
129 lines (112 loc) · 3.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import * as _ from 'lodash'
import * as Yup from 'yup'
import { useFormik } from 'formik'
import { useNavigate } from '@reach/router'
import { useMutation } from 'react-apollo'
import { CREATE_HOST } from '_graphql/actions/hosts'
import Flex from '_components/flex'
import Box from '_components/box'
import Link from '_components/link'
import Text from '_components/text'
import Button from '_components/button'
import TextInput from '_components/inputs/text-input'
import { Dragon as Spinner } from '_components/spinner'
import toast from '_components/toast'
import { withValidityIndicator } from '_hocs/'
import FormikState from '_utils/formik-utils'
import TagsInput from '_components/inputs/tags-input'
const Container = styled(Flex)`
flex-direction: column;
`
const NewHostView = ({ networkId }) => {
const navigate = useNavigate()
const formik = useFormik({
initialValues: {
name: null,
labels: [],
advertiseAddress: null,
},
validationSchema: Yup.object().shape({
name: Yup.string()
.required()
.nullable(),
advertiseAddress: Yup.string().nullable(),
}),
})
const [createHost, createHostMutation] = useMutation(CREATE_HOST, {
variables: { networkId },
onCompleted: () => {
toast.success('Host created')
navigate('/ui/hosts', { replace: true })
},
onError: () => {
toast.error('Error creating host')
navigate('/ui/hosts', { replace: true })
},
})
const handleSaveButtonClick = () => {
formik.validateForm().then(errors => {
if (_.isEmpty(errors)) {
createHost({ variables: { ...formik.values } })
} else {
toast.error('Form has errors')
}
})
}
const isLoading = createHostMutation.loading
if (isLoading) {
return <Spinner />
}
return (
<Container>
<Text textStyle="title" mb={4}>
New host
</Text>
<Box flexDirection="column">
<form>
<Text my={3}>Name</Text>
{withValidityIndicator(
<TextInput name="name" {...formik.getFieldProps('name')} placeholder="host-1" />,
formik.errors.name
)}
<Text my={3}>Labels</Text>
{withValidityIndicator(
<TagsInput
id="labels"
name="labels"
value={formik.values.labels.map(el => ({ value: el, label: el }))}
onChange={values => {
formik.setFieldValue('labels', values !== null ? values.map(el => el.value) : [])
}}
placeholder="Add label and hit Enter"
/>,
formik.errors.labels
)}
<Text my={3}>Advertise address</Text>
{withValidityIndicator(
<TextInput
name="advertiseAddress"
{...formik.getFieldProps('advertiseAddress')}
placeholder="wireguard.domain.io"
/>,
formik.errors.advertiseAddress
)}
<Button width="100%" borderRadius={3} mt={3} mb={3} onClick={handleSaveButtonClick}>
Save
</Button>
</form>
<Link mx="auto" to="/ui/hosts">
Cancel
</Link>
<FormikState {...formik} />
</Box>
</Container>
)
}
NewHostView.propTypes = {
networkId: PropTypes.string.isRequired,
}
export default NewHostView