class UserNotificationManager {
|
constructor(){
|
this.resetFilters();
|
this.activeTab = 'all';
|
this.isLoading = false;
|
this.loading = window.jvbLoading;
|
|
this.container = document.querySelector('.container');
|
this.grid = this.container.querySelector('.notifications-list');
|
|
|
|
this.tabs = new window.jvbTabs(this.container, {
|
all: () => {
|
this.resetFilters();
|
this.activeTab = 'all';
|
this.loadNotifications();
|
},
|
favourite: () =>{
|
this.resetFilters();
|
this.activeTab = 'favourite';
|
this.filters.content = 'favourite';
|
this.loadNotifications();
|
},
|
artist: () =>{
|
this.resetFilters();
|
this.activeTab = 'artist';
|
this.filters.content = 'artist';
|
this.loadNotifications();
|
},
|
shop: () =>{
|
this.resetFilters();
|
this.activeTab = 'shop';
|
this.filters.content = 'shop';
|
this.loadNotifications();
|
},
|
event: () =>{
|
this.resetFilters();
|
this.activeTab = 'favourite';
|
this.filters.content = 'favourite';
|
this.loadNotifications();
|
},
|
news: () =>{
|
this.resetFilters();
|
this.activeTab = 'news';
|
this.filters.content = 'news';
|
this.loadNotifications();
|
},
|
system: () =>{
|
this.resetFilters();
|
this.activeTab = 'system';
|
this.filters.content = 'system';
|
this.loadNotifications();
|
},
|
|
});
|
|
this.loadNotifications();
|
}
|
|
resetFilters(){
|
this.filters = {
|
content: 'all',
|
date: ''
|
};
|
this.hasMore = true;
|
}
|
|
async loadNotifications(reset = true) {
|
|
if(this.isLoading || !this.hasMore) return;
|
|
try {
|
this.isLoading = true;
|
this.loading.show();
|
|
if(reset){
|
this.filters.page = 1;
|
this.grid.classList.remove('empty');
|
}
|
const params = this.buildFilters();
|
console.log(this.filters);
|
console.log('Reset? ',this.reset);
|
const data = await this.cache.fetchWithCache(
|
`${jvbSettings.api}notifications?${params.toString()}`,
|
{
|
method: 'GET',
|
headers: {
|
'X-WP-Nonce': window.auth.getNonce(),
|
'X-Action-Nonce': jvbAdmin.nonce,
|
}
|
},{
|
context: 'admin',
|
forceRefresh: true
|
}
|
);
|
|
console.log(data);
|
|
// // Process and render the favourites
|
// this.renderItems(data.items || [], this.filters.page > 1);
|
// [
|
// this.hasMore,
|
// this.totalItems,
|
// this.maxPages
|
// ] = [
|
// data.has_more,
|
// data.total_items,
|
// data.total_pages
|
// ];
|
// if(this.hasMore){
|
// this.filters.page++;
|
// }
|
//
|
// this.setupInfiniteScroll();
|
|
return data;
|
} catch (error) {
|
this.handleError(error, 'loading notifications');
|
throw error;
|
} finally {
|
this.isLoading = false;
|
this.loading.hide();
|
}
|
|
}
|
|
buildFilters(){
|
//Clone to avoid modifying original
|
const filters = JSON.parse(JSON.stringify(this.filters));
|
|
let temp = {};
|
for(var[name, value] of Object.entries(filters)){
|
if(value !== false && value !== null){
|
temp[name] = value;
|
}
|
}
|
temp.context = 'admin';
|
temp.user = window.auth.getUser();
|
|
return new URLSearchParams(temp);
|
}
|
}
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
window.notificationsDash = new UserNotificationManager();
|
console.log(jvbSettings);
|
});
|