2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
2017-04-11 16:39:45 +03:00
|
|
|
import moment from 'moment';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
2019-11-04 09:38:30 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2019-11-04 09:38:30 +03:00
|
|
|
feature: service(),
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
post: null,
|
|
|
|
saveType: null,
|
|
|
|
|
|
|
|
// used to set minDate in datepicker
|
|
|
|
_minDate: null,
|
|
|
|
_publishedAtBlogTZ: null,
|
|
|
|
|
|
|
|
'data-test-publishmenu-draft': true,
|
|
|
|
|
|
|
|
didInsertElement() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.set('publishedAtBlogTZ', this.get('post.publishedAtUTC'));
|
2017-04-11 16:39:45 +03:00
|
|
|
this.send('setSaveType', 'publish');
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
setSaveType(type) {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.saveType !== type) {
|
2017-04-11 16:39:45 +03:00
|
|
|
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);
|
2019-03-06 16:53:54 +03:00
|
|
|
this.setSaveType(type);
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
// when publish: switch to now to avoid validation errors
|
2017-07-20 13:46:31 +03:00
|
|
|
// when schedule: switch to last valid or new minimum scheduled date
|
2017-04-11 16:39:45 +03:00
|
|
|
if (type === 'publish') {
|
|
|
|
if (!hasDateError && !hasTimeError) {
|
|
|
|
this._publishedAtBlogTZ = this.get('post.publishedAtBlogTZ');
|
|
|
|
} else {
|
|
|
|
this._publishedAtBlogTZ = this.get('post.publishedAtUTC');
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.set('publishedAtBlogTZ', this.get('post.publishedAtUTC'));
|
2017-04-11 16:39:45 +03:00
|
|
|
} else {
|
|
|
|
if (!this._publishedAtBlogTZ || moment(this._publishedAtBlogTZ).isBefore(minDate)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.set('publishedAtBlogTZ', minDate);
|
2017-04-11 16:39:45 +03:00
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.set('publishedAtBlogTZ', this._publishedAtBlogTZ);
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.post.validate();
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setDate(date) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2017-04-11 16:39:45 +03:00
|
|
|
let dateString = moment(date).format('YYYY-MM-DD');
|
|
|
|
|
|
|
|
post.set('publishedAtBlogDate', dateString);
|
|
|
|
return post.validate();
|
|
|
|
},
|
|
|
|
|
|
|
|
setTime(time) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
post.set('publishedAtBlogTime', time);
|
|
|
|
return post.validate();
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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');
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
});
|