From bb61ef3935eeb5d0070a031fa1003e36a60f122a Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 8 Dec 2017 00:23:46 +0000 Subject: [PATCH] Improve presentation of events. --- src/Event.tsx | 37 ++++++++++++++++++++++++++++++++++++- src/ical.js.d.ts | 14 ++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Event.tsx b/src/Event.tsx index 6ce88df..6fc6c03 100644 --- a/src/Event.tsx +++ b/src/Event.tsx @@ -1,7 +1,36 @@ import * as React from 'react'; +import * as moment from 'moment'; import * as ICAL from 'ical.js'; +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.day === mEnd.day) && (mEnd.diff(mStart, 'days', true) < 1)) { + strStart = mStart.format(fullFormat); + strEnd = mEnd.format('LT'); + } else { + strStart = mStart.format(fullFormat); + strEnd = mEnd.format(fullFormat); + } + + return strStart + ' - ' + strEnd; +} + class Event extends React.Component { props: { event?: ICAL.Event, @@ -12,9 +41,15 @@ class Event extends React.Component { throw Error('Event should be defined!'); } + (window as any).me = this.props.event; + return (
-

{this.props.event.summary}

+

{this.props.event.summary}

+
{formatDateRange(this.props.event.startDate, this.props.event.endDate)}
+
{this.props.event.location}
+
{this.props.event.description}
+
Attendees: {this.props.event.attendees.map((x) => (x.getFirstValue())).join(', ')}
); } diff --git a/src/ical.js.d.ts b/src/ical.js.d.ts index 7c004ca..246de17 100644 --- a/src/ical.js.d.ts +++ b/src/ical.js.d.ts @@ -15,8 +15,11 @@ declare module 'ical.js' { class Event { uid: string; summary: string; - startDate: any; - endDate: any; + startDate: Time; + endDate: Time; + description: string; + location: string; + attendees: Array; constructor(component?: Component | null, options?: {strictExceptions: boolean, exepctions: Array}); @@ -26,7 +29,14 @@ declare module 'ical.js' { name: string; type: string; + getFirstValue(): string; getValues(): Array; toJSON(): any; } + + class Time { + isDate: boolean; + + toJSDate(): Date; + } }