Ghost/ghost/admin/app/components/gh-publishmenu-draft.js
Kevin Ansfield 89a6bc683b 🐛 Fixed incorrect publish type showing in publish menu after close/re-open
closes https://github.com/TryGhost/Team/issues/1151

The publish-type state was being set directly on the model in the publish menu rather than being internal to the menu until publishing. A previous attempt had been made to fix that but it wasn't complete and resulted in the menu showing that it would "Publish" after setting it and closing/re-opening when in fact it would "Publish and send".

- moved all state for the distribution type and email settings to be stored in the `<PublishMenu>` component rather than split across the the component and model
- when saving a post from the publish menu, we now pass `emailOnly` as an option to the editor's save task the same as `sendEmailWhenPublished` (corresponding to the `email_recipient_filter` query param) so that we're back to one place controlling email behaviour for a post
- when `emailOnly` is passed as an option to the editor save task it will set the property on the model before saving and reset it back if the save fails. That way the email-only flag behaves as close to the "send when published" flag as possible without it also being sent as a query param.
2021-10-15 15:42:32 +01:00

114 lines
3.5 KiB
JavaScript

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 nextActionName() {
return this.args.emailOnly ? 'send' : 'publish';
}
get showEmailSection() {
return this.args.canSendEmail && this.args.recipientsFilter !== 'none';
}
get showEmailOnlyInput() {
return this.args.post.isPost;
}
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');
}
}