diff --git a/src/components/Event.tsx b/src/components/Event.tsx index 5ea9296..865fda8 100644 --- a/src/components/Event.tsx +++ b/src/components/Event.tsx @@ -1,44 +1,11 @@ import * as React from 'react'; -import moment from 'moment'; import PimItemHeader from './PimItemHeader'; -import * as ICAL from 'ical.js'; +import { formatDateRange } from '../helpers'; import { EventType } from '../pim-types'; -function formatDateRange(start: ICAL.Time, end: ICAL.Time) { - const mStart = moment(start.toJSDate()); - const mEnd = moment(end.toJSDate()); - let strStart; - let strEnd; - - const allDayFormat = 'dddd, LL'; - const fullFormat = 'LLLL'; - - // All day - if (start.isDate) { - if (mEnd.diff(mStart, 'days', true) === 1) { - return mStart.format(allDayFormat); - } else { - strStart = mStart.format(allDayFormat); - strEnd = mEnd.clone().subtract(1, 'day').format(allDayFormat); - } - } else if (mStart.isSame(mEnd, 'day')) { - strStart = mStart.format(fullFormat); - strEnd = mEnd.format('LT'); - - if (mStart.isSame(mEnd)) { - return strStart; - } - } else { - strStart = mStart.format(fullFormat); - strEnd = mEnd.format(fullFormat); - } - - return strStart + ' - ' + strEnd; -} - class Event extends React.PureComponent { props: { item?: EventType, diff --git a/src/helpers.tsx b/src/helpers.tsx index de456ee..23b364b 100644 --- a/src/helpers.tsx +++ b/src/helpers.tsx @@ -1,4 +1,6 @@ import * as React from 'react'; +import * as ICAL from 'ical.js'; +import moment from 'moment'; // Generic handling of input changes export function handleInputChange(self: React.Component, part?: string) { @@ -47,3 +49,35 @@ export function insertSorted(array: T[] = [], newItem: T, key: string) { return array; } + +export function formatDateRange(start: ICAL.Time, end: ICAL.Time) { + const mStart = moment(start.toJSDate()); + const mEnd = moment(end.toJSDate()); + let strStart; + let strEnd; + + const allDayFormat = 'dddd, LL'; + const fullFormat = 'LLLL'; + + // All day + if (start.isDate) { + if (mEnd.diff(mStart, 'days', true) === 1) { + return mStart.format(allDayFormat); + } else { + strStart = mStart.format(allDayFormat); + strEnd = mEnd.clone().subtract(1, 'day').format(allDayFormat); + } + } else if (mStart.isSame(mEnd, 'day')) { + strStart = mStart.format(fullFormat); + strEnd = mEnd.format('LT'); + + if (mStart.isSame(mEnd)) { + return strStart; + } + } else { + strStart = mStart.format(fullFormat); + strEnd = mEnd.format(fullFormat); + } + + return strStart + ' - ' + strEnd; +}