upgrade abstraction -> AbstractDelete.js && modify GetOne.js

develop
TBS093A 2020-08-17 15:35:44 +02:00
parent aceb9d92c5
commit 0bdc490e38
5 changed files with 81 additions and 55 deletions

View File

@ -0,0 +1,27 @@
/**
* Delete one row
* @param {*} refList
* @param {*} consoleHistory
* @param {*} setConsoleHistory
* @param {*} setMessage
* @param {*} deleteAction
* @param {*} token
*/
export const AbstractDelete = async (
refList,
consoleHistory, setConsoleHistory,
setMessage,
deleteAction,
token
) => {
let id = refList[0].current.value
setConsoleHistory( consoleHistory + 'id: ' + id + '\n')
if ( id >= 0 ) {
deleteAction(
id,
token
).then( response => {
setMessage( response['info'] + '\n' )
})
}
}

View File

@ -15,7 +15,7 @@ export const AbstractGetOne = async (
mapping, mapping,
) => { ) => {
let inputValue = refList[0].current.value let inputValue = refList[0].current.value
setConsoleHistory( consoleHistory + 'album id: ' + inputValue + '\n') setConsoleHistory( consoleHistory + 'id: ' + inputValue + '\n')
if ( inputValue >= 0 ) { if ( inputValue >= 0 ) {
await getOneAction( inputValue ).then( response => { await getOneAction( inputValue ).then( response => {
if ( response['info'] !== 'Not found.' ){ if ( response['info'] !== 'Not found.' ){

View File

@ -2,7 +2,9 @@ import React, { useState, useEffect } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { deleteAlbum } from '../../../../../../stores/album/duck/operations' import { deleteAlbum } from '../../../../../../stores/album/duck/operations'
import { AbstractDelete } from '../Abstract Utils/AbstractDelete'
import { FormGenerator } from '../Abstract Utils/FormGenerator'
import { ResetComponent } from '../Abstract Utils/ResetComponent'
const AlbumDelete = ({ const AlbumDelete = ({
user, user,
@ -16,49 +18,50 @@ const AlbumDelete = ({
const idInput = React.createRef() const idInput = React.createRef()
let refList = [
idInput
]
let inputList = [
{
type: 'text',
name: 'idDelete',
endpoint: 'Album',
ref: idInput
}
]
const deleteFetch = (event) => { const deleteFetch = (event) => {
event.preventDefault() AbstractDelete(
let id = idInput.current.value refList,
setConsoleHistory( consoleHistory + 'album id: ' + id + '\n') consoleHistory,
if ( id >= 0 ) { setConsoleHistory,
deleteAlbum( setMessage,
id, deleteAlbum,
user.token user.token
).then( response => { )
setMessage( response['info'] + '\n' )
})
}
} }
useEffect( const resetState = () => {
() => {
if ( componentVisible ) {
document.getElementById('idAlbumDeleteInput').focus()
} else {
activateConsoleInput()
}
if ( message !== '' ) {
idInput.current.value = ''
setConsoleHistory( consoleHistory + message ) setConsoleHistory( consoleHistory + message )
setComponentVisible( false ) setComponentVisible( false )
setMessage('') setMessage('')
} }
}
)
return ( return (
<div> <div>
<form onSubmit={ deleteFetch }> <FormGenerator
album id: inputList={ inputList }
<input refList={ refList }
id='idAlbumDeleteInput' action={ deleteFetch }
autoComplete='off' />
ref={ idInput } <ResetComponent
resetState={ resetState }
refList={ refList }
message={ message }
componentVisible={ componentVisible }
activateConsoleInput={ activateConsoleInput }
/> />
<button type='submit' />
</form>
</div> </div>
) )
} }

View File

@ -26,7 +26,7 @@ const AlbumGetOne = ({
let inputList = [ let inputList = [
{ {
type: 'text', type: 'text',
name: 'id', name: 'idGetOne',
endpoint: 'Album', endpoint: 'Album',
ref: getOneInput ref: getOneInput
} }
@ -42,7 +42,7 @@ const AlbumGetOne = ({
return mapRowToString( album, mapFields ) return mapRowToString( album, mapFields )
} }
const getOneAlbumFetch = (event) => { const getOneAlbumFetch = () => {
AbstractGetOne( AbstractGetOne(
refList, refList,
consoleHistory, consoleHistory,
@ -77,16 +77,6 @@ const AlbumGetOne = ({
) )
} }
{/* <form onSubmit={ getOne }>
album id:
<input
id='getOneAlbumInput'
autoComplete='off'
ref={ getOneInput }
/>
<button type='submit' />
</form> */}
const mapStateToProps = state => ({ const mapStateToProps = state => ({
album: state.album album: state.album
}) })

View File

@ -84,10 +84,16 @@ const responseGD = async (address, method, token) => {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
}) })
if ( method === 'GET' )
return await responseExceptions( return await responseExceptions(
await response.json(), await response.json(),
response.status response.status
) )
else
return await responseExceptions(
await response,
response.status
)
} catch (error) { } catch (error) {
return { info: error } return { info: error }
} }