Skip to content

Commit

Permalink
update validation chain for submit form
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Jan 28, 2020
1 parent 975ca8e commit e37456a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
16 changes: 8 additions & 8 deletions assets/js/aeria.js

Large diffs are not rendered by default.

Binary file added assets/js/aeria.js.zip
Binary file not shown.
34 changes: 27 additions & 7 deletions scripts/hoc/withPreventPostUpdate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { PureComponent } from 'react'
import { Validator } from '@aeria/uikit'
import { addValidator } from '../utils/prevent-post-update'
import scrollTo from '../utils/scroll-to'


export default function withPreventPostUpdate(WrappedComponent) {
return class extends PureComponent {
constructor(props) {
Expand All @@ -16,23 +18,25 @@ export default function withPreventPostUpdate(WrappedComponent) {
console.log(`[AERIA] Element with error ( id -> ${elementId}) not found!`)
return
}
el.focus()
el.blur()
scrollTo(el, 100, 300)
scrollTo(el.hidden ? el.parentNode : el, 100, 300)
}

validate = () => {
validate = async() => {
this.lastInvalidField = false
if (this.hasErrors(this.state.fields)) {
const fieldsUpdate = await this.updateFields(this.state.fields)
this.onChange({fields: fieldsUpdate})
this.updateErrorState(fieldsUpdate)

if (this.lastInvalidField) {
this.scrollToElement(this.props.id + '-' + this.lastInvalidField)
}

return !!this.lastInvalidField
}

hasErrors(fields) {
updateErrorState(fields) {
return fields.some(field => {
if (field.error || (field.required && !field.value)) {
if (field.error) {
this.lastInvalidField = field.id
return true
}
Expand All @@ -46,6 +50,22 @@ export default function withPreventPostUpdate(WrappedComponent) {
})
}

async updateFields(fields) {
const fieldsUpdate = await Promise.all(
fields.map(async field => {
const v = new Validator(field)
field.error = await v.validate((field.value || field.defaultValue))

if (field.children) {
field.children = await this.updateFields(field.children)
}
return field
})
)

return fieldsUpdate
}

onChange = ({fields}) => {
this.setState({fields})
}
Expand Down
17 changes: 12 additions & 5 deletions scripts/utils/prevent-post-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ const formPost = document.getElementById('post')
const saveButton = document.getElementById('publish')
const validators = []

function validate() {
return validators.some(validate => !validate())
async function validate() {
const results = await Promise.all(
validators.map(async validation => {
return await validation()
})
)
return results.some(r => !r)
}

function handleSubmit(e) {
if (!validate()) {
e.preventDefault()
async function handleSubmit(e) {
e.preventDefault()
const isValid = await validate()
if (isValid) {
formPost.submit()
}
}

Expand Down

0 comments on commit e37456a

Please sign in to comment.