diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractGetOne.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractGetOne.js new file mode 100644 index 0000000..ebf7cbd --- /dev/null +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractGetOne.js @@ -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' + ) + } + }) + } +} \ No newline at end of file diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js index 2364e1b..63a6cb8 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js @@ -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 }) => { diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js index 987f885..3777b77 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/MapRowsToString.js @@ -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 } \ 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 6a8c5ed..c1b0c2e 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/ResetComponent.js @@ -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 = '' -} +} \ No newline at end of file diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js index 7a07383..1506cc4 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js @@ -88,7 +88,6 @@ const AlbumCreate = ({ refList={ refList } message={ message } componentVisible={ componentVisible } - firstComponentInput={ 'titleAlbumInput' } activateConsoleInput={ activateConsoleInput } /> diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js b/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js index 64a5064..ef54d37 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/GetAll.js @@ -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 ( diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/GetOne.js b/src/components/index/indexConsole/commands/fetchCommands/Album/GetOne.js index 1f1c08c..20a4e00 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/GetOne.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/GetOne.js @@ -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 (