diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js new file mode 100644 index 0000000..987f885 --- /dev/null +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js @@ -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 +} \ No newline at end of file diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js index 791c275..6a8c5ed 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js @@ -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() + } } }) diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js index fc659b0..64cb906 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js @@ -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' diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js b/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js index 8d9a28a..64a5064 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js @@ -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 (
+
) } diff --git a/src/stores/store.js b/src/stores/store.js index 1e5d72a..6430223 100644 --- a/src/stores/store.js +++ b/src/stores/store.js @@ -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))