modify abstract utils && add AbstractGetOne

develop
TBS093A 2020-08-17 15:09:31 +02:00
parent 436c1bbc27
commit aceb9d92c5
7 changed files with 112 additions and 51 deletions

View File

@ -0,0 +1,34 @@
/**
* Get one row && mapping
* @param {*} refList
* @param {*} consoleHistory
* @param {*} setConsoleHistory
* @param {*} setMessage
* @param {*} getOneAction
* @param {*} mapping
*/
export const AbstractGetOne = async (
refList,
consoleHistory, setConsoleHistory,
setMessage,
getOneAction,
mapping,
) => {
let inputValue = refList[0].current.value
setConsoleHistory( consoleHistory + 'album id: ' + inputValue + '\n')
if ( inputValue >= 0 ) {
await getOneAction( inputValue ).then( response => {
if ( response['info'] !== 'Not found.' ){
setMessage(
mapping(
response['response']
) + response['info'] + '\n'
)
} else{
setMessage(
response['info'] + '\n'
)
}
})
}
}

View File

@ -6,7 +6,7 @@ import React from 'react'
* @param { [] } refList - react ref objects list for handler validation * @param { [] } refList - react ref objects list for handler validation
* @param { } action - fetch method * @param { } action - fetch method
*/ */
const FormGenerator = ({ export const FormGenerator = ({
inputList, refList, inputList, refList,
action action
}) => { }) => {

View File

@ -1,5 +1,5 @@
export const mapRowsToString = (objects, type, fields) => { export const mapAllRowsToString = (objects, type, fields) => {
let list = '.' + type + '\n' let list = '.' + type + '\n'
for (let i = 0; i < objects.length; i++) { for (let i = 0; i < objects.length; i++) {
if (i !== objects.length - 1) { if (i !== objects.length - 1) {
@ -25,4 +25,18 @@ export const mapRowsToString = (objects, type, fields) => {
} }
} }
return list return list
}
export const mapRowToString = ( object, fields ) => {
let row = ''
for (let i = 0; i < fields.length; i++) {
if ( i === 0 ) {
row += object[ fields[i] ] + '\n'
} else if ( i !== fields.length - 1 ) {
row += '├── ' + fields[i] + ': ' + object[ fields[i] ] + '\n'
} else {
row += '└── ' + fields[i] + ': ' + object[ fields[i] ] + '\n'
}
}
return row
} }

View File

@ -6,19 +6,18 @@ import React, { useState, useEffect } from "react"
* @param { [] } refList - reset react refs * @param { [] } refList - reset react refs
* @param { useState } message - for check message trigger * @param { useState } message - for check message trigger
* @param { useState } componentVisible - for focus first comp. input * @param { useState } componentVisible - for focus first comp. input
* @param { string } firstComponentInput - ---""---
* @param { action } activateConsoleInput - activate general console input * @param { action } activateConsoleInput - activate general console input
*/ */
export const ResetComponent = ({ export const ResetComponent = ({
resetState, refList, resetState, refList,
message, message,
componentVisible, firstComponentInput, componentVisible,
activateConsoleInput activateConsoleInput
}) => { }) => {
useEffect( () => { useEffect( () => {
if ( componentVisible === true ) { if ( componentVisible === true ) {
document.getElementById( firstComponentInput ).focus() refList[0].current.focus()
if ( message !== '' ) { if ( message !== '' ) {
refList.forEach( resetRefs ) refList.forEach( resetRefs )
resetState() resetState()
@ -73,4 +72,4 @@ export const ResetComponentWithoutInputs = ({
const resetRefs = ( element, array ) => { const resetRefs = ( element, array ) => {
element.current.value = '' element.current.value = ''
} }

View File

@ -88,7 +88,6 @@ const AlbumCreate = ({
refList={ refList } refList={ refList }
message={ message } message={ message }
componentVisible={ componentVisible } componentVisible={ componentVisible }
firstComponentInput={ 'titleAlbumInput' }
activateConsoleInput={ activateConsoleInput } activateConsoleInput={ activateConsoleInput }
/> />
</div> </div>

View File

@ -3,7 +3,7 @@ import { connect } from 'react-redux'
import { getAllAlbum } from '../../../../../../stores/album/duck/operations' import { getAllAlbum } from '../../../../../../stores/album/duck/operations'
import { ResetComponentWithoutInputs } from '../Abstract Utils/ResetComponent' import { ResetComponentWithoutInputs } from '../Abstract Utils/ResetComponent'
import { mapRowsToString } from '../Abstract Utils/MapRowsToString' import { mapAllRowsToString } from '../Abstract Utils/MapRowsToString'
const AlbumGetAll = ({ const AlbumGetAll = ({
album, album,
@ -28,7 +28,7 @@ const AlbumGetAll = ({
'user_id', 'user_id',
'url_code' 'url_code'
] ]
return mapRowsToString( albums, 'albums', mapFields ) return mapAllRowsToString( albums, 'albums', mapFields )
} }
return ( return (

View File

@ -2,7 +2,10 @@ import React, { useState, useEffect } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { getOneAlbum } from '../../../../../../stores/album/duck/operations' import { getOneAlbum } from '../../../../../../stores/album/duck/operations'
import { ResetComponent } from '../Abstract Utils/ResetComponent'
import { FormGenerator } from '../Abstract Utils/FormGenerator'
import { mapRowToString } from '../Abstract Utils/MapRowsToString'
import { AbstractGetOne } from '../Abstract Utils/AbstractGetOne'
const AlbumGetOne = ({ const AlbumGetOne = ({
album, album,
@ -16,50 +19,65 @@ const AlbumGetOne = ({
const getOneInput = React.createRef() const getOneInput = React.createRef()
const getOne = (event) => { let refList = [
event.preventDefault() getOneInput
let inputValue = getOneInput.current.value ]
setConsoleHistory( consoleHistory + 'album id: ' + inputValue + '\n')
if ( inputValue >= 0 ) { let inputList = [
getOneAlbum( inputValue ).then( response => { {
if ( response['info'] !== 'Not found.' ){ type: 'text',
setMessage( name: 'id',
response['response'].title + '\n' endpoint: 'Album',
+ '├── id: ' + response['response'].id + '\n' ref: getOneInput
+ '├── user id: ' + response['response'].user_id + '\n'
+ '└── url: ' + response['response'].url_code + '\n'
+ response['info'] + '\n'
)
} else{
setMessage(
response['info'] + '\n'
)
}
})
} }
]
const mapAlbumToString = ( album ) => {
let mapFields = [
'title',
'id',
'user_id',
'url_code'
]
return mapRowToString( album, mapFields )
} }
useEffect( const getOneAlbumFetch = (event) => {
() => { AbstractGetOne(
if ( componentVisible ) { refList,
document.getElementById('getOneAlbumInput').focus() consoleHistory,
} else { setConsoleHistory,
activateConsoleInput() setMessage,
} getOneAlbum,
if ( message !== '' ) { mapAlbumToString
)
}
getOneInput.current.value = '' const resetState = () => {
setConsoleHistory( consoleHistory + message )
setConsoleHistory( consoleHistory + message ) setComponentVisible( false )
setComponentVisible( false ) setMessage('')
setMessage('') }
}
}
)
return ( return (
<div> <div>
<form onSubmit={ getOne }> <FormGenerator
inputList={ inputList }
refList={ refList }
action={ getOneAlbumFetch }
/>
<ResetComponent
resetState={ resetState }
refList={ refList }
message={ message }
componentVisible={ componentVisible }
activateConsoleInput={ activateConsoleInput }
/>
</div>
)
}
{/* <form onSubmit={ getOne }>
album id: album id:
<input <input
id='getOneAlbumInput' id='getOneAlbumInput'
@ -67,10 +85,7 @@ const AlbumGetOne = ({
ref={ getOneInput } ref={ getOneInput }
/> />
<button type='submit' /> <button type='submit' />
</form> </form> */}
</div>
)
}
const mapStateToProps = state => ({ const mapStateToProps = state => ({
album: state.album album: state.album