Ghost/ghost/admin/app/components/editor/publish-options/publish-at.js
Kevin Ansfield 060d791a63 Removed need for .get() with settings service
no issue

The `settings` service has been a source of confusion when writing with modern Ember patterns because it's use of the deprecated `ProxyMixin` forced all property access/setting to go via `.get()` and `.set()` whereas the rest of the system has mostly (there are a few other uses of ProxyObjects remaining) eliminated the use of the non-native get/set methods.

- removed use of `ProxyMixin` in the `settings` service by grabbing the attributes off the setting model after fetching and using `Object.defineProperty()` to add native getters/setters that pass through to the model's getters/setters. Ember's autotracking automatically works across the native getters/setters so we can then use the service as if it was any other native object
- updated all code to use `settings.{attrName}` directly for getting/setting instead of `.get()` and `.set()`
- removed use of observer in the `customViews` service because it was being set up before the native properties had been added on the settings service meaning autotracking wasn't able to set up properly
2022-10-07 16:14:57 +01:00

60 lines
1.9 KiB
JavaScript

import Component from '@glimmer/component';
import moment from 'moment-timezone';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class PublishAtOption extends Component {
@service settings;
@action
setDate(selectedDate) {
// selectedDate is a Date object that contains the correct date string in the blog timezone
const selectedMoment = moment.tz(selectedDate, this.settings.timezone);
const {years, months, date} = selectedMoment.toObject();
// Create a new moment from existing scheduledAtUTC _in site timezone_.
// This ensures we're setting the date correctly because we don't need
// to account for the converted UTC date being yesterday/tomorrow.
const newDate = moment.tz(
this.args.publishOptions.scheduledAtUTC,
this.settings.timezone
);
newDate.set({years, months, date});
// converts back to UTC
this.args.publishOptions.setScheduledAt(newDate);
}
@action
setTime(time, event) {
const newDate = moment.tz(this.args.publishOptions.scheduledAtUTC, this.settings.timezone);
// used to reset the time value on blur if it's invalid
const oldTime = newDate.format('HH:mm');
if (!time) {
event.target.value = oldTime;
return;
}
if (time.match(/^\d:\d\d$/)) {
time = `0${time}`;
}
if (!time.match(/^\d\d:\d\d$/)) {
event.target.value = oldTime;
return;
}
const [hour, minute] = time.split(':').map(n => parseInt(n, 10));
if (isNaN(hour) || hour < 0 || hour > 23 || isNaN(minute) || minute < 0 || minute > 59) {
event.target.value = oldTime;
return;
}
newDate.set({hour, minute});
this.args.publishOptions.setScheduledAt(newDate);
}
}