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-24 16:20:51 +03:00
|
|
|
get showEmailSection() {
|
2021-10-21 15:07:56 +03:00
|
|
|
return this.args.canSendEmail && this.args.distributionAction !== 'publish';
|
2021-08-24 16:20:51 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.args.post.set('publishedAtBlogTZ', this.args.post.publishedAtUTC);
|
2022-01-22 03:05:05 +03:00
|
|
|
|
|
|
|
this._updateDatesForSaveType(this.args.saveType);
|
2021-07-09 00:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setSaveType(type) {
|
|
|
|
if (this.args.saveType !== type) {
|
2022-01-22 03:05:05 +03:00
|
|
|
this._updateDatesForSaveType(type);
|
2021-07-09 00:44:52 +03:00
|
|
|
this.args.setSaveType(type);
|
|
|
|
this.args.post.validate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:01:26 +03:00
|
|
|
@action
|
|
|
|
setDistributionAction(type) {
|
|
|
|
this.args.setDistributionAction(type);
|
|
|
|
}
|
|
|
|
|
2021-07-09 00:44:52 +03:00
|
|
|
@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
|
|
|
|
2022-01-22 03:05:05 +03:00
|
|
|
_updateDatesForSaveType(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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
} else {
|
|
|
|
this._publishedAtBlogTZ = this.args.post.publishedAtUTC;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|