diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js index 7c41465..d494102 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/Create.js @@ -25,9 +25,9 @@ const AlbumCreate = ({ let image = imageInput.current.value if ( title !== '' && description !== '' && image !== '' ) { createFetch(title, description, image) - } else if ( description !== '' && image !== '' ) { + } if ( description !== '' && image !== '' ) { document.getElementById('descriptionAlbumInput').focus() - } else if ( image !== '' ) { + } if ( image !== '' ) { document.getElementById('imageAlbumInput').focus() } } @@ -86,7 +86,7 @@ const AlbumCreate = ({ descriptionInput.current.value = '' imageInput.current.value = '' - setConsoleHistory( consoleHistory + message ) + setConsoleHistory( consoleHistory + message + '\n' ) setComponentVisible( false ) setMessage('') } @@ -125,7 +125,7 @@ const mapStateToProps = state => ({ }) const mapDispatchToProps = dispatch => ({ - createAlbum: (album, token) => dispatch( createAlbum(album, token) ) + createAlbum: (album, token) => createAlbum(album, token) }) export default connect(mapStateToProps, mapDispatchToProps)(AlbumCreate) \ No newline at end of file diff --git a/src/components/index/indexConsole/commands/fetchCommands/Album/Update.js b/src/components/index/indexConsole/commands/fetchCommands/Album/Update.js index e69de29..439179a 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Album/Update.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Album/Update.js @@ -0,0 +1,128 @@ +import React, { useState, useEffect } from 'react' +import { connect } from 'react-redux' + +import { updateAlbum } from '../../../../../../stores/album/duck/operations' + + +const AlbumUpdate = ({ + user, + updateAlbum, + consoleHistory, setConsoleHistory, + componentVisible, setComponentVisible, + activateConsoleInput +}) => { + + const [message, setMessage] = useState('') + + const idInput = React.createRef() + const userInput = React.createRef() + const titleInput = React.createRef() + const descriptionInput = React.createRef() + const imageInput = React.createRef() + + const update = (event) => { + event.preventDefault() + + let id = idInput.current.value + let userID = userInput.current.value + let title = titleInput.current.value + let description = descriptionInput.current.value + let image = imageInput.current.value + + if ( id !== '' ) { + updateFetch(id, userID, title, description, image) + } else { + document.getElementById('idUpdateAlbumInput').focus() + } + } + + const updateFetch = async (id, userID, title, description, image) => { + let album = {} + if ( userID !== '' ) + album['user_id'] = userID + if ( title !== '' ) + album['title'] = title + if ( description !== '' ) + album['description'] = description + if ( image !== '' ) + album['image'] = image + await updateAlbum( + id, + album, + user.token + ).then( () => { + setMessage('album update success') + }).catch( () => { + setMessage('album update failed') + }) + } + + useEffect( + () => { + if ( componentVisible ) { + document.getElementById('idUpdateAlbumInput').focus() + } else { + activateConsoleInput() + } + if ( message !== '' ) { + + userInput.current.value = '' + titleInput.current.value = '' + descriptionInput.current.value = '' + imageInput.current.value = '' + + setConsoleHistory( consoleHistory + message + '\n' ) + setComponentVisible( false ) + setMessage('') + } + } + ) + + return ( +
+
+ id: +
+ user_id: +
+ title: +
+ description: +
+ image: + +
+ ) +} + +const mapStateToProps = state => ({ + user: state.user +}) + +const mapDispatchToProps = dispatch => ({ + updateAlbum: (id, album, token) => updateAlbum(id, album, token) +}) + +export default connect(mapStateToProps, mapDispatchToProps)(AlbumUpdate) \ No newline at end of file diff --git a/src/components/index/indexConsole/indexConsole.js b/src/components/index/indexConsole/indexConsole.js index 073a959..a203a9b 100644 --- a/src/components/index/indexConsole/indexConsole.js +++ b/src/components/index/indexConsole/indexConsole.js @@ -8,11 +8,11 @@ import Logout from './commands/fetchCommands/Logout' import AlbumGetAll from './commands/fetchCommands/Album/GetAll' import AlbumGetOne from './commands/fetchCommands/Album/GetOne' import AlbumCreate from './commands/fetchCommands/Album/Create' +import AlbumUpdate from './commands/fetchCommands/Album/Update' import '../../../styles/general.scss' import { deleteAuth } from '../../../stores/user/duck/operations' -import { createAlbum } from '../../../stores/album/duck/operations' const IndexConsole = ({ user, @@ -74,6 +74,9 @@ const IndexConsole = ({ } else if ( inputValue === 'create album' ) { setConsoleHistory( consoleHistory + consoleUser ) setAlbumCreate( !albumCreate ) + } else if ( inputValue === 'update album' ) { + setConsoleHistory( consoleHistory + consoleUser ) + setAlbumUpdate( !albumUpdate ) } else if ( inputValue === 'clean' ){ setConsoleHistory( '' ) } else { @@ -167,6 +170,15 @@ const IndexConsole = ({ activateConsoleInput={ activateInput } /> +
+ +
{ ) } -export const updateAlbum = async (album, token) => { +export const updateAlbum = async (id, album, token) => { return await AppService._patch( - endpoint, + endpoint + id + '/', album, token )