fix: trigger form validation

fix: package-lock

fix: remove comment
This commit is contained in:
Sérgio Salgado 2021-02-12 16:00:45 +00:00 committed by Josh Harvey
parent b594eb8b48
commit 3b33ae0255
4 changed files with 4184 additions and 5993 deletions

File diff suppressed because it is too large Load diff

View file

@ -37,7 +37,7 @@
"react-virtualized": "^9.21.2", "react-virtualized": "^9.21.2",
"sanctuary": "^2.0.1", "sanctuary": "^2.0.1",
"uuid": "^7.0.2", "uuid": "^7.0.2",
"yup": "0.29.3" "yup": "0.30.0"
}, },
"devDependencies": { "devDependencies": {
"@storybook/addon-actions": "6.0.26", "@storybook/addon-actions": "6.0.26",

View file

@ -171,7 +171,6 @@ const InfoPanel = ({ step, config = {}, liveValues = {}, currency }) => {
{oldText} {oldText}
{step !== 1 && ', '} {step !== 1 && ', '}
{newTextElements} {newTextElements}
{/* <span className={classes.infoCurrentText}>{newText}</span> */}
{!isLastStep && '...'} {!isLastStep && '...'}
</Info3> </Info3>
</> </>

View file

@ -5,9 +5,14 @@ import * as R from 'ramda'
import React, { memo } from 'react' import React, { memo } from 'react'
import * as Yup from 'yup' import * as Yup from 'yup'
import { TextInput, RadioGroup } from 'src/components/inputs/formik' import {
NumberInput,
TextInput,
RadioGroup
} from 'src/components/inputs/formik'
import { H4, Label2, Label1, Info1, Info2 } from 'src/components/typography' import { H4, Label2, Label1, Info1, Info2 } from 'src/components/typography'
import { errorColor } from 'src/styling/variables' import { errorColor } from 'src/styling/variables'
import { transformNumber } from 'src/utils/number'
// import { ReactComponent as TxInIcon } from 'src/styling/icons/direction/cash-in.svg' // import { ReactComponent as TxInIcon } from 'src/styling/icons/direction/cash-in.svg'
// import { ReactComponent as TxOutIcon } from 'src/styling/icons/direction/cash-out.svg' // import { ReactComponent as TxOutIcon } from 'src/styling/icons/direction/cash-out.svg'
@ -86,7 +91,7 @@ const threshold = Yup.object().shape({
thresholdDays: Yup.number().test({ thresholdDays: Yup.number().test({
test(val) { test(val) {
const { triggerType } = this.parent const { triggerType } = this.parent
const requireThrehsold = ['txVolume', 'txVelocity'] const requireThrehsold = ['txVolume', 'txVelocity', 'consecutiveDays']
if (R.isEmpty(val) && R.includes(triggerType, requireThrehsold)) { if (R.isEmpty(val) && R.includes(triggerType, requireThrehsold)) {
return this.createError() return this.createError()
@ -202,10 +207,37 @@ const Schema = Yup.object().shape({
// } // }
// TYPE // TYPE
const typeSchema = Yup.object().shape({ const typeSchema = Yup.object()
triggerType, .shape({
threshold triggerType: Yup.string().required(),
}) threshold: Yup.object({
threshold: Yup.number()
.transform(transformNumber)
.nullable(),
thresholdDays: Yup.number()
.transform(transformNumber)
.nullable()
})
})
.test(
'are-fields-set',
'All fields must be set.',
({ triggerType, threshold }, 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' })
)
}
)
const typeOptions = [ const typeOptions = [
{ display: 'Transaction amount', code: 'txAmount' }, { display: 'Transaction amount', code: 'txAmount' },
@ -228,6 +260,13 @@ const Type = ({ ...props }) => {
const isThresholdDaysEnabled = containsType(['txVolume', 'txVelocity']) const isThresholdDaysEnabled = containsType(['txVolume', 'txVelocity'])
const isConsecutiveDaysEnabled = containsType(['consecutiveDays']) const isConsecutiveDaysEnabled = containsType(['consecutiveDays'])
const thresholdClass = {
[classes.error]:
errors.threshold &&
((!containsType(['consecutiveDays']) && touched.threshold?.threshold) ||
(!containsType(['txAmount']) && touched.threshold?.thresholdDays))
}
const isRadioGroupActive = () => { const isRadioGroupActive = () => {
return ( return (
isThresholdCurrencyEnabled || isThresholdCurrencyEnabled ||
@ -253,7 +292,7 @@ const Type = ({ ...props }) => {
<div className={classes.thresholdWrapper}> <div className={classes.thresholdWrapper}>
{isRadioGroupActive() && ( {isRadioGroupActive() && (
<H4 className={classnames(typeClass, classes.thresholdTitle)}> <H4 className={classnames(thresholdClass, classes.thresholdTitle)}>
Threshold Threshold
</H4> </H4>
)} )}
@ -262,11 +301,11 @@ const Type = ({ ...props }) => {
<> <>
<Field <Field
className={classes.thresholdField} className={classes.thresholdField}
component={TextInput} component={NumberInput}
size="lg" size="lg"
name="threshold.threshold" name="threshold.threshold"
/> />
<Info1 className={classnames(typeClass, classes.description)}> <Info1 className={classnames(classes.description)}>
{props.currency} {props.currency}
</Info1> </Info1>
</> </>
@ -275,11 +314,11 @@ const Type = ({ ...props }) => {
<> <>
<Field <Field
className={classes.thresholdField} className={classes.thresholdField}
component={TextInput} component={NumberInput}
size="lg" size="lg"
name="threshold.threshold" name="threshold.threshold"
/> />
<Info1 className={classnames(typeClass, classes.description)}> <Info1 className={classnames(classes.description)}>
transactions transactions
</Info1> </Info1>
</> </>
@ -296,24 +335,22 @@ const Type = ({ ...props }) => {
</Info1> </Info1>
<Field <Field
className={classes.thresholdField} className={classes.thresholdField}
component={TextInput} component={NumberInput}
size="lg" size="lg"
name="threshold.thresholdDays" name="threshold.thresholdDays"
/> />
<Info1 className={classnames(typeClass, classes.description)}> <Info1 className={classnames(classes.description)}>days</Info1>
days
</Info1>
</> </>
)} )}
{isConsecutiveDaysEnabled && ( {isConsecutiveDaysEnabled && (
<> <>
<Field <Field
className={classes.thresholdField} className={classes.thresholdField}
component={TextInput} component={NumberInput}
size="lg" size="lg"
name="threshold.thresholdDays" name="threshold.thresholdDays"
/> />
<Info1 className={classnames(typeClass, classes.description)}> <Info1 className={classnames(classes.description)}>
consecutive days consecutive days
</Info1> </Info1>
</> </>
@ -329,7 +366,10 @@ const type = currency => ({
options: typeOptions, options: typeOptions,
Component: Type, Component: Type,
props: { currency }, props: { currency },
initialValues: { triggerType: '', threshold: '' } initialValues: {
triggerType: '',
threshold: { threshold: '', thresholdDays: '' }
}
}) })
const requirementSchema = Yup.object().shape({ const requirementSchema = Yup.object().shape({
@ -337,7 +377,10 @@ 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(),
otherwise: Yup.number()
.nullable()
.transform(() => null)
}) })
}).required() }).required()
}) })
@ -594,7 +637,7 @@ const getElements = (currency, classes) => [
name: 'threshold', name: 'threshold',
size: 'sm', size: 'sm',
width: 284, width: 284,
textAlign: 'right', textAlign: 'left',
input: () => <ThresholdInput currency={currency} />, input: () => <ThresholdInput currency={currency} />,
view: (it, config) => <ThresholdView config={config} currency={currency} /> view: (it, config) => <ThresholdView config={config} currency={currency} />
} }