2022-04-27 20:20:46 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2022-04-27 20:20:46 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-05-12 14:13:06 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-04-27 20:20:46 +03:00
|
|
|
|
|
|
|
export default class PublishAtOption extends Component {
|
2022-05-12 14:13:06 +03:00
|
|
|
@service settings;
|
|
|
|
|
2022-04-27 20:20:46 +03:00
|
|
|
@action
|
|
|
|
setDate(selectedDate) {
|
2022-10-07 13:20:06 +03:00
|
|
|
// selectedDate is a Date object that contains the correct date string in the blog timezone
|
2022-10-07 16:23:21 +03:00
|
|
|
const selectedMoment = moment.tz(selectedDate, this.settings.timezone);
|
2022-07-01 10:36:40 +03:00
|
|
|
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,
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.timezone
|
2022-07-01 10:36:40 +03:00
|
|
|
);
|
2022-05-24 12:41:38 +03:00
|
|
|
newDate.set({years, months, date});
|
2022-04-27 20:20:46 +03:00
|
|
|
|
2022-07-01 10:36:40 +03:00
|
|
|
// converts back to UTC
|
2022-04-27 20:20:46 +03:00
|
|
|
this.args.publishOptions.setScheduledAt(newDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-05-12 16:23:48 +03:00
|
|
|
setTime(time, event) {
|
2022-10-07 16:23:21 +03:00
|
|
|
const newDate = moment.tz(this.args.publishOptions.scheduledAtUTC, this.settings.timezone);
|
2022-05-12 16:23:48 +03:00
|
|
|
|
|
|
|
// used to reset the time value on blur if it's invalid
|
2022-05-24 00:12:37 +03:00
|
|
|
const oldTime = newDate.format('HH:mm');
|
2022-05-12 16:23:48 +03:00
|
|
|
|
2022-04-27 20:20:46 +03:00
|
|
|
if (!time) {
|
2022-05-12 16:23:48 +03:00
|
|
|
event.target.value = oldTime;
|
2022-04-27 20:20:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time.match(/^\d:\d\d$/)) {
|
|
|
|
time = `0${time}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!time.match(/^\d\d:\d\d$/)) {
|
2022-05-12 16:23:48 +03:00
|
|
|
event.target.value = oldTime;
|
2022-04-27 20:20:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [hour, minute] = time.split(':').map(n => parseInt(n, 10));
|
|
|
|
|
|
|
|
if (isNaN(hour) || hour < 0 || hour > 23 || isNaN(minute) || minute < 0 || minute > 59) {
|
2022-05-12 16:23:48 +03:00
|
|
|
event.target.value = oldTime;
|
2022-04-27 20:20:46 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-24 00:12:37 +03:00
|
|
|
newDate.set({hour, minute});
|
2022-04-27 20:20:46 +03:00
|
|
|
this.args.publishOptions.setScheduledAt(newDate);
|
|
|
|
}
|
|
|
|
}
|