Ghost/ghost/admin/app/components/gh-publishmenu-draft.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

75 lines
2.5 KiB
JavaScript

import Component from '@ember/component';
import moment from 'moment';
import {isEmpty} from '@ember/utils';
export default Component.extend({
post: null,
saveType: null,
// used to set minDate in datepicker
_minDate: null,
_publishedAtBlogTZ: null,
'data-test-publishmenu-draft': true,
didInsertElement() {
this.post.set('publishedAtBlogTZ', this.get('post.publishedAtUTC'));
this.send('setSaveType', 'publish');
},
actions: {
setSaveType(type) {
if (this.saveType !== type) {
let hasDateError = !isEmpty(this.get('post.errors').errorsFor('publishedAtBlogDate'));
let hasTimeError = !isEmpty(this.get('post.errors').errorsFor('publishedAtBlogTime'));
let minDate = this._getMinDate();
this.set('_minDate', minDate);
this.setSaveType(type);
// when publish: switch to now to avoid validation errors
// when schedule: switch to last valid or new minimum scheduled date
if (type === 'publish') {
if (!hasDateError && !hasTimeError) {
this._publishedAtBlogTZ = this.get('post.publishedAtBlogTZ');
} else {
this._publishedAtBlogTZ = this.get('post.publishedAtUTC');
}
this.post.set('publishedAtBlogTZ', this.get('post.publishedAtUTC'));
} else {
if (!this._publishedAtBlogTZ || moment(this._publishedAtBlogTZ).isBefore(minDate)) {
this.post.set('publishedAtBlogTZ', minDate);
} else {
this.post.set('publishedAtBlogTZ', this._publishedAtBlogTZ);
}
}
this.post.validate();
}
},
setDate(date) {
let post = this.post;
let dateString = moment(date).format('YYYY-MM-DD');
post.set('publishedAtBlogDate', dateString);
return post.validate();
},
setTime(time) {
let post = this.post;
post.set('publishedAtBlogTime', time);
return post.validate();
}
},
// API only accepts dates at least 2 mins in the future, default the
// scheduled date 5 mins in the future to avoid immediate validation errors
_getMinDate() {
return moment.utc().add(5, 'minutes');
}
});