refactor handler Abstract FormGenerator.js / Create.js
parent
3fa6d9cfbf
commit
436c1bbc27
|
|
@ -3,14 +3,27 @@ import React from 'react'
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param { [ {}, {}, ...{} ] } inputList - list of dicts with info about input
|
* @param { [ {}, {}, ...{} ] } inputList - list of dicts with info about input
|
||||||
|
* @param { [] } refList - react ref objects list for handler validation
|
||||||
* @param { } action - fetch method
|
* @param { } action - fetch method
|
||||||
*/
|
*/
|
||||||
const FormGenerator = ({
|
const FormGenerator = ({
|
||||||
inputList, action
|
inputList, refList,
|
||||||
|
action
|
||||||
}) => {
|
}) => {
|
||||||
|
|
||||||
|
const handler = async (event) => {
|
||||||
|
event.preventDefault()
|
||||||
|
for ( let i = 0; i < refList.length; i++ ) {
|
||||||
|
if ( refList[i].current.value === '' ) {
|
||||||
|
refList[i].current.focus()
|
||||||
|
} else if ( i === refList.length - 1 ) {
|
||||||
|
await action( refList )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={ event => action( event ) }>
|
<form onSubmit={ event => handler( event ) }>
|
||||||
{
|
{
|
||||||
inputList.map( (input, key) => {
|
inputList.map( (input, key) => {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,22 +60,11 @@ const AlbumCreate = ({
|
||||||
setMessage('')
|
setMessage('')
|
||||||
}
|
}
|
||||||
|
|
||||||
const create = async (event) => {
|
const createFetch = async ( refs ) => {
|
||||||
event.preventDefault()
|
|
||||||
let title = titleInput.current.value
|
|
||||||
let description = descriptionInput.current.value
|
|
||||||
if ( title !== '' && description !== '' ) {
|
|
||||||
await createFetch(title, description)
|
|
||||||
} if ( description === '') {
|
|
||||||
document.getElementById('descriptionAlbumInput').focus()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const createFetch = async (title, description) => {
|
|
||||||
let album = {
|
let album = {
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
title: title,
|
title: refs[0].current.value,
|
||||||
description: description,
|
description: refs[1].current.value,
|
||||||
image: image,
|
image: image,
|
||||||
url_code: generateUrlCode( 'album' ),
|
url_code: generateUrlCode( 'album' ),
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +80,8 @@ const AlbumCreate = ({
|
||||||
<div>
|
<div>
|
||||||
<FormGenerator
|
<FormGenerator
|
||||||
inputList={ inputList }
|
inputList={ inputList }
|
||||||
action={ create }
|
refList={ refList }
|
||||||
|
action={ createFetch }
|
||||||
/>
|
/>
|
||||||
<ResetComponent
|
<ResetComponent
|
||||||
resetState={ resetState }
|
resetState={ resetState }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue