create abstract string mapping

develop
TBS093A 2020-08-17 10:40:37 +02:00
parent 5ff1a6ae06
commit 3fa6d9cfbf
5 changed files with 60 additions and 54 deletions

View File

@ -0,0 +1,28 @@
export const mapRowsToString = (objects, type, fields) => {
let list = '.' + type + '\n'
for (let i = 0; i < objects.length; i++) {
if (i !== objects.length - 1) {
for (let j = 0; j < fields.length; j++) {
if ( j === 0 ) {
list += '├── ' + objects[i][ fields[j] ] + '\n'
} else if (j !== fields.length - 1) {
list += '│ ├── ' + fields[j] + ': ' + objects[i][ fields[j] ] + '\n'
} else {
list += '│ └── ' + fields[j] + ': ' + objects[i][ fields[j] ] + '\n'
}
}
} else {
for (let j = 0; j < fields.length; j++) {
if ( j === 0 ) {
list += '└── ' + objects[i][ fields[j] ] + '\n'
} else if (j !== fields.length - 1) {
list += ' ├── ' + fields[j] + ': ' + objects[i][ fields[j] ] + '\n'
} else {
list += ' └── ' + fields[j] + ': ' + objects[i][ fields[j] ] + '\n'
}
}
}
}
return list
}

View File

@ -50,10 +50,8 @@ export const ResetComponentWithoutInputs = ({
activateConsoleInput
}) => {
const [oneRequest, setOneRequest] = useState(false)
useEffect( () => {
if (componentVisible && oneRequest === false) {
if (componentVisible) {
fetchAction().then(response => {
setMessage(
mapObjectToString(
@ -61,13 +59,10 @@ export const ResetComponentWithoutInputs = ({
) + response['info'] + '\n'
)
})
setOneRequest( true )
} else {
activateConsoleInput()
}
if (message !== '') {
resetState()
setOneRequest( false )
activateConsoleInput()
}
}
})

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState } from 'react'
import { connect } from 'react-redux'
import { createAlbum } from '../../../../../../stores/album/duck/operations'

View File

@ -1,8 +1,9 @@
import React, { useState, useEffect } from 'react'
import React, { useState } from 'react'
import { connect } from 'react-redux'
import { getAllAlbum } from '../../../../../../stores/album/duck/operations'
import { ResetComponentWithoutInputs } from '../Abstract Utils/ResetComponent'
import { mapRowsToString } from '../Abstract Utils/MapRowsToString'
const AlbumGetAll = ({
album,
@ -13,50 +14,34 @@ const AlbumGetAll = ({
}) => {
const [message, setMessage] = useState('')
const [oneRequest, setOne] = useState(false)
useEffect(
() => {
if (componentVisible && oneRequest === false) {
getAllAlbum().then(response => {
setMessage(
mapAlbumsToString(
response['response']
) + response['info'] + '\n'
)
})
setOne( true )
} else {
activateConsoleInput()
}
if (message !== '') {
const resetState = () => {
setConsoleHistory(consoleHistory + message)
setComponentVisible(false)
setOne(!oneRequest)
setMessage('')
}
}
)
const mapAlbumsToString = (albums) => {
let list = '.albums\n'
for (let i = 0; i < albums.length; i++) {
if (i !== albums.length - 1)
list += '├── ' + albums[i].title + '\n'
+ '│ ├── id: ' + albums[i].id + '\n'
+ '│ ├── user id: ' + albums[i].user_id + '\n'
+ '│ └── url: ' + albums[i].url_code + '\n'
else
list += '└── ' + albums[i].title + '\n'
+ ' ├── id: ' + albums[i].id + '\n'
+ ' ├── user id: ' + albums[i].user_id + '\n'
+ ' └── url: ' + albums[i].url_code + '\n'
}
return list
let mapFields = [
'title',
'id',
'user_id',
'url_code'
]
return mapRowsToString( albums, 'albums', mapFields )
}
return (
<div>
<ResetComponentWithoutInputs
resetState={ resetState }
fetchAction={ getAllAlbum }
mapObjectToString={ mapAlbumsToString }
message={ message }
setMessage={ setMessage }
componentVisible={ componentVisible }
activateConsoleInput={ activateConsoleInput }
/>
</div>
)
}

View File

@ -11,15 +11,13 @@ export const store = createStore(rootReducer, persistedState, applyMiddleware(th
store.subscribe( () => {
saveState({
user: store.getState().user,
album: store.getState().album
user: store.getState().user
})
})
store.subscribe( loadash.throttle( () => {
saveState({
user: store.getState().user,
album: store.getState().album
user: store.getState().user
})
}, 1000))