Ghost/ghost/admin/app/services/custom-theme-settings.js
Kevin Ansfield dab672c7b8 Wired up custom theme settings form to preview updates
refs https://github.com/TryGhost/Team/issues/1097

- added `customThemeSettings.keyValueObject` which returns a plain object with setting values assign to the setting keys on the object
- updated customize modal's preview data generation to use `customThemeSettings.keyValueObject` to assign a JSON-stringified version to the `custom` param on the `x-ghost-preview` header
- updated theme settings `<Select>` component to call the passed through `updatePreview()` action when the value changes
2021-09-30 13:30:06 +01:00

65 lines
1.7 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 = [];
get isDirty() {
const dirtySetting = this.settings.find(setting => setting.hasDirtyAttributes);
return !!dirtySetting;
}
get keyValueObject() {
const keyValue = {};
this.settings.forEach((setting) => {
keyValue[setting.key] = setting.value;
});
return keyValue;
}
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 = [];
}
}