Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek committed Dec 5, 2024
1 parent 80bdb9c commit bd70d29
Showing 1 changed file with 21 additions and 54 deletions.
75 changes: 21 additions & 54 deletions client/components/fields/editor/EventSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,7 +24,6 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
super(props);

this.changeSchedule = this.changeSchedule.bind(this);
this.onAllDayChange = this.onAllDayChange.bind(this);
this.changeTimezone = this.changeTimezone.bind(this);
}

Expand Down Expand Up @@ -90,12 +87,8 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
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,
};
Expand All @@ -108,12 +101,7 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
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};

Expand Down Expand Up @@ -155,9 +143,9 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
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,
};

Expand All @@ -171,9 +159,7 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
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};
Expand All @@ -200,39 +186,6 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
}
}

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 ?? {};
Expand Down Expand Up @@ -331,3 +284,17 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
);
}
}

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')
);
}

0 comments on commit bd70d29

Please sign in to comment.