2020-09-21 01:19:12 +02:00
const appListeners = ( app , state , emitter ) => {
2020-09-16 22:16:50 +02:00
emitter . on ( state . events . DOMCONTENTLOADED , ( ) => {
emitter . emit ( state . events . DOMTITLECHANGE , app . siteConfig . siteName ) ;
2020-09-16 22:00:45 +02:00
2019-09-25 20:32:52 +02:00
// Emitter listeners
2020-09-16 22:16:50 +02:00
emitter . on ( state . events . RENDER , callback => {
2019-09-25 20:32:52 +02:00
// This is a dirty hack to get the callback to call *after* re-rendering.
if ( callback && typeof callback === "function" ) {
setTimeout ( ( ) => {
callback ( ) ;
} , 50 ) ;
}
} ) ;
2020-09-16 22:16:50 +02:00
emitter . on ( state . events . SET _LANGUAGE , newLanguage => {
2019-09-25 20:32:52 +02:00
app . setSettingsItem ( 'lang' , newLanguage ) ;
state . language = newLanguage ;
2019-10-28 07:01:27 +01:00
state . i18n . fetchLocaleUI ( ) . then ( ( ) => {
2020-09-16 22:16:50 +02:00
emitter . emit ( state . events . RENDER ) ;
2019-10-28 07:01:27 +01:00
} ) ;
2019-09-25 20:32:52 +02:00
} ) ;
2019-09-26 20:56:28 +02:00
2020-09-17 00:54:29 +02:00
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 ) ) ;
} ) ;
2020-09-22 04:49:02 +02:00
if ( state . isFrontend ) {
2020-09-21 01:19:12 +02:00
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
} ) ;
2019-10-28 07:01:27 +01:00
} ) ;
2020-09-21 01:19:12 +02:00
}
2019-09-25 20:32:52 +02:00
} ) ;
2020-09-21 01:19:12 +02:00
}
module . exports = { appListeners } ;