2021-07-09 00:44:52 +03:00
|
|
|
import Component from '@glimmer/component';
|
2017-04-11 16:39:45 +03:00
|
|
|
import moment from 'moment';
|
2021-07-09 00:44:52 +03:00
|
|
|
import {action} from '@ember/object';
|
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';
|
2021-07-09 00:44:52 +03:00
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
export default class GhPublishMenuDraftComponent extends Component {
|
|
|
|
@service config;
|
|
|
|
@service feature;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
@service store;
|
2021-06-08 15:07:16 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
@tracked totalMemberCount = 0;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
// used to set minDate in datepicker
|
2021-07-09 00:44:52 +03:00
|
|
|
_minDate = null;
|
|
|
|
_publishedAtBlogTZ = null;
|
|
|
|
|
|
|
|
get disableEmailOption() {
|
|
|
|
// TODO: remove owner or admin check when editors can count members
|
2021-07-12 15:55:56 +03:00
|
|
|
return this.session.user.isAdmin && (this.totalMemberCount === 0 || this.countTotalMembersTask.isRunning);
|
2021-07-09 00:44:52 +03:00
|
|
|
}
|
|
|
|
|
2021-08-13 15:23:35 +03:00
|
|
|
get nextActionName() {
|
|
|
|
return this.args.post.get('emailOnly') ? 'send' : 'publish';
|
|
|
|
}
|
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.args.post.set('publishedAtBlogTZ', this.args.post.publishedAtUTC);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setSaveType(type) {
|
|
|
|
if (this.args.saveType !== type) {
|
|
|
|
let hasDateError = !isEmpty(this.args.post.errors.errorsFor('publishedAtBlogDate'));
|
|
|
|
let hasTimeError = !isEmpty(this.args.post.errors.errorsFor('publishedAtBlogTime'));
|
|
|
|
let minDate = this._getMinDate();
|
|
|
|
|
|
|
|
this._minDate = minDate;
|
|
|
|
this.args.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.args.post.publishedAtBlogTZ;
|
2017-04-11 16:39:45 +03:00
|
|
|
} else {
|
2021-07-09 00:44:52 +03:00
|
|
|
this._publishedAtBlogTZ = this.args.post.publishedAtUTC;
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
this.args.post.set('publishedAtBlogTZ', this.args.post.publishedAtUTC);
|
|
|
|
} else {
|
|
|
|
if (!this._publishedAtBlogTZ || moment(this._publishedAtBlogTZ).isBefore(minDate)) {
|
|
|
|
this.args.post.set('publishedAtBlogTZ', minDate);
|
|
|
|
} else {
|
|
|
|
this.args.post.set('publishedAtBlogTZ', this._publishedAtBlogTZ);
|
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
this.args.post.validate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDate(date) {
|
|
|
|
let post = this.args.post;
|
|
|
|
let dateString = moment(date).format('YYYY-MM-DD');
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
post.set('publishedAtBlogDate', dateString);
|
|
|
|
return post.validate();
|
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
@action
|
|
|
|
setTime(time) {
|
|
|
|
let post = this.args.post;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
post.set('publishedAtBlogTime', time);
|
|
|
|
return post.validate();
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
@task
|
|
|
|
*countTotalMembersTask() {
|
2021-06-08 15:07:16 +03:00
|
|
|
const user = yield this.session.user;
|
|
|
|
|
2021-07-12 15:55:56 +03:00
|
|
|
if (user.isAdmin) {
|
2021-06-08 15:07:16 +03:00
|
|
|
const result = yield this.store.query('member', {limit: 1, filter: 'subscribed:true'});
|
2021-07-09 00:44:52 +03:00
|
|
|
this.totalMemberCount = result.meta.pagination.total;
|
2021-06-08 15:07:16 +03:00
|
|
|
}
|
2021-07-09 00:44:52 +03:00
|
|
|
}
|
2021-06-08 15:07:16 +03:00
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
// API only accepts dates at least 2 mins in the future, default the
|
2018-01-11 20:43:23 +03:00
|
|
|
// 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
|
|
|
}
|
2021-07-09 00:44:52 +03:00
|
|
|
}
|