1
0
Fork 0
mirror of https://gitlab.com/Alamantus/Readlebee.git synced 2025-09-02 17:24:37 +02:00

Compare commits

..

No commits in common. "fc643c63882af88585061feb8eb97d8274ff32d1" and "d687685a4c0627f27a2862d2d7c8e31b2d03b2f1" have entirely different histories.

8 changed files with 58 additions and 62 deletions

View file

@ -1,9 +1,8 @@
export const appListeners = (app, state, emitter) => {
emitter.on(state.events.DOMCONTENTLOADED, () => {
emitter.emit(state.events.DOMTITLECHANGE, app.siteConfig.siteName);
emitter.on('DOMContentLoaded', () => {
document.title = app.siteConfig.siteName;
// Emitter listeners
emitter.on(state.events.RENDER, callback => {
emitter.on('render', callback => {
// This is a dirty hack to get the callback to call *after* re-rendering.
if (callback && typeof callback === "function") {
setTimeout(() => {
@ -12,50 +11,17 @@ export const appListeners = (app, state, emitter) => {
}
});
emitter.on(state.events.SET_LANGUAGE, newLanguage => {
emitter.on('set-language', newLanguage => {
app.setSettingsItem('lang', newLanguage);
state.language = newLanguage;
state.i18n.fetchLocaleUI().then(() => {
emitter.emit(state.events.RENDER);
emitter.emit('render');
});
});
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
emitter.emit('render'); // This should hopefully only run once after the DOM is loaded. It prevents routing issues where 'render' hasn't been defined yet
});
})
});

View file

@ -1,9 +1,6 @@
import { I18n } from "./i18n";
export const appState = (app, state, emitter) => {
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 = {};
state.isLoggedIn = false;

View file

@ -13,7 +13,7 @@ export const aboutView = (state, emit, i18n) => {
community.innerHTML = i18n.pages.community;
}
if (promises.length > 0) {
Promise.all(promises).then(fulfilled => emit(state.events.RENDER));
Promise.all(promises).then(fulfilled => emit('render'));
}
return [
content,

View file

@ -34,7 +34,7 @@ export class LoginController extends ViewController {
this.state.fieldValues.loginEmail = '';
this.state.fieldValues.loginPassword = '';
this.emit(this.appState.events.RENDER);
this.emit('render');
}
clearCreateAccountForm () {
@ -44,7 +44,7 @@ export class LoginController extends ViewController {
this.state.fieldValues.createPassword = '';
this.state.fieldValues.createConfirm = '';
this.emit(this.appState.events.RENDER);
this.emit('render');
}
validateLogin () {
@ -52,7 +52,7 @@ export class LoginController extends ViewController {
this.state.loginError = '';
this.state.isChecking = true;
this.emit(this.appState.events.RENDER, () => {
this.emit('render', () => {
const {
loginEmail,
loginPassword
@ -64,7 +64,7 @@ export class LoginController extends ViewController {
].includes('')) {
this.state.loginError = __('login.login_required_field_blank');
this.state.isChecking = false;
this.emit(this.appState.events.RENDER);
this.emit('render');
return;
}
@ -77,7 +77,7 @@ export class LoginController extends ViewController {
this.state.createError = '';
this.state.isChecking = true;
this.emit(this.appState.events.RENDER, () => {
this.emit('render', () => {
const {
createEmail,
createUsername,
@ -93,14 +93,14 @@ export class LoginController extends ViewController {
].includes('')) {
this.state.createError = __('login.create_required_field_blank');
this.state.isChecking = false;
this.emit(this.appState.events.RENDER);
this.emit('render');
return;
}
if (createPassword !== createConfirm) {
this.state.createError = __('login.create_password_confirm_mismatch');
this.state.isChecking = false;
this.emit(this.appState.events.RENDER);
this.emit('render');
return;
}
@ -130,7 +130,7 @@ export class LoginController extends ViewController {
console.error(response);
this.state.loginError = __(response.message);
this.state.isChecking = false;
this.emit(this.appState.events.RENDER);
this.emit('render');
return;
}
@ -169,7 +169,7 @@ export class LoginController extends ViewController {
console.error(response);
this.state.createError = __(response.message);
this.state.isChecking = false;
this.emit(this.appState.events.RENDER);
this.emit('render');
return;
}

View file

@ -66,7 +66,7 @@ export class SearchController extends ViewController {
search() {
if (this.hasQuery) {
this.state.done = false;
this.emit(this.appState.events.RENDER, () => {
this.emit('render', () => {
this.state.lastSearch = this.appState.query.for;
this.state.lastSource = this.state.searchSource;
this.state.lastBy = this.state.searchBy;
@ -79,7 +79,7 @@ export class SearchController extends ViewController {
this.state.results = responseJSON;
this.state.done = true;
})
.then(() => this.emit(this.appState.events.RENDER));
.then(() => this.emit('render'));
});
}
}
@ -143,16 +143,16 @@ export class SearchController extends ViewController {
shelvesPromise.then(() => {
console.log(shelfController.state.myShelves);
this.showShelves = true;
this.emit(this.appState.events.RENDER);
this.emit('render');
});
}
addToShelf(bookData, shelfId) {
const { ADD_TO_SHELF, RENDER } = this.appState.events;
this.emit(ADD_TO_SHELF, bookData, shelfId, (result) => {
const shelfController = new ShelvesController(this.appState, this.i18n);
shelfController.addItemToShelf(bookData, shelfId).then(result => {
console.log(result);
this.showShelves = false;
this.emit(RENDER);
this.emit('render');
});
}
}

View file

@ -42,4 +42,37 @@ 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,
}),
})
}
}

View file

@ -24,7 +24,7 @@ export const shelfView = (shelvesController, emit) => {
if (typeof shelvesController.state.loadedShelves[shelvesController.targetShelf] === 'undefined') {
shelvesController.getTargetShelf().then(() => {
emit(shelvesController.appState.events.RENDER);
emit('render');
});
}
@ -77,7 +77,7 @@ export const shelfView = (shelvesController, emit) => {
<div class="third sixth-700">
<button class="pseudo" onclick=${() => {
delete shelvesController.state.loadedShelves[shelvesController.targetShelf];
emit(shelvesController.appState.events.RENDER);
emit('render');
}}>
Reload <i class="icon-reload"></i>
</button>

View file

@ -22,7 +22,7 @@ export const userShelvesView = (shelvesController, emit) => {
if (shelvesController.state.myShelves.length <= 0) {
shelvesController.getUserShelves().then(() => {
emit(shelvesController.appState.events.RENDER);
emit('render');
});
}