From 13e9dc08d79f6683e33a8f58cb8a6741401748db Mon Sep 17 00:00:00 2001 From: TBS093A Date: Mon, 17 Aug 2020 18:36:14 +0200 Subject: [PATCH] refactor input form generator && complete update abstract --- .../Abstract Utils/AbstractUpdate.js | 12 ++ .../Abstract Utils/FormGenerator.js | 33 ++-- .../commands/fetchCommands/Album/Create.js | 10 +- .../commands/fetchCommands/Album/Delete.js | 8 +- .../commands/fetchCommands/Album/GetOne.js | 8 +- .../commands/fetchCommands/Album/Update.js | 144 ++++++++---------- 6 files changed, 120 insertions(+), 95 deletions(-) create mode 100644 src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractUpdate.js diff --git a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractUpdate.js b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractUpdate.js new file mode 100644 index 0000000..b732968 --- /dev/null +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/AbstractUpdate.js @@ -0,0 +1,12 @@ +export const validate = ( inputs ) => { + let object = {} + for (let i = 0; i < inputs.length; i++) { + if ( + inputs[i]['type'] === 'text' + && inputs[i]['ref'] !== undefined + && inputs[i]['ref'].current.value !== '' + ) + object[ inputs[i]['name'] ] = inputs[i]['ref'].current.value + } + return object +} \ 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 63a6cb8..efba6d5 100644 --- a/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js +++ b/src/components/index/indexConsole/commands/fetchCommands/Abstract Utils/FormGenerator.js @@ -14,23 +14,32 @@ export const FormGenerator = ({ const handler = async (event) => { event.preventDefault() for ( let i = 0; i < refList.length; i++ ) { - if ( refList[i].current.value === '' ) { + if ( + refList[i].current.value === '' + && inputList[0].action !== 'Update' + || i === 0 && refList.length !== 1 + ) { refList[i].current.focus() } else if ( i === refList.length - 1 ) { await action( refList ) } } } + + let info return (
handler( event ) }> { inputList.map( (input, key) => { - - if ( input.type === 'text' ) { + + if ( input.type === 'info' ) { + info = input + } else if ( input.type === 'text' ) { return ( ) @@ -38,6 +47,7 @@ export const FormGenerator = ({ return ( ) @@ -54,19 +64,24 @@ export const FormGenerator = ({ * @param { * { * type: 'text', - * name: 'name', - * endpoint: 'Album', + * name: 'name', * ref: React.createRef() * } } input - basic text input + * @param { + * { + * type: 'info', + * action: 'Update' + * endpoint: 'Album' + * } } info - information about form */ const TextInputGenerator = ({ - input + input, info }) => { return (
{ input.name + ':' } @@ -87,7 +102,7 @@ const TextInputGenerator = ({ * } } input - */ const UploadInputGenerator = ({ - input + input, info }) => { const onLoadFile = async ( event ) => { @@ -129,7 +144,7 @@ const UploadInputGenerator = ({ { const [message, setMessage] = useState('') + const [image, setImage] = useState('') + const [imageInfo, setImageInfo] = useState('Drop/Click\nfor upload album image...') 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 refList = [ + idInput, + userInput, + titleInput, + descriptionInput + ] - 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() + let inputList = [ + { + type: 'info', + action: 'Update', + endpoint: 'Album' + }, + { + type: 'text', + name: 'id', + ref: idInput + }, + { + type: 'text', + name: 'user', + ref: userInput + }, + { + type: 'text', + name: 'title', + ref: titleInput + }, + { + type: 'text', + name: 'description', + ref: descriptionInput + }, + { + type: 'file', + name: 'image', + fileType: 'image', + dropInfo: imageInfo, + setDropInfo: setImageInfo, + file: image, + setFile: setImage } - } + ] - 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 + const updateFetch = async ( refs ) => { + let album = validate( inputList ) await updateAlbum( - id, + album.id, album, user.token ).then( response => { @@ -55,62 +79,26 @@ const AlbumUpdate = ({ }) } - 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 ) - setComponentVisible( false ) - setMessage('') - } - } - ) + const resetState = () => { + setConsoleHistory( consoleHistory + message ) + setComponentVisible( false ) + setMessage('') + } return (
- - id: -
- user_id: -
- title: -
- description: -
- image: - -
) }