|
|
@ -1,16 +1,32 @@
|
|
|
|
// TODO: Enable exportDefaultFrom in Babel syntax
|
|
|
|
// TODO: Enable exportDefaultFrom in Babel syntax
|
|
|
|
import React, { useReducer } from 'react'
|
|
|
|
import React, { useReducer } from 'react'
|
|
|
|
|
|
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
|
|
|
|
import styles from './styles.module.css'
|
|
|
|
import styles from './styles.module.css'
|
|
|
|
export const Input = require('./input.js').default
|
|
|
|
export const Input = require('./input.js').default
|
|
|
|
export const Button = require('./button.js').default
|
|
|
|
export const Button = require('./button.js').default
|
|
|
|
|
|
|
|
|
|
|
|
export const FormController = function FormController({errors, errorDispatch, children, onSubmit}){
|
|
|
|
const errorReducer = (errors, action) => {
|
|
|
|
const initialState = {
|
|
|
|
switch (action.type) {
|
|
|
|
fields: {}
|
|
|
|
case 'set_errors':
|
|
|
|
|
|
|
|
const newErrors = {}
|
|
|
|
|
|
|
|
for(const field of action.errors)
|
|
|
|
|
|
|
|
newErrors[field.param] = field.msg
|
|
|
|
|
|
|
|
return newErrors
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'clear_error':
|
|
|
|
|
|
|
|
console.log('clear_error', action)
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
...errors,
|
|
|
|
|
|
|
|
[action.field]: undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const reducer = (state, action)=>{
|
|
|
|
default:
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const formReducer = (state, action)=>{
|
|
|
|
let fields = {...state.fields}
|
|
|
|
let fields = {...state.fields}
|
|
|
|
const prevField = state.fields[action.name]
|
|
|
|
const prevField = state.fields[action.name]
|
|
|
|
|
|
|
|
|
|
|
@ -55,6 +71,12 @@ export const FormController = function FormController({errors, errorDispatch, c
|
|
|
|
return {fields}
|
|
|
|
return {fields}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const FormController = function FormController({children, url, method = 'POST', afterSubmit = ()=>null}){
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
|
|
|
|
fields: {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update initial state
|
|
|
|
// Update initial state
|
|
|
|
React.Children.forEach(children, child => {
|
|
|
|
React.Children.forEach(children, child => {
|
|
|
|
if(!child.props.name) return;
|
|
|
|
if(!child.props.name) return;
|
|
|
@ -68,26 +90,41 @@ export const FormController = function FormController({errors, errorDispatch, c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Create reducer
|
|
|
|
// Create reducers
|
|
|
|
const [state, dispatch] = useReducer(reducer, initialState)
|
|
|
|
const [state, dispatch] = useReducer(formReducer, initialState)
|
|
|
|
|
|
|
|
const [errors, errorDispatch] = useReducer(errorReducer, {})
|
|
|
|
|
|
|
|
|
|
|
|
const submitForm = ev=>{
|
|
|
|
// Handle submitting form
|
|
|
|
|
|
|
|
const handleSubmit = async (ev) => {
|
|
|
|
if(ev) ev.preventDefault();
|
|
|
|
if(ev) ev.preventDefault();
|
|
|
|
|
|
|
|
|
|
|
|
const fields = {}
|
|
|
|
const data = {}
|
|
|
|
for(const name in state.fields){
|
|
|
|
for(const name in state.fields){
|
|
|
|
const field = state.fields[name]
|
|
|
|
const field = state.fields[name]
|
|
|
|
fields[field.name] = field.value
|
|
|
|
data[field.name] = field.value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(url)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await axios({ method, url, data })
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
if(!err.response || err.response.status !== 422) throw err;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return errorDispatch({
|
|
|
|
|
|
|
|
type: 'set_errors',
|
|
|
|
|
|
|
|
errors: err.response.data.errors
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit(fields)
|
|
|
|
afterSubmit(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Map children
|
|
|
|
const _children = React.Children.map(children, child => {
|
|
|
|
const _children = React.Children.map(children, child => {
|
|
|
|
if(child.type === Button && child.props.type.toLowerCase() === 'submit')
|
|
|
|
if(child.type === Button && child.props.type.toLowerCase() === 'submit')
|
|
|
|
return React.cloneElement(child, {
|
|
|
|
return React.cloneElement(child, {
|
|
|
|
enabled: !Object.values(state.fields).some(field=>!field.isValid),
|
|
|
|
enabled: !Object.values(state.fields).some(field=>!field.isValid),
|
|
|
|
onClick: submitForm
|
|
|
|
onClick: handleSubmit
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const {name} = child.props;
|
|
|
|
const {name} = child.props;
|
|
|
@ -102,7 +139,7 @@ export const FormController = function FormController({errors, errorDispatch, c
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<form onSubmit={onSubmit} className={styles.formContainer}>
|
|
|
|
<form autocomplete="off" onSubmit={handleSubmit} className={styles.formContainer}>
|
|
|
|
{_children}
|
|
|
|
{_children}
|
|
|
|
</form>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
)
|
|
|
|