refactor input form generator && complete update abstract

develop
TBS093A 2020-08-17 18:36:14 +02:00
parent 0bdc490e38
commit 13e9dc08d7
6 changed files with 120 additions and 95 deletions

View File

@ -0,0 +1,12 @@
export const validate = ( inputs ) => {
let object = {}
for (let i = 0; i < inputs.length; i++) {
if (
inputs[i]['type'] === 'text'
&& inputs[i]['ref'] !== undefined
&& inputs[i]['ref'].current.value !== ''
)
object[ inputs[i]['name'] ] = inputs[i]['ref'].current.value
}
return object
}

View File

@ -14,7 +14,11 @@ export const FormGenerator = ({
const handler = async (event) => { const handler = async (event) => {
event.preventDefault() event.preventDefault()
for ( let i = 0; i < refList.length; i++ ) { for ( let i = 0; i < refList.length; i++ ) {
if ( refList[i].current.value === '' ) { if (
refList[i].current.value === ''
&& inputList[0].action !== 'Update'
|| i === 0 && refList.length !== 1
) {
refList[i].current.focus() refList[i].current.focus()
} else if ( i === refList.length - 1 ) { } else if ( i === refList.length - 1 ) {
await action( refList ) await action( refList )
@ -22,15 +26,20 @@ export const FormGenerator = ({
} }
} }
let info
return ( return (
<form onSubmit={ event => handler( event ) }> <form onSubmit={ event => handler( event ) }>
{ {
inputList.map( (input, key) => { inputList.map( (input, key) => {
if ( input.type === 'text' ) { if ( input.type === 'info' ) {
info = input
} else if ( input.type === 'text' ) {
return ( return (
<TextInputGenerator <TextInputGenerator
input={ input } input={ input }
info={ info }
key={ key } key={ key }
/> />
) )
@ -38,6 +47,7 @@ export const FormGenerator = ({
return ( return (
<UploadInputGenerator <UploadInputGenerator
input={ input } input={ input }
info={ info }
key={ key } key={ key }
/> />
) )
@ -55,18 +65,23 @@ export const FormGenerator = ({
* { * {
* type: 'text', * type: 'text',
* name: 'name', * name: 'name',
* endpoint: 'Album',
* ref: React.createRef() * ref: React.createRef()
* } } input - basic text input * } } input - basic text input
* @param {
* {
* type: 'info',
* action: 'Update'
* endpoint: 'Album'
* } } info - information about form
*/ */
const TextInputGenerator = ({ const TextInputGenerator = ({
input input, info
}) => { }) => {
return ( return (
<div> <div>
{ input.name + ':' } { input.name + ':' }
<input <input
id={ input.name + input.endpoint + 'Input' } id={ input.name + info.action + info.endpoint + 'Input' }
autoComplete='off' autoComplete='off'
ref={ input.ref } ref={ input.ref }
/> />
@ -87,7 +102,7 @@ const TextInputGenerator = ({
* } } input - * } } input -
*/ */
const UploadInputGenerator = ({ const UploadInputGenerator = ({
input input, info
}) => { }) => {
const onLoadFile = async ( event ) => { const onLoadFile = async ( event ) => {
@ -129,7 +144,7 @@ const UploadInputGenerator = ({
</pre> </pre>
<input <input
style={ { marginTop: '-55px' } } style={ { marginTop: '-55px' } }
id={ input.name + input.endpoint + 'Input' } id={ input.name + info.action + info.endpoint + 'Input' }
className='uploadInput' className='uploadInput'
type='file' type='file'
accept={ input.fileType + '/*' } accept={ input.fileType + '/*' }

View File

@ -3,7 +3,7 @@ import { connect } from 'react-redux'
import { createAlbum } from '../../../../../../stores/album/duck/operations' import { createAlbum } from '../../../../../../stores/album/duck/operations'
import { generateUrlCode } from '../../../../../generateUrlCode' import { generateUrlCode } from '../../../../../generateUrlCode'
import FormGenerator from '../Abstract Utils/FormGenerator' import { FormGenerator } from '../Abstract Utils/FormGenerator'
import { ResetComponent } from '../Abstract Utils/ResetComponent' import { ResetComponent } from '../Abstract Utils/ResetComponent'
@ -28,22 +28,24 @@ const AlbumCreate = ({
] ]
let inputList = [ let inputList = [
{
type: 'info',
action: 'Create',
endpoint: 'Album'
},
{ {
type: 'text', type: 'text',
name: 'title', name: 'title',
endpoint: 'Album',
ref: titleInput ref: titleInput
}, },
{ {
type: 'text', type: 'text',
name: 'description', name: 'description',
endpoint: 'Album',
ref: descriptionInput ref: descriptionInput
}, },
{ {
type: 'file', type: 'file',
name: 'image', name: 'image',
endpoint: 'Album',
fileType: 'image', fileType: 'image',
dropInfo: imageInfo, dropInfo: imageInfo,
setDropInfo: setImageInfo, setDropInfo: setImageInfo,

View File

@ -23,10 +23,14 @@ const AlbumDelete = ({
] ]
let inputList = [ let inputList = [
{
type: 'info',
action: 'Delete',
endpoint: 'Album'
},
{ {
type: 'text', type: 'text',
name: 'idDelete', name: 'id',
endpoint: 'Album',
ref: idInput ref: idInput
} }
] ]

View File

@ -24,10 +24,14 @@ const AlbumGetOne = ({
] ]
let inputList = [ let inputList = [
{
type: 'info',
action: 'GetOne',
endpoint: 'Album'
},
{ {
type: 'text', type: 'text',
name: 'idGetOne', name: 'id',
endpoint: 'Album',
ref: getOneInput ref: getOneInput
} }
] ]

View File

@ -2,6 +2,9 @@ import React, { useState, useEffect } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { updateAlbum } from '../../../../../../stores/album/duck/operations' import { updateAlbum } from '../../../../../../stores/album/duck/operations'
import { FormGenerator } from '../Abstract Utils/FormGenerator'
import { ResetComponent } from '../Abstract Utils/ResetComponent'
import { validate } from '../Abstract Utils/AbstractUpdate'
const AlbumUpdate = ({ const AlbumUpdate = ({
@ -13,41 +16,62 @@ const AlbumUpdate = ({
}) => { }) => {
const [message, setMessage] = useState('') const [message, setMessage] = useState('')
const [image, setImage] = useState('')
const [imageInfo, setImageInfo] = useState('Drop/Click\nfor upload album image...')
const idInput = React.createRef() const idInput = React.createRef()
const userInput = React.createRef() const userInput = React.createRef()
const titleInput = React.createRef() const titleInput = React.createRef()
const descriptionInput = React.createRef() const descriptionInput = React.createRef()
const imageInput = React.createRef()
const update = (event) => { let refList = [
event.preventDefault() idInput,
userInput,
titleInput,
descriptionInput
]
let id = idInput.current.value let inputList = [
let userID = userInput.current.value {
let title = titleInput.current.value type: 'info',
let description = descriptionInput.current.value action: 'Update',
let image = imageInput.current.value endpoint: 'Album'
},
if ( id !== '' ) { {
updateFetch(id, userID, title, description, image) type: 'text',
} else { name: 'id',
document.getElementById('idUpdateAlbumInput').focus() ref: idInput
},
{
type: 'text',
name: 'user',
ref: userInput
},
{
type: 'text',
name: 'title',
ref: titleInput
},
{
type: 'text',
name: 'description',
ref: descriptionInput
},
{
type: 'file',
name: 'image',
fileType: 'image',
dropInfo: imageInfo,
setDropInfo: setImageInfo,
file: image,
setFile: setImage
} }
} ]
const updateFetch = async (id, userID, title, description, image) => { const updateFetch = async ( refs ) => {
let album = {} let album = validate( inputList )
if ( userID !== '' )
album['user_id'] = userID
if ( title !== '' )
album['title'] = title
if ( description !== '' )
album['description'] = description
if ( image !== '' )
album['image'] = image
await updateAlbum( await updateAlbum(
id, album.id,
album, album,
user.token user.token
).then( response => { ).then( response => {
@ -55,62 +79,26 @@ const AlbumUpdate = ({
}) })
} }
useEffect( const resetState = () => {
() => { setConsoleHistory( consoleHistory + message )
if ( componentVisible ) { setComponentVisible( false )
document.getElementById('idUpdateAlbumInput').focus() setMessage('')
} else { }
activateConsoleInput()
}
if ( message !== '' ) {
userInput.current.value = ''
titleInput.current.value = ''
descriptionInput.current.value = ''
imageInput.current.value = ''
setConsoleHistory( consoleHistory + message )
setComponentVisible( false )
setMessage('')
}
}
)
return ( return (
<div> <div>
<form onSubmit={ update }> <FormGenerator
id: inputList={ inputList }
<input refList={ refList }
id='idUpdateAlbumInput' action={ updateFetch }
autoComplete='off' />
ref={ idInput } <ResetComponent
/> <br /> resetState={ resetState }
user_id: refList={ refList }
<input message={ message }
id='userUpdateAlbumInput' componentVisible={ componentVisible }
autoComplete='off' activateConsoleInput={ activateConsoleInput }
ref={ userInput } />
/> <br />
title:
<input
id='titleUpdateAlbumInput'
autoComplete='off'
ref={ titleInput }
/> <br />
description:
<input
id='descriptionUpdateAlbumInput'
autoComplete='off'
ref={ descriptionInput }
/> <br />
image:
<input
id='imageUpdateAlbumInput'
autoComplete='off'
ref={ imageInput }
/>
<button type='submit' />
</form>
</div> </div>
) )
} }