Ghost/ghost/admin/app/services/custom-theme-settings.js
Kevin Ansfield 1e8e0485e3 Added customThemeSettings service with all-records-in-one save request
refs https://github.com/TryGhost/Team/issues/1070

- adds a `customThemeSettings` service that handles overall setting loading and saving to avoid components having to know any of the intricacies of the model setup
- adds `custom-theme-setting-list` model so that we can save multiple setting records as embedded relations
  - custom adapter ensures requests go to the `/custom_theme_settings` base route as a `PUT` request
  - custom serializer drops the default `models: []` wrapper in the save request data so the format matches the `PUT /settings` endpoint, then converts the response to look like a `custom-theme-setting-list` response
2021-09-28 16:50:28 +01:00

50 lines
1.4 KiB
JavaScript

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 = [];
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 = [];
}
}