diff --git a/client/components/fields/editor/EventSchedule.tsx b/client/components/fields/editor/EventSchedule.tsx index d60c92dd0..676a9ed52 100644 --- a/client/components/fields/editor/EventSchedule.tsx +++ b/client/components/fields/editor/EventSchedule.tsx @@ -4,8 +4,6 @@ import moment from 'moment-timezone'; import {IEditorFieldProps, IEventFormProfile, IEventItem} from '../../../interfaces'; import {superdeskApi} from '../../../superdeskApi'; -import {Switch} from 'superdesk-ui-framework/react'; - import {EditorFieldEndDateTime} from './EndDateTime'; import {EditorFieldStartDateTime} from './StartDateTime'; import {Row, TimeZoneInput} from '../../UI/Form'; @@ -26,7 +24,6 @@ export class EditorFieldEventSchedule extends React.PureComponent { super(props); this.changeSchedule = this.changeSchedule.bind(this); - this.onAllDayChange = this.onAllDayChange.bind(this); this.changeTimezone = this.changeTimezone.bind(this); } @@ -90,12 +87,8 @@ export class EditorFieldEventSchedule extends React.PureComponent { const changes = { _startTime: null, _endTime: null, - 'dates.start': startDate ? moment.utc(startDate).hour(0) - .minute(0) - .second(0) : null, - 'dates.end': endDate ? moment.utc(endDate).hour(0) - .minute(0) - .second(0) : null, + 'dates.start': startDate ? localDateToUtc(startDate, this.props.item?.dates?.tz) : null, + 'dates.end': endDate ? localDateToUtc(endDate, this.props.item?.dates?.tz) : null, 'dates.all_day': true, 'dates.no_end_time': false, }; @@ -108,12 +101,7 @@ export class EditorFieldEventSchedule extends React.PureComponent { const defaultDurationOnChange = this.props.profile?.editor?.dates?.default_duration_on_change ?? 1; const isAllDay = eventUtils.isEventAllDay(startDate, endDate, true); const isMultiDay = !isSameDay(startDate, endDate); - const newStartDate = !startDate ? - value : - moment(startDate) - .hour(value.hour()) - .minute(value.minute()) - .second(value.second()); + const newStartDate = !startDate ? value : combineDateTime(startDate, value, this.props.item.dates?.tz); const changes = {'dates.start': newStartDate, 'dates.all_day': false}; @@ -155,9 +143,9 @@ export class EditorFieldEventSchedule extends React.PureComponent { if (!value) { const changes = { _endTime: null, - 'dates.end': this.props.item.dates?.end ? moment.utc(this.props.item.dates.end).hour(0) - .minute(0) - .second(0) : null, + 'dates.end': this.props.item.dates?.end ? + localDateToUtc(this.props.item.dates.end, this.props.item.dates.tz) + : null, 'dates.no_end_time': true, }; @@ -171,9 +159,7 @@ export class EditorFieldEventSchedule extends React.PureComponent { null; const _endTime = this.props.item?._endTime; const defaultDurationOnChange = this.props.profile?.editor?.dates?.default_duration_on_change ?? 1; - const newEndDate = endDate ? - endDate.hour(value.hour()).minute(value.minute()) : - value; + const newEndDate = endDate ? combineDateTime(endDate, value, this.props.item.dates?.tz) : value; const isAllDay = eventUtils.isEventAllDay(startDate, endDate, true); const isMultiDay = !isSameDay(startDate, endDate); const changes = {'dates.end': newEndDate}; @@ -200,39 +186,6 @@ export class EditorFieldEventSchedule extends React.PureComponent { } } - onAllDayChange(isAllDay: boolean) { - const dates = this.props.item?.dates ?? this.props.defaultValue ?? {}; - const nowTz = dates.tz ? - moment.tz(dates.tz) : - moment(); - const newStart = moment(dates.start || nowTz) - .startOf('day'); - let newEnd: moment.Moment; - let startTime: moment.Moment; - let endTime: moment.Moment; - - if (isAllDay) { - // If allDay is enabled, then set the event to all day - newEnd = moment(dates.end || nowTz).endOf('day'); - startTime = newStart.clone(); - endTime = newEnd.clone(); - } else { - // If allDay is disabled, then set the new dates to the initial values - // since last save and time to empty - newEnd = moment(dates.end || newStart) - .hour(0) - .minute(1); - } - - this.props.onChange({ - 'dates.start': newStart, - 'dates.end': newEnd, - _startTime: startTime, - _endTime: endTime, - _time_to_be_confirmed: false, - }, null); - } - changeTimezone(_: string, timezone?: string) { const dtFormat = 'DD/MM/YYYY HH:mm'; const dates = this.props.item?.dates ?? {}; @@ -331,3 +284,17 @@ export class EditorFieldEventSchedule extends React.PureComponent { ); } } + +function combineDateTime(date: moment.MomentInput, time: moment.Moment, tz?: string): moment.Moment { + return moment.tz(moment(date).format('YYYY-MM-DD'), tz) // we only want the date part + .hour(time.hour()) + .minute(time.minute()) + .second(time.second()); +} + +function localDateToUtc(date: moment.MomentInput, tz?: string): moment.Moment { + return moment.utc(tz ? + moment.tz(date, tz).format('YYYY-MM-DD') : + moment(date).format('YYYY-MM-DD') + ); +} \ No newline at end of file