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()
const deleteFetch = (event) => { let refList = [
event.preventDefault() idInput
let id = idInput.current.value ]
setConsoleHistory( consoleHistory + 'album id: ' + id + '\n')
if ( id >= 0 ) { let inputList = [
deleteAlbum( {
id, type: 'text',
user.token name: 'idDelete',
).then( response => { endpoint: 'Album',
setMessage( response['info'] + '\n' ) ref: idInput
})
} }
]
const deleteFetch = (event) => {
AbstractDelete(
refList,
consoleHistory,
setConsoleHistory,
setMessage,
deleteAlbum,
user.token
)
} }
useEffect( const resetState = () => {
() => { setConsoleHistory( consoleHistory + message )
if ( componentVisible ) { setComponentVisible( false )
document.getElementById('idAlbumDeleteInput').focus() setMessage('')
} else { }
activateConsoleInput()
}
if ( message !== '' ) {
idInput.current.value = ''
setConsoleHistory( consoleHistory + message )
setComponentVisible( false )
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 }
<button type='submit' /> refList={ refList }
</form> message={ message }
componentVisible={ componentVisible }
activateConsoleInput={ activateConsoleInput }
/>
</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"
} }
}) })
return await responseExceptions( if ( method === 'GET' )
await response.json(), return await responseExceptions(
response.status await response.json(),
) response.status
)
else
return await responseExceptions(
await response,
response.status
)
} catch (error) { } catch (error) {
return { info: error } return { info: error }
} }