2021-09-27 16:01:09 +03:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class CustomThemeSettingsServices extends Service {
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
@tracked settings = [];
|
|
|
|
|
2021-09-28 18:12:01 +03:00
|
|
|
get isDirty() {
|
|
|
|
const dirtySetting = this.settings.find(setting => setting.hasDirtyAttributes);
|
|
|
|
return !!dirtySetting;
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:28:26 +03:00
|
|
|
get keyValueObject() {
|
|
|
|
const keyValue = {};
|
|
|
|
|
|
|
|
this.settings.forEach((setting) => {
|
|
|
|
keyValue[setting.key] = setting.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return keyValue;
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:01:09 +03:00
|
|
|
load() {
|
|
|
|
return this.loadTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*loadTask() {
|
|
|
|
// unload stored settings and re-load from API so they always match active theme
|
|
|
|
// run is required here, see https://github.com/emberjs/data/issues/5447#issuecomment-845672812
|
|
|
|
run(() => this.store.unloadAll('custom-theme-setting'));
|
|
|
|
|
|
|
|
const settings = yield this.store.findAll('custom-theme-setting');
|
|
|
|
this.settings = settings;
|
|
|
|
|
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*saveTask() {
|
|
|
|
// save all records in a single request to `/custom_theme_settings`
|
|
|
|
const listRecord = this.store.createRecord('custom-theme-setting-list', {customThemeSettings: this.settings});
|
|
|
|
yield listRecord.save();
|
|
|
|
|
|
|
|
// don't keep references to individual settings around
|
|
|
|
this.store.unloadRecord(listRecord);
|
|
|
|
|
|
|
|
return this.settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
rollback() {
|
|
|
|
run(() => this.store.unloadAll('custom-theme-setting'));
|
|
|
|
|
|
|
|
this.settings = [];
|
|
|
|
}
|
|
|
|
}
|