From fc643c63882af88585061feb8eb97d8274ff32d1 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 16 Sep 2020 16:54:29 -0600 Subject: [PATCH] Move addItemToShelf into an event instead of class method --- app/appListeners.js | 33 +++++++++++++++++++++++++++++++++ app/appState.js | 3 ++- app/views/search/controller.js | 6 +++--- app/views/shelves/controller.js | 33 --------------------------------- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/app/appListeners.js b/app/appListeners.js index 176979e..984b1e6 100644 --- a/app/appListeners.js +++ b/app/appListeners.js @@ -20,6 +20,39 @@ export const appListeners = (app, state, emitter) => { }); }); + emitter.on(state.events.ADD_TO_SHELF, async (book, shelfId, callback = () => {}) => { + let bookId; + if(typeof book.source !== 'undefined' && typeof book.uri !== 'undefined') { + const bookSearchResult = await fetch('/api/books/getId', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(book), + }).then(response => response.json()); + + if (typeof bookSearchResult.error !== 'undefined') { + console.error(bookSearchResult); + return bookSearchResult; + } + + bookId = bookSearchResult; + } else { + bookId = book.id; + } + + return fetch('/api/shelf/addItem', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + shelfId, + bookId, + }), + }).then(result => callback(result)); + }); + state.i18n.fetchLocaleUI().then(() => { app.checkIfLoggedIn(state).then(isLoggedIn => { emitter.emit(state.events.RENDER); // This should hopefully only run once after the DOM is loaded. It prevents routing issues where 'render' hasn't been defined yet diff --git a/app/appState.js b/app/appState.js index 7d6eacd..40e2591 100644 --- a/app/appState.js +++ b/app/appState.js @@ -1,7 +1,8 @@ import { I18n } from "./i18n"; export const appState = (app, state, emitter) => { - state.events.SET_LANGUAGE = 'set-language'; + state.events.SET_LANGUAGE = 'setLanguage'; + state.events.ADD_TO_SHELF = 'addToShelf'; state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0]; state.viewStates = {}; diff --git a/app/views/search/controller.js b/app/views/search/controller.js index a4e24d2..1be489f 100644 --- a/app/views/search/controller.js +++ b/app/views/search/controller.js @@ -148,11 +148,11 @@ export class SearchController extends ViewController { } addToShelf(bookData, shelfId) { - const shelfController = new ShelvesController(this.appState, this.i18n); - shelfController.addItemToShelf(bookData, shelfId).then(result => { + const { ADD_TO_SHELF, RENDER } = this.appState.events; + this.emit(ADD_TO_SHELF, bookData, shelfId, (result) => { console.log(result); this.showShelves = false; - this.emit(this.appState.events.RENDER); + this.emit(RENDER); }); } } \ No newline at end of file diff --git a/app/views/shelves/controller.js b/app/views/shelves/controller.js index 9c66d1a..153f3ea 100644 --- a/app/views/shelves/controller.js +++ b/app/views/shelves/controller.js @@ -42,37 +42,4 @@ export class ShelvesController extends ViewController { this.state.loadedShelves[this.targetShelf] = shelf; }); } - - async addItemToShelf (book, shelfId) { - let bookId; - if (typeof book.source !== 'undefined' && typeof book.uri !== 'undefined') { - const bookSearchResult = await fetch('/api/books/getId', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(book), - }).then(response => response.json()); - - if (typeof bookSearchResult.error !== 'undefined') { - console.error(bookSearchResult); - return bookSearchResult; - } - - bookId = bookSearchResult; - } else { - bookId = book.id; - } - - return fetch('/api/shelf/addItem', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - shelfId, - bookId, - }), - }) - } } \ No newline at end of file