refactor handler Abstract FormGenerator.js / Create.js

develop
TBS093A 2020-08-17 12:23:43 +02:00
parent 3fa6d9cfbf
commit 436c1bbc27
2 changed files with 20 additions and 17 deletions

View File

@ -3,14 +3,27 @@ import React from 'react'
/**
*
* @param { [ {}, {}, ...{} ] } inputList - list of dicts with info about input
* @param { [] } refList - react ref objects list for handler validation
* @param { } action - fetch method
*/
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 (
<form onSubmit={ event => action( event ) }>
<form onSubmit={ event => handler( event ) }>
{
inputList.map( (input, key) => {

View File

@ -60,22 +60,11 @@ const AlbumCreate = ({
setMessage('')
}
const create = async (event) => {
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) => {
const createFetch = async ( refs ) => {
let album = {
user_id: user.id,
title: title,
description: description,
title: refs[0].current.value,
description: refs[1].current.value,
image: image,
url_code: generateUrlCode( 'album' ),
}
@ -91,7 +80,8 @@ const AlbumCreate = ({
<div>
<FormGenerator
inputList={ inputList }
action={ create }
refList={ refList }
action={ createFetch }
/>
<ResetComponent
resetState={ resetState }