2019-12-22 04:16:52 +01:00
|
|
|
import { ViewController } from '../controller';
|
|
|
|
|
|
|
|
export class ShelvesController extends ViewController {
|
|
|
|
constructor(state, i18n) {
|
|
|
|
// Super passes state, view name, and default state to ViewController,
|
|
|
|
// which stores state in this.appState and the view controller's state to this.state
|
|
|
|
super(state, i18n, 'shelves', {
|
|
|
|
openModal: null, // state value for edit modals
|
|
|
|
myShelves: [], // array of objects in sort order with name, id, and deletability.
|
2020-01-08 06:50:58 +01:00
|
|
|
loadedShelves: {}, // object key is shelf id with name and shelfItems
|
2019-12-22 04:16:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// If using controller methods in an input's onchange or onclick instance,
|
|
|
|
// either bind the class's 'this' instance to the method first...
|
|
|
|
// or use `onclick=${() => controller.submit()}` to maintain the 'this' of the class instead.
|
|
|
|
}
|
|
|
|
|
|
|
|
get openModal () {
|
|
|
|
return this.state.openModal;
|
|
|
|
}
|
|
|
|
set openModal (modalId) {
|
|
|
|
this.state.openModal = modalId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get targetShelf () {
|
|
|
|
return typeof this.appState.query.shelf !== 'undefined' ? parseInt(this.appState.query.shelf) : null;
|
|
|
|
}
|
|
|
|
|
2020-01-08 06:50:58 +01:00
|
|
|
get targetDomain () {
|
|
|
|
return typeof this.appState.query.domain !== 'undefined' ? parseInt(this.appState.query.domain) : null;
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:16:52 +01:00
|
|
|
getUserShelves () {
|
2020-02-04 23:00:45 +01:00
|
|
|
return fetch('/api/shelf/getAll').then(response => response.json()).then(shelves => {
|
2019-12-24 00:39:03 +01:00
|
|
|
this.state.myShelves = shelves;
|
|
|
|
});
|
2019-12-22 04:16:52 +01:00
|
|
|
}
|
2019-12-26 22:36:04 +01:00
|
|
|
|
|
|
|
getTargetShelf () {
|
2020-01-12 04:35:35 +01:00
|
|
|
const target = this.targetShelf + '/' + (this.targetDomain !== null ? `${this.targetDomain}` : '');
|
2020-01-08 06:50:58 +01:00
|
|
|
return fetch('/api/shelf/get/' + target).then(response => response.json()).then(shelf => {
|
2019-12-26 22:36:04 +01:00
|
|
|
this.state.loadedShelves[this.targetShelf] = shelf;
|
|
|
|
});
|
|
|
|
}
|
2019-12-22 04:16:52 +01:00
|
|
|
}
|