-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFormConfig.js
More file actions
108 lines (98 loc) · 3 KB
/
FormConfig.js
File metadata and controls
108 lines (98 loc) · 3 KB
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
import yup from 'helpers/validation';
const formFieldsStepOne = ['name', 'body'];
const formFieldsStepTwo = [
'strategy',
'choices',
'tabOption',
'voteType',
'maxWeight',
'minBalance',
];
const formFieldsStepThree = ['startDate', 'endDate', 'startTime', 'endTime'];
const NAME_MAX_LENGTH = 128;
const StepOneSchema = yup.object().shape({
name: yup
.string()
.trim()
.required('Please enter a proposal title')
.max(NAME_MAX_LENGTH, 'The maximum length for title is 128 characters'),
body: yup.string().required('Please enter a proposal description'),
});
const StepTwoSchema = yup.object().shape({
strategy: yup.string().required('Please select a strategy'),
tabOption: yup.string().oneOf(['text-based', 'visual']),
choices: yup
.array()
.of(
yup.object({
value: yup.string().required('Please enter option value'),
choiceImgUrl: yup.string().nullable(),
})
)
.when('tabOption', {
is: 'visual',
then: yup.array().of(
yup.object({
value: yup.string().required('Please enter option value'),
choiceImgUrl: yup
.string()
.trim()
.url('Image option is not valid')
.required('Please upload an image'),
})
),
})
.when('voteType', (voteType, schema) => {
if (voteType === 'basic') return;
return voteType === 'single-choice'
? schema.min(2, 'Please add a choice, minimum amount is two')
: schema.min(3, 'Please add a choice, minimum amount is three');
})
.unique('value', 'Invalid duplicated option'),
quorum: yup
.string()
.trim()
.matches(/\s+$|^$|(^[0-9]+$)/, 'Quorum threshold must be a valid number')
.when('voteType', (voteType, schema) => {
if (voteType !== 'basic') return;
return schema.required('Quorum threshold is required');
}),
maxWeight: yup
.string()
.trim()
.matches(
/\s+$|^$|(^[0-9]+$)/,
'Proposal maximum weight must be a valid number'
),
minBalance: yup
.string()
.trim()
.matches(
/\s+$|^$|(^[0-9]+$)/,
'Proposal minimum balance must be a valid number'
),
});
const StepThreeSchema = yup.object().shape({
startDate: yup.date().required('Please provide a start date'),
startTime: yup.date().required('Please provide a start time'),
endDate: yup.date().required('Please provide an end date'),
endTime: yup.date().required('Please provide an end time'),
});
const initialValues = (fields = []) =>
Object.assign({}, ...fields.map((key) => ({ [key]: undefined })));
const stepOne = {
Schema: StepOneSchema,
initialValues: initialValues(formFieldsStepOne),
formFields: formFieldsStepOne,
};
const stepTwo = {
Schema: StepTwoSchema,
initialValues: initialValues(formFieldsStepTwo),
formFields: formFieldsStepTwo,
};
const stepThree = {
Schema: StepThreeSchema,
initialValues: initialValues(formFieldsStepThree),
formFields: formFieldsStepThree,
};
export { stepOne, stepTwo, stepThree, NAME_MAX_LENGTH };