Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
feat(tokens): form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Jul 30, 2018
1 parent 9c99055 commit dd55b9e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
REACT_APP_PROD_RINKEBY_ARBITRATOR_ADDRESS="0x27a836a66a16d10a4d5a400dd89417e4e9a8961f"
REACT_APP_PROD_MAINNET_ARBITRATOR_ADDRESS="0xa9f0ed953257de9598523c04741adc8225c9b1c5"
REACT_APP_PROD_STORE_PROVIDER="https://develop.kleros.in"
REACT_APP_PROD_DOGE_IMAGES_BASE_URL="https://staging-doges-on-trial-doge-images.s3.us-east-2.amazonaws.com/"
REACT_APP_PROD_DOGE_IMAGES_BASE_URL="https://production-doges-on-trial-doge-images.s3.us-east-2.amazonaws.com/"

[context.production.environment]
REACT_APP_PROD_ETHEREUM_PROVIDER="<provider>"
Expand Down
8 changes: 4 additions & 4 deletions src/components/__snapshots__/storyshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8670,7 +8670,7 @@ exports[`Storyshots Table default 1`] = `
value=""
/>
<div
className="TextInput-placeholder"
className="TextInput-placeholder is-touched"
>
SEARCH
</div>
Expand Down Expand Up @@ -9508,7 +9508,7 @@ exports[`Storyshots Text Input default 1`] = `
value=""
/>
<div
className="TextInput-placeholder"
className="TextInput-placeholder is-touched"
>
EMAIL
</div>
Expand Down Expand Up @@ -9816,7 +9816,7 @@ exports[`Storyshots Text Input error 1`] = `
value=""
/>
<div
className="TextInput-placeholder"
className="TextInput-placeholder is-touched"
>
EMAIL
</div>
Expand Down Expand Up @@ -10437,7 +10437,7 @@ exports[`Storyshots Text Input valid 1`] = `
value=""
/>
<div
className="TextInput-placeholder"
className="TextInput-placeholder is-touched"
>
EMAIL
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/components/text-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const TextInput = ({
{placeholder && (
<div
className={`TextInput-placeholder${
touched || value ? ' is-touched' : ''
touched || (value !== null && value !== undefined)
? ' is-touched'
: ''
}`}
>
{placeholder}
Expand Down
4 changes: 2 additions & 2 deletions src/containers/home/components/activate-pnk-form/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { form } from '../../../../utils/form-generator'
import { required, number } from '../../../../utils/validation'
import { required, number, positiveNumber } from '../../../../utils/validation'

export const {
Form: ActivatePNKForm,
Expand All @@ -8,7 +8,7 @@ export const {
} = form('activatePNKForm', {
amount: {
type: 'text',
validate: [required, number],
validate: [required, number, positiveNumber],
props: {
type: 'number',
className: 'Form-fullWidth'
Expand Down
4 changes: 2 additions & 2 deletions src/containers/tokens/components/transfer-pnk-form/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { form } from '../../../../utils/form-generator'
import { required, number } from '../../../../utils/validation'
import { required, number, positiveNumber } from '../../../../utils/validation'

export const {
Form: TransferPNKForm,
Expand All @@ -15,7 +15,7 @@ export const {
},
amount: {
type: 'text',
validate: [required, number],
validate: [required, number, positiveNumber],
props: {
type: 'number',
className: 'Form-noMargins'
Expand Down
4 changes: 2 additions & 2 deletions src/containers/tokens/components/withdraw-pnk-form/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { form } from '../../../../utils/form-generator'
import { required, number } from '../../../../utils/validation'
import { required, number, positiveNumber } from '../../../../utils/validation'

export const {
Form: WithdrawPNKForm,
Expand All @@ -15,7 +15,7 @@ export const {
},
amount: {
type: 'text',
validate: [required, number],
validate: [required, number, positiveNumber],
props: {
type: 'number',
className: 'Form-noMargins'
Expand Down
21 changes: 21 additions & 0 deletions src/containers/tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ class Tokens extends PureComponent {
fetchArbitratorData()
}

validateTransferPNKForm = values => {
const { PNKBalance } = this.props
const errors = {}
if (PNKBalance.data.contractBalance < values.amount)
errors.amount = 'You do not own this much PNK.'
return errors
}

validateWithdrawPNKForm = values => {
const { PNKBalance } = this.props
const errors = {}
if (
PNKBalance.data.tokenBalance - PNKBalance.data.lockedTokens <
values.amount
)
errors.amount = 'You do not have this much free PNK.'
return errors
}

render() {
const {
accounts,
Expand Down Expand Up @@ -118,6 +137,7 @@ class Tokens extends PureComponent {
amount: PNKBalance.data.contractBalance
}}
onSubmit={transferPNK}
validate={this.validateTransferPNKForm}
/>

<Button
Expand All @@ -141,6 +161,7 @@ class Tokens extends PureComponent {
amount: PNKBalance.data.tokenBalance - PNKBalance.data.lockedTokens
}}
onSubmit={withdrawPNK}
validate={this.validateWithdrawPNKForm}
/>

<Button
Expand Down
5 changes: 4 additions & 1 deletion src/utils/validation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const required = name => v => (v ? undefined : `${name} is required.`)
export const required = name => v =>
v !== null && v !== undefined ? undefined : `${name} is required.`
export const number = name => v =>
Number.isNaN(Number(v)) ? `${name} must be a number.` : undefined
export const positiveNumber = name => v =>
v < 0 ? `${name} must be positive.` : undefined

0 comments on commit dd55b9e

Please sign in to comment.