refactor: simplify and cleanup front-end

This commit is contained in:
José Oliveira 2021-02-15 13:46:09 +00:00 committed by Josh Harvey
parent a8fbb57bf6
commit 351d2a078b
14 changed files with 93 additions and 213 deletions

View file

@ -2,9 +2,9 @@ import React, { memo, useState } from 'react'
import { TextInput } from '../base' import { TextInput } from '../base'
const SecretInput = memo(({ value, onFocus, onBlur, ...props }) => { const SecretInput = memo(
({ value, onFocus, isPasswordFilled, onBlur, ...props }) => {
const [focused, setFocused] = useState(false) const [focused, setFocused] = useState(false)
const isPasswordFilled = props.isPasswordFilled
const placeholder = '⚬ ⚬ ⚬ This field is set ⚬ ⚬ ⚬' const placeholder = '⚬ ⚬ ⚬ This field is set ⚬ ⚬ ⚬'
const innerOnFocus = event => { const innerOnFocus = event => {
setFocused(true) setFocused(true)
@ -22,12 +22,14 @@ const SecretInput = memo(({ value, onFocus, onBlur, ...props }) => {
type="password" type="password"
onFocus={innerOnFocus} onFocus={innerOnFocus}
onBlur={innerOnBlur} onBlur={innerOnBlur}
isPasswordFilled={isPasswordFilled}
value={value} value={value}
InputProps={{ value: value }} InputProps={{ value: value }}
InputLabelProps={{ shrink: isPasswordFilled || value || focused }} InputLabelProps={{ shrink: isPasswordFilled || value || focused }}
placeholder={isPasswordFilled ? placeholder : ''} placeholder={isPasswordFilled ? placeholder : ''}
/> />
) )
}) }
)
export default SecretInput export default SecretInput

View file

@ -11,6 +11,7 @@ const useStyles = makeStyles(styles)
const TextInput = memo( const TextInput = memo(
({ ({
name, name,
isPasswordFilled,
onChange, onChange,
onBlur, onBlur,
value, value,
@ -26,7 +27,6 @@ const TextInput = memo(
...props ...props
}) => { }) => {
const classes = useStyles({ textAlign, width, size }) const classes = useStyles({ textAlign, width, size })
const isPasswordFilled = props.isPasswordFilled
const isTextFilled = !error && !R.isNil(value) && !R.isEmpty(value) const isTextFilled = !error && !R.isNil(value) && !R.isEmpty(value)
const filled = isPasswordFilled || isTextFilled const filled = isPasswordFilled || isTextFilled
const inputClasses = { const inputClasses = {

View file

@ -2,16 +2,16 @@ import React, { memo } from 'react'
import { SecretInput } from '../base' import { SecretInput } from '../base'
const SecretInputFormik = memo(({ ...props }) => { const SecretInputFormik = memo(({ isPasswordFilled, ...props }) => {
const { name, onChange, onBlur, value } = props.field const { name, onChange, onBlur, value } = props.field
const { touched, errors } = props.form const { touched, errors } = props.form
const isPasswordFilled = props.isPasswordFilled
const error = !isPasswordFilled && !!(touched[name] && errors[name]) const error = !isPasswordFilled && !!(touched[name] && errors[name])
return ( return (
<SecretInput <SecretInput
name={name} name={name}
isPasswordFilled={isPasswordFilled}
onChange={onChange} onChange={onChange}
onBlur={onBlur} onBlur={onBlur}
value={value} value={value}

View file

@ -58,21 +58,17 @@ const Services = () => {
} }
const getElements = ({ code, elements }) => { const getElements = ({ code, elements }) => {
return R.compose( return R.map(elem => {
R.map(elem => { if (elem.component !== SecretInput) return elem
return elem.component === SecretInput return {
? R.assoc( ...elem,
'inputProps', inputProps: {
{
isPasswordFilled: isPasswordFilled:
!R.isNil(accounts[code]) && !R.isNil(accounts[code]) &&
!R.isNil(R.path([elem.code], accounts[code])) !R.isNil(R.path([elem.code], accounts[code]))
}, }
elem }
) }, elements)
: R.identity(elem)
})
)(elements)
} }
const getAccounts = ({ elements, code }) => { const getAccounts = ({ elements, code }) => {
@ -81,22 +77,14 @@ const Services = () => {
R.reject(R.isNil), R.reject(R.isNil),
R.map(({ component, code }) => (component === SecretInput ? code : null)) R.map(({ component, code }) => (component === SecretInput ? code : null))
)(elements) )(elements)
return R.compose( return R.mapObjIndexed(
R.mapObjIndexed((value, key, obj) => (value, key) => (R.includes(key, passwordFields) ? '' : value),
R.includes(key, passwordFields) ? '' : value account
) )
)(account)
} }
const getValidationSchema = ({ const getValidationSchema = ({ code, getValidationSchema }) =>
validationSchema, getValidationSchema(accounts[code])
code,
hasSecret,
getValidationSchema
}) => {
if (!hasSecret) return validationSchema
return getValidationSchema(accounts[code])
}
return ( return (
<div className={classes.wrapper}> <div className={classes.wrapper}>

View file

@ -13,7 +13,6 @@ const isDefined = it => it && it.length
export default { export default {
code: 'bitgo', code: 'bitgo',
name: 'BitGo', name: 'BitGo',
hasSecret: true,
title: 'BitGo (Wallet)', title: 'BitGo (Wallet)',
elements: [ elements: [
{ {
@ -88,51 +87,8 @@ export default {
component: SecretInput component: SecretInput
} }
], ],
validationSchema: Yup.object().shape({
token: Yup.string()
.max(100, 'Too long')
.required(),
BTCWalletId: Yup.string().max(100, 'Too long'),
BTCWalletPassphrase: Yup.string()
.max(100, 'Too long')
.when('BTCWalletId', {
is: isDefined,
then: Yup.string().required()
}),
LTCWalletId: Yup.string().max(100, 'Too long'),
LTCWalletPassphrase: Yup.string()
.max(100, 'Too long')
.when('LTCWalletId', {
is: isDefined,
then: Yup.string().required()
}),
ZECWalletId: Yup.string().max(100, 'Too long'),
ZECWalletPassphrase: Yup.string()
.max(100, 'Too long')
.when('ZECWalletId', {
is: isDefined,
then: Yup.string().required()
}),
BCHWalletId: Yup.string().max(100, 'Too long'),
BCHWalletPassphrase: Yup.string()
.max(100, 'Too long')
.when('BCHWalletId', {
is: isDefined,
then: Yup.string().required()
}),
DASHWalletId: Yup.string().max(100, 'Too long'),
DASHWalletPassphrase: Yup.string()
.max(100, 'Too long')
.when('DASHWalletId', {
is: isDefined,
then: Yup.string().required()
}),
environment: Yup.string()
.matches(/(prod|test)/)
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
token: Yup.string() token: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -174,7 +130,6 @@ export default {
environment: Yup.string() environment: Yup.string()
.matches(/(prod|test)/) .matches(/(prod|test)/)
.required() .required()
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -8,7 +8,6 @@ import secretTest from './helper'
export default { export default {
code: 'bitstamp', code: 'bitstamp',
name: 'Bitstamp', name: 'Bitstamp',
hasSecret: true,
title: 'Bitstamp (Exchange)', title: 'Bitstamp (Exchange)',
elements: [ elements: [
{ {
@ -31,19 +30,8 @@ export default {
component: SecretInputFormik component: SecretInputFormik
} }
], ],
validationSchema: Yup.object().shape({
clientId: Yup.string()
.max(100, 'Too long')
.required(),
key: Yup.string()
.max(100, 'Too long')
.required(),
secret: Yup.string()
.max(100, 'Too long')
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
clientId: Yup.string() clientId: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -53,7 +41,6 @@ export default {
secret: Yup.string() secret: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.test(secretTest(account?.secret)) .test(secretTest(account?.secret))
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -5,7 +5,6 @@ import TextInputFormik from 'src/components/inputs/formik/TextInput'
export default { export default {
code: 'blockcypher', code: 'blockcypher',
name: 'Blockcypher', name: 'Blockcypher',
hasSecret: false,
title: 'Blockcypher (Payments)', title: 'Blockcypher (Payments)',
elements: [ elements: [
{ {
@ -22,8 +21,8 @@ export default {
face: true face: true
} }
], ],
getValidationSchema: () => {
validationSchema: Yup.object().shape({ return Yup.object().shape({
token: Yup.string() token: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -33,3 +32,4 @@ export default {
.required() .required()
}) })
} }
}

View file

@ -8,7 +8,6 @@ import secretTest from './helper'
export default { export default {
code: 'infura', code: 'infura',
name: 'Infura', name: 'Infura',
hasSecret: true,
title: 'Infura (Wallet)', title: 'Infura (Wallet)',
elements: [ elements: [
{ {
@ -30,19 +29,8 @@ export default {
face: true face: true
} }
], ],
validationSchema: Yup.object().shape({
apiKey: Yup.string()
.max(100, 'Too long')
.required(),
apiSecret: Yup.string()
.max(100, 'Too long')
.required(),
endpoint: Yup.string()
.max(100, 'Too long')
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
apiKey: Yup.string() apiKey: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -52,7 +40,6 @@ export default {
endpoint: Yup.string() endpoint: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required() .required()
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -8,7 +8,6 @@ import secretTest from './helper'
export default { export default {
code: 'itbit', code: 'itbit',
name: 'itBit', name: 'itBit',
hasSecret: true,
title: 'itBit (Exchange)', title: 'itBit (Exchange)',
elements: [ elements: [
{ {
@ -36,22 +35,8 @@ export default {
component: SecretInputFormik component: SecretInputFormik
} }
], ],
validationSchema: Yup.object().shape({
userId: Yup.string()
.max(100, 'Too long')
.required(),
walletId: Yup.string()
.max(100, 'Too long')
.required(),
clientKey: Yup.string()
.max(100, 'Too long')
.required(),
clientSecret: Yup.string()
.max(100, 'Too long')
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
userId: Yup.string() userId: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -64,7 +49,6 @@ export default {
clientSecret: Yup.string() clientSecret: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.test(secretTest(account?.clientSecret)) .test(secretTest(account?.clientSecret))
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -8,7 +8,6 @@ import secretTest from './helper'
export default { export default {
code: 'kraken', code: 'kraken',
name: 'Kraken', name: 'Kraken',
hasSecret: true,
title: 'Kraken (Exchange)', title: 'Kraken (Exchange)',
elements: [ elements: [
{ {
@ -24,23 +23,14 @@ export default {
component: SecretInputFormik component: SecretInputFormik
} }
], ],
validationSchema: Yup.object().shape({
apiKey: Yup.string()
.max(100, 'Too long')
.required(),
privateKey: Yup.string()
.max(100, 'Too long')
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
apiKey: Yup.string() apiKey: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
privateKey: Yup.string() privateKey: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.test(secretTest(account?.privateKey)) .test(secretTest(account?.privateKey))
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -5,7 +5,6 @@ import TextInputFormik from 'src/components/inputs/formik/TextInput'
export default { export default {
code: 'mailgun', code: 'mailgun',
name: 'Mailgun', name: 'Mailgun',
hasSecret: false,
title: 'Mailgun (Email)', title: 'Mailgun (Email)',
elements: [ elements: [
{ {
@ -31,7 +30,8 @@ export default {
face: true face: true
} }
], ],
validationSchema: Yup.object().shape({ getValidationSchema: () => {
return Yup.object().shape({
apiKey: Yup.string() apiKey: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -48,3 +48,4 @@ export default {
.required() .required()
}) })
} }
}

View file

@ -8,7 +8,6 @@ import secretTest from './helper'
export default { export default {
code: 'twilio', code: 'twilio',
name: 'Twilio', name: 'Twilio',
hasSecret: true,
title: 'Twilio (SMS)', title: 'Twilio (SMS)',
elements: [ elements: [
{ {
@ -34,22 +33,8 @@ export default {
face: true face: true
} }
], ],
validationSchema: Yup.object().shape({
accountSid: Yup.string()
.max(100, 'Too long')
.required(),
authToken: Yup.string()
.max(100, 'Too long')
.required(),
fromNumber: Yup.string()
.max(100, 'Too long')
.required(),
toNumber: Yup.string()
.max(100, 'Too long')
.required()
}),
getValidationSchema: account => { getValidationSchema: account => {
const schema = { return Yup.object().shape({
accountSid: Yup.string() accountSid: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required(), .required(),
@ -62,7 +47,6 @@ export default {
toNumber: Yup.string() toNumber: Yup.string()
.max(100, 'Too long') .max(100, 'Too long')
.required() .required()
} })
return Yup.object().shape(schema)
} }
} }

View file

@ -24,8 +24,8 @@ const getItems = (accountsConfig, accounts, type, crypto) => {
const account = find(code) const account = find(code)
if (!schema[code]) return true if (!schema[code]) return true
const { validationSchema } = schema[code] const { getValidationSchema } = schema[code]
return validationSchema.isValidSync(account) return getValidationSchema(account).isValidSync(account)
})(fConfig) })(fConfig)
return { filled, unfilled } return { filled, unfilled }

View file

@ -144,7 +144,9 @@ const ChooseWallet = ({ data: currentData, addData }) => {
value={accounts.infura} value={accounts.infura}
save={saveWallet(selected)} save={saveWallet(selected)}
elements={schema.infura.elements} elements={schema.infura.elements}
validationSchema={schema.infura.validationSchema} validationSchema={schema.infura.getValidationSchema(
accounts.infura
)}
buttonLabel={'Continue'} buttonLabel={'Continue'}
buttonClass={classes.formButton} buttonClass={classes.formButton}
/> />