Ghost/ghost/admin/app/components/editor/publish-options/publish-at.js
Kevin Ansfield 100dea6d98 🐛 Fixed scheduled post datepicker sometimes picking day before selected date
no issue

- in sites with a timezone that is negatively offset from UTC at certain times of day when the equivalent UTC date would be the next day, selecting a date when scheduling a post would select a day before the selected date
- fixed the date adjustment when applying the selected date to properly take timezones into account
2022-07-01 11:20:26 +01:00

59 lines
1.8 KiB
JavaScript

import Component from '@glimmer/component';
import moment from 'moment';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class PublishAtOption extends Component {
@service settings;
@action
setDate(selectedDate) {
const selectedMoment = moment(selectedDate);
const {years, months, date} = selectedMoment.toObject();
// Create a new moment from existing scheduledAtUTC _in site timezone_.
// This ensures we're setting the date correctly because we don't need
// to account for the converted UTC date being yesterday/tomorrow.
const newDate = moment.tz(
this.args.publishOptions.scheduledAtUTC,
this.settings.get('timezone')
);
newDate.set({years, months, date});
// converts back to UTC
this.args.publishOptions.setScheduledAt(newDate);
}
@action
setTime(time, event) {
const newDate = moment.tz(this.args.publishOptions.scheduledAtUTC, this.settings.get('timezone'));
// used to reset the time value on blur if it's invalid
const oldTime = newDate.format('HH:mm');
if (!time) {
event.target.value = oldTime;
return;
}
if (time.match(/^\d:\d\d$/)) {
time = `0${time}`;
}
if (!time.match(/^\d\d:\d\d$/)) {
event.target.value = oldTime;
return;
}
const [hour, minute] = time.split(':').map(n => parseInt(n, 10));
if (isNaN(hour) || hour < 0 || hour > 23 || isNaN(minute) || minute < 0 || minute > 59) {
event.target.value = oldTime;
return;
}
newDate.set({hour, minute});
this.args.publishOptions.setScheduledAt(newDate);
}
}