Ghost/ghost/admin/app/components/gh-publishmenu-draft.js

106 lines
3.3 KiB
JavaScript
Raw Normal View History

import Component from '@glimmer/component';
import moment from 'moment';
import {action} from '@ember/object';
import {isEmpty} from '@ember/utils';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency-decorators';
import {tracked} from '@glimmer/tracking';
export default class GhPublishMenuDraftComponent extends Component {
@service config;
@service feature;
@service session;
@service settings;
@service store;
@tracked totalMemberCount = 0;
// used to set minDate in datepicker
_minDate = null;
_publishedAtBlogTZ = null;
get disableEmailOption() {
// TODO: remove owner or admin check when editors can count members
return this.session.user.isAdmin && (this.totalMemberCount === 0 || this.countTotalMembersTask.isRunning);
}
get showEmailSection() {
🐛 Fixed inconsistent publish/send messaging in publish menu no issue - fixes the "publish/send" dropdown showing when mail is not allowed by tying it to the `canSendEmail` property rather than always showing when editing a draft post - was previously showing when mailgun is not configured and when default email recipients is set to "disabled", in both cases the recipient selection was never available so the dropdown was confusing and lead to invalid states - removed the unnecessary property`showEmailOnlyInput` - replaced with `@canSendEmail` - removed the unnecessary property `nextActionName` - it always resulted in "publish" because it was only being shown for pages and pages can't be sent by email - removed the logic for setting the initial selected option in `<GhDistributionActionSelect>` because it meant the select was not directly tied to the real distribution action value leading to mixed states across components. Switched to using the passed in `@distributionAction` value directly. - fixed the publish button incorrectly showing "Publish & send" when no send would be occurring for a post - if "Publish & Send" is selected, the button will now show "Publish" when no recipients are selected - fixed the publish button incorrectly showing "Publish & send" for pages - fixed the now/schedule radio texts not changing when publish type was set to "Send" - the conditionals were incorrectly looking at `this.args.post.emailOnly` which is incorrect syntax would only update _after_ saving, switched to the menu's internal `@emailOnly` argument instead
2021-10-21 15:07:56 +03:00
return this.args.canSendEmail && this.args.distributionAction !== 'publish';
}
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;
} 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);
}
}
this.args.post.validate();
}
}
@action
setDistributionAction(type) {
this.args.setDistributionAction(type);
}
@action
setDate(date) {
let post = this.args.post;
let dateString = moment(date).format('YYYY-MM-DD');
post.set('publishedAtBlogDate', dateString);
return post.validate();
}
@action
setTime(time) {
let post = this.args.post;
post.set('publishedAtBlogTime', time);
return post.validate();
}
@task
*countTotalMembersTask() {
const user = yield this.session.user;
if (user.isAdmin) {
const result = yield this.store.query('member', {limit: 1, filter: 'subscribed:true'});
this.totalMemberCount = result.meta.pagination.total;
}
}
// 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');
}
}