Skip to content

Commit

Permalink
move out isSameDay function from utilis
Browse files Browse the repository at this point in the history
  • Loading branch information
dzonidoo committed Nov 7, 2024
1 parent 59b0cff commit 4f4c2a3
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
5 changes: 3 additions & 2 deletions client/components/Events/EventDateTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {DateTime} from '../UI';

import './style.scss';
import {Spacer} from 'superdesk-ui-framework/react';
import {isSameDay} from 'helpers';

interface IProps {
item: IEventItem;
Expand All @@ -24,8 +25,8 @@ export class EventDateTime extends React.PureComponent<IProps> {
const start = eventUtils.getStartDate(item);
const end = eventUtils.getEndDate(item);
const isAllDay = eventUtils.isEventAllDay(start, end);
const multiDay = !eventUtils.isSameDay(start, end);
const isEventAndPlanningSameDate = eventUtils.isSameDay(start, this.props.planningProps?.date);
const multiDay = !isSameDay(start, end);
const isEventAndPlanningSameDate = isSameDay(start, this.props.planningProps?.date);
const showEventStartDate = eventUtils.showEventStartDate(start, multiDay, this.props.planningProps?.date);
const isRemoteTimeZone = timeUtils.isEventInDifferentTimeZone(item);
const withYear = multiDay && start.year() !== end.year();
Expand Down
5 changes: 3 additions & 2 deletions client/components/Events/EventScheduleInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {TO_BE_CONFIRMED_FIELD} from '../../../constants';

import {Row, DateTimeInput, LineInput, ToggleInput, Field, TimeZoneInput} from '../../UI/Form';
import {RecurringRulesInput} from '../RecurringRulesInput';
import {isSameDay} from 'helpers';

interface IProps {
item: IEventItem;
Expand Down Expand Up @@ -42,7 +43,7 @@ export class EventScheduleInput extends React.Component<IProps, IState> {

this.state = {
isAllDay: eventUtils.isEventAllDay(dates.start, dates.end, true),
isMultiDay: !eventUtils.isSameDay(dates.start, dates.end),
isMultiDay: !isSameDay(dates.start, dates.end),
};

this.onChange = this.onChange.bind(this);
Expand Down Expand Up @@ -197,7 +198,7 @@ export class EventScheduleInput extends React.Component<IProps, IState> {
componentWillReceiveProps(nextProps: Readonly<IProps>) {
const nextDates = nextProps.diff?.dates ?? {};
const isAllDay = eventUtils.isEventAllDay(nextDates.start, nextDates.end, true);
const isMultiDay = !eventUtils.isSameDay(nextDates.start, nextDates.end);
const isMultiDay = !isSameDay(nextDates.start, nextDates.end);

const newState: Partial<IState> = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Row} from '../../UI/Preview';
import {TextAreaInput, Field} from '../../UI/Form';

import '../style.scss';
import {isSameDay} from 'helpers';

export class RescheduleEventComponent extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -134,8 +135,8 @@ export class RescheduleEventComponent extends React.Component {
fieldsToValidate // Validate only those fields which can change while rescheduling.
);

const multiDayChanged = eventUtils.isSameDay(original.dates.start, original.dates.end) &&
!eventUtils.isSameDay(diff.dates.start, diff.dates.end);
const multiDayChanged = isSameDay(original.dates.start, original.dates.end) &&
!isSameDay(diff.dates.start, diff.dates.end);

if ((!diff[TO_BE_CONFIRMED_FIELD] &&
eventUtils.eventsDatesSame(diff, original, TIME_COMPARISON_GRANULARITY.MINUTE)) ||
Expand Down
5 changes: 3 additions & 2 deletions client/components/fields/editor/EventSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {EditorFieldStartDateTime} from './StartDateTime';
import {Row, TimeZoneInput} from '../../UI/Form';
import {eventUtils, timeUtils} from '../../../utils';
import {TO_BE_CONFIRMED_FIELD} from '../../../constants';
import {isSameDay} from 'helpers';

interface IProps extends IEditorFieldProps {
item: IEventItem;
Expand Down Expand Up @@ -80,7 +81,7 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
const _startTime = this.props.item?._startTime;
const defaultDurationOnChange = this.props.profile?.editor?.dates?.default_duration_on_change ?? 1;
const isAllDay = eventUtils.isEventAllDay(startDate, endDate, true);
const isMultiDay = !eventUtils.isSameDay(startDate, endDate);
const isMultiDay = !isSameDay(startDate, endDate);
const newStartDate = !startDate ?
value :
moment(startDate)
Expand Down Expand Up @@ -130,7 +131,7 @@ export class EditorFieldEventSchedule extends React.PureComponent<IProps> {
endDate.hour(value.hour()).minute(value.minute()) :
value;
const isAllDay = eventUtils.isEventAllDay(startDate, endDate, true);
const isMultiDay = !eventUtils.isSameDay(startDate, endDate);
const isMultiDay = !isSameDay(startDate, endDate);
const changes = {'dates.end': newEndDate};

if (((_endTime && isAllDay) || !_endTime) &&
Expand Down
9 changes: 7 additions & 2 deletions client/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from 'moment';
import {GENERIC_ITEM_ACTIONS} from './constants';
import {IItemAction} from './interfaces';
import {IDateTime, IItemAction} from './interfaces';

export function isItemAction(
x: IItemAction | typeof GENERIC_ITEM_ACTIONS.DIVIDER | typeof GENERIC_ITEM_ACTIONS.LABEL,
Expand All @@ -11,4 +12,8 @@ export function isMenuDivider(
x: IItemAction | typeof GENERIC_ITEM_ACTIONS.DIVIDER | typeof GENERIC_ITEM_ACTIONS.LABEL,
): x is typeof GENERIC_ITEM_ACTIONS.DIVIDER {
return x['label'] != null && x['label'] === GENERIC_ITEM_ACTIONS.DIVIDER.label;
}
}

export function isSameDay(startingDate: IDateTime, endingDate: IDateTime): boolean {
return moment(startingDate).format('DD/MM/YYYY') === moment(endingDate).format('DD/MM/YYYY');
}
8 changes: 2 additions & 6 deletions client/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
sanitizeItemFields,
} from './index';
import {toUIFrameworkInterface, getRelatedEventIdsForPlanning} from './planning';
import {isSameDay} from 'helpers';


/**
Expand All @@ -76,10 +77,6 @@ function isEventAllDay(startingDate: IDateTime, endingDate: IDateTime, checkMult
end.isSame(end.clone().endOf('day'), 'minute');
}

function isSameDay(startingDate: IDateTime, endingDate: IDateTime): boolean {
return moment(startingDate).format('DD/MM/YYYY') === moment(endingDate).format('DD/MM/YYYY');
}

function showEventStartDate(eventDate: IDateTime, multiDay: boolean, planningDate?: IDateTime): boolean {
if (planningDate == null) {
return true;
Expand Down Expand Up @@ -843,7 +840,7 @@ function getEventActions(
const CREATE_PLANNING = callBacks[EVENTS.ITEM_ACTIONS.CREATE_PLANNING.actionName];
const CREATE_AND_OPEN_PLANNING = callBacks[EVENTS.ITEM_ACTIONS.CREATE_AND_OPEN_PLANNING.actionName];

(!withMultiPlanningDate || self.isSameDay(item)) ?
(!withMultiPlanningDate || isSameDay(item)) ?
self.getSingleDayPlanningActions(item, actions, CREATE_PLANNING, CREATE_AND_OPEN_PLANNING) :
self.getMultiDayPlanningActions(item, actions, CREATE_PLANNING, CREATE_AND_OPEN_PLANNING);
}
Expand Down Expand Up @@ -1361,7 +1358,6 @@ const self = {
canUpdateEventTime,
canConvertToRecurringEvent,
canUpdateEventRepetitions,
isSameDay,
showEventStartDate,
isEventRecurring,
getDateStringForEvent,
Expand Down
3 changes: 2 additions & 1 deletion client/validators/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {gettext, eventUtils} from '../utils';
import * as selectors from '../selectors';
import {formProfile} from './profile';
import {PRIVILEGES, EVENTS, TO_BE_CONFIRMED_FIELD} from '../constants';
import {isSameDay} from 'helpers';

const validateRequiredDates = ({value, errors, messages, diff}) => {
if (!get(value, 'start')) {
Expand Down Expand Up @@ -46,7 +47,7 @@ const validateDateRange = ({value, errors, messages}) => {
}

if (endDate.isSameOrBefore(startDate, 'minutes')) {
if (eventUtils.isSameDay(value.start, value.end)) {
if (isSameDay(value.start, value.end)) {
set(errors, '_endTime', gettext('End time should be after start time'));
messages.push(gettext('END TIME should be after START TIME'));
} else {
Expand Down

0 comments on commit 4f4c2a3

Please sign in to comment.