-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathuseForm.ts
108 lines (98 loc) · 2.92 KB
/
useForm.ts
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
/*--------------------------------------------------------------------------
* Copyright (c) 2019-2021, Postgres.ai, Nikolay Samokhvalov nik@postgres.ai
* All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited
*--------------------------------------------------------------------------
*/
import { useFormik } from 'formik'
import * as Yup from 'yup'
export type FormValues = {
debug: boolean
dockerImage: string
dockerTag: string
dockerPath: string
dockerImageType: string
sharedBuffers: string
sharedPreloadLibraries: string
tuningParams: string
timetable: string
dbname: string
host: string
port: string
username: string
password: string
databases: string
dumpParallelJobs: string
dumpIgnoreErrors: boolean
restoreParallelJobs: string
restoreIgnoreErrors: boolean
pgDumpCustomOptions: string
pgRestoreCustomOptions: string
}
const Schema = Yup.object().shape({
dockerImage: Yup.string().required('Docker image is required'),
dbname: Yup.string().required('Dbname is required'),
host: Yup.string().required('Host is required'),
port: Yup.string().required('Port is required'),
username: Yup.string().required('Username is required'),
})
export const useForm = (onSubmit: (values: FormValues) => void) => {
const formik = useFormik<FormValues>({
initialValues: {
debug: false,
dockerImage: '',
dockerTag: '',
dockerPath: '',
dockerImageType: '',
sharedBuffers: '',
sharedPreloadLibraries: '',
tuningParams: '',
timetable: '',
dbname: '',
host: '',
port: '',
username: '',
password: '',
databases: '',
dumpParallelJobs: '',
restoreParallelJobs: '',
pgDumpCustomOptions: '',
pgRestoreCustomOptions: '',
dumpIgnoreErrors: false,
restoreIgnoreErrors: false,
},
validationSchema: Schema,
onSubmit,
validateOnBlur: false,
validateOnChange: false,
})
const formatDatabaseArray = (database: string) => {
let databases = []
const splitDatabaseArray = database.split(/[,(\s)(\n)(\r)(\t)(\r\n)]/)
for (let i = 0; i < splitDatabaseArray.length; i++) {
if (splitDatabaseArray[i] !== '') {
databases.push(splitDatabaseArray[i])
}
}
return databases
}
const connectionData = {
host: formik.values.host,
port: formik.values.port,
username: formik.values.username,
password: formik.values.password,
dbname: formik.values.dbname,
...(formik.values.databases && {
db_list: formatDatabaseArray(formik.values.databases),
}),
...(formik.values.dockerImageType === 'custom' && {
dockerImage: formik.values.dockerImage,
}),
}
const isConnectionDataValid =
formik.values.host &&
formik.values.port &&
formik.values.username &&
formik.values.dbname
return [{ formik, connectionData, isConnectionDataValid }]
}