fix: forms when editing triggers

This commit is contained in:
José Oliveira 2021-03-15 21:00:24 +00:00 committed by Josh Harvey
parent 04c8213430
commit fd582990ef

View file

@ -85,40 +85,58 @@ const useStyles = makeStyles({
}) })
// const direction = Yup.string().required() // const direction = Yup.string().required()
const triggerType = Yup.string().required() const triggerType = Yup.string().required()
const threshold = Yup.object().shape({ const threshold = Yup.object().shape({
threshold: Yup.number(), threshold: Yup.number()
thresholdDays: Yup.number().test({ .nullable()
test(val) { .transform(transformNumber)
const { triggerType } = this.parent .label('Invalid threshold'),
const requireThrehsold = ['txVolume', 'txVelocity', 'consecutiveDays'] thresholdDays: Yup.number()
.transform(transformNumber)
if (R.isEmpty(val) && R.includes(triggerType, requireThrehsold)) { .nullable()
return this.createError() .label('Invalid threshold days')
}
return true
}
})
}) })
const requirement = Yup.object().shape({ const requirement = Yup.object().shape({
requirement: Yup.string().required(), requirement: Yup.string().required(),
suspensionDays: Yup.number().when('requirement', { suspensionDays: Yup.number().when('requirement', {
is: 'suspend', is: 'suspend',
then: Yup.number().required(), then: Yup.number()
.required()
.min(0)
.label('Invalid value'),
otherwise: Yup.number() otherwise: Yup.number()
.nullable() .nullable()
.transform(() => null) .transform(() => null)
}) })
}) })
const Schema = Yup.object().shape({ const Schema = Yup.object()
triggerType, .shape({
requirement, triggerType,
threshold requirement,
// direction threshold
}) // direction
})
.test(
'are-fields-set',
'Invalid values',
({ threshold, triggerType }, context) => {
const validator = {
txAmount: threshold => threshold.threshold >= 0,
txVolume: threshold =>
threshold.threshold >= 0 && threshold.thresholdDays >= 0,
txVelocity: threshold =>
threshold.threshold >= 0 && threshold.thresholdDays >= 0,
consecutiveDays: threshold => threshold.thresholdDays >= 0
}
return (
(triggerType && validator?.[triggerType](threshold)) ||
context.createError({ path: 'threshold' })
)
}
)
// Direction V2 only // Direction V2 only
// const directionSchema = Yup.object().shape({ direction }) // const directionSchema = Yup.object().shape({ direction })
@ -377,7 +395,9 @@ const requirementSchema = Yup.object().shape({
requirement: Yup.string().required(), requirement: Yup.string().required(),
suspensionDays: Yup.number().when('requirement', { suspensionDays: Yup.number().when('requirement', {
is: value => value === 'suspend', is: value => value === 'suspend',
then: Yup.number().required(), then: Yup.number()
.required()
.min(0),
otherwise: Yup.number() otherwise: Yup.number()
.nullable() .nullable()
.transform(() => null) .transform(() => null)