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 { } action - fetch method
*/
const FormGenerator = ({
export const FormGenerator = ({
inputList, refList,
action
}) => {

View File

@ -1,5 +1,5 @@
export const mapRowsToString = (objects, type, fields) => {
export const mapAllRowsToString = (objects, type, fields) => {
let list = '.' + type + '\n'
for (let i = 0; i < objects.length; i++) {
if (i !== objects.length - 1) {
@ -25,4 +25,18 @@ export const mapRowsToString = (objects, type, fields) => {
}
}
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 { useState } message - for check message trigger
* @param { useState } componentVisible - for focus first comp. input
* @param { string } firstComponentInput - ---""---
* @param { action } activateConsoleInput - activate general console input
*/
export const ResetComponent = ({
resetState, refList,
message,
componentVisible, firstComponentInput,
componentVisible,
activateConsoleInput
}) => {
useEffect( () => {
if ( componentVisible === true ) {
document.getElementById( firstComponentInput ).focus()
refList[0].current.focus()
if ( message !== '' ) {
refList.forEach( resetRefs )
resetState()
@ -73,4 +72,4 @@ export const ResetComponentWithoutInputs = ({
const resetRefs = ( element, array ) => {
element.current.value = ''
}
}

View File

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

View File

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

View File

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