2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-10-07 16:23:21 +03:00
|
|
|
import {action} from '@ember/object';
|
2020-01-30 18:35:36 +03:00
|
|
|
import {observes} from '@ember-decorators/object';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const DEFAULT_SETTINGS = {
|
|
|
|
expanded: {
|
|
|
|
posts: true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class NavigationService extends Service {
|
|
|
|
@service session;
|
|
|
|
|
|
|
|
@tracked settings;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2020-05-18 15:42:01 +03:00
|
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS);
|
2020-01-30 18:35:36 +03:00
|
|
|
this.updateSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line ghost/ember/no-observers
|
2021-07-08 16:37:31 +03:00
|
|
|
@observes('session.{isAuthenticated,user}', 'session.user.accessibility')
|
2020-01-30 18:35:36 +03:00
|
|
|
async updateSettings() {
|
2020-02-03 15:27:18 +03:00
|
|
|
// avoid fetching user before authenticated otherwise the 403 can fire
|
|
|
|
// during authentication and cause errors during setup/signin
|
2021-07-08 16:37:31 +03:00
|
|
|
if (!this.session.isAuthenticated || !this.session.user) {
|
2020-02-03 15:27:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:37:31 +03:00
|
|
|
let userSettings = JSON.parse(this.session.user.accessibility || '{}') || {};
|
2020-01-30 18:35:36 +03:00
|
|
|
this.settings = userSettings.navigation || Object.assign({}, DEFAULT_SETTINGS);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async toggleExpansion(key) {
|
|
|
|
if (!this.settings.expanded) {
|
|
|
|
this.settings.expanded = {};
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.expanded[key] = !this.settings.expanded[key];
|
2020-01-30 18:35:36 +03:00
|
|
|
|
|
|
|
return await this._saveNavigationSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _saveNavigationSettings() {
|
2021-07-08 16:37:31 +03:00
|
|
|
let user = this.session.user;
|
2020-05-18 15:45:59 +03:00
|
|
|
let userSettings = JSON.parse(user.get('accessibility')) || {};
|
2020-01-30 18:35:36 +03:00
|
|
|
userSettings.navigation = this.settings;
|
|
|
|
user.set('accessibility', JSON.stringify(userSettings));
|
|
|
|
return user.save();
|
|
|
|
}
|
|
|
|
}
|