From c2d4027e13f666a05f6068b1e96ce2c4a74b2b03 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 14 Feb 2019 21:20:20 +0000 Subject: [PATCH] Move event date formatting function to a shared place. --- src/components/Event.tsx | 35 +---------------------------------- src/helpers.tsx | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-) 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; +}