create abstract string mapping
parent
5ff1a6ae06
commit
3fa6d9cfbf
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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 )
|
||||
if (message !== '') {
|
||||
resetState()
|
||||
activateConsoleInput()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -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 !== '') {
|
||||
setConsoleHistory(consoleHistory + message)
|
||||
setComponentVisible(false)
|
||||
setOne(!oneRequest)
|
||||
setMessage('')
|
||||
}
|
||||
}
|
||||
)
|
||||
const resetState = () => {
|
||||
setConsoleHistory(consoleHistory + message)
|
||||
setComponentVisible(false)
|
||||
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>
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue