Skip to content

Commit 00104e9

Browse files
committed
deduplicate the time gathering code in Calendar
1 parent cbe9893 commit 00104e9

File tree

3 files changed

+46
-57
lines changed

3 files changed

+46
-57
lines changed

source/views/calendar/event-detail.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {fastGetTrimmedText} from '../../lib/html'
66
import {Cell, Section, TableView} from 'react-native-tableview-simple'
77
import type {EventType} from './types'
88
import {ShareButton} from '../components/nav-buttons'
9+
import {times} from './times'
910

1011
const styles = StyleSheet.create({
1112
chunk: {
@@ -36,33 +37,12 @@ function display(title: string, data: string) {
3637
}
3738

3839
function displayTimes(title: string, event: EventType) {
39-
const eventLength = moment
40-
.duration(event.endTime.diff(event.startTime))
41-
.asHours()
42-
43-
const allDay = eventLength === 24
44-
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
45-
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
40+
const {allDay, start, end} = times(event)
4641

4742
if (allDay) {
4843
return display(title, 'All-Day')
4944
}
5045

51-
let start, end
52-
if (event.isOngoing) {
53-
start = event.startTime.format('MMM. D')
54-
end = event.endTime.format('MMM. D')
55-
} else if (multiDay) {
56-
start = event.startTime.format('h:mm A')
57-
end = `${event.endTime.format('MMM. D h:mm A')}`
58-
} else if (sillyZeroLength) {
59-
start = event.startTime.format('h:mm A')
60-
end = '???'
61-
} else {
62-
start = event.startTime.format('h:mm A')
63-
end = event.endTime.format('h:mm A')
64-
}
65-
6646
return display(title, start + ' — ' + end)
6747
}
6848

source/views/calendar/event-row.js

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import React from 'react'
33
import {StyleSheet, Text} from 'react-native'
44
import type {EventType} from './types'
5-
import moment from 'moment-timezone'
65
import * as c from '../components/colors'
76
import {Row, Column} from '../components/layout'
87
import {ListRow, Detail, Title} from '../components/list'
98
import {fastGetTrimmedText} from '../../lib/html'
109
import {Bar} from './vertical-bar'
10+
import {times} from './times'
1111

1212
const styles = StyleSheet.create({
1313
row: {
@@ -72,13 +72,7 @@ export default function EventRow({
7272
}
7373

7474
function CalendarTimes({event, style}: {event: EventType, style: any}) {
75-
const eventLength = moment
76-
.duration(event.endTime.diff(event.startTime))
77-
.asHours()
78-
79-
const allDay = eventLength === 24
80-
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
81-
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
75+
const {allDay, start, end} = times(event)
8276

8377
if (allDay) {
8478
return (
@@ -88,33 +82,6 @@ function CalendarTimes({event, style}: {event: EventType, style: any}) {
8882
)
8983
}
9084

91-
let startTimeFormatted = event.startTime.format('h:mm A')
92-
let endTimeFormatted = event.endTime.format('h:mm A')
93-
let midnightTime = '12:00 AM'
94-
95-
let start, end
96-
if (event.isOngoing) {
97-
start = event.startTime.format('MMM. D')
98-
end = event.endTime.format('MMM. D')
99-
} else if (multiDay) {
100-
// 12:00 PM to Jun. 25 3:00pm
101-
// Midnight to Jun. 25 <-- assuming the end time is also midnight
102-
start = startTimeFormatted
103-
const endFormat = endTimeFormatted === midnightTime
104-
? 'MMM. D'
105-
: 'MMM. D h:mm A'
106-
end = `to ${event.endTime.format(endFormat)}`
107-
} else if (sillyZeroLength) {
108-
start = startTimeFormatted
109-
end = 'until ???'
110-
} else {
111-
start = startTimeFormatted
112-
end = endTimeFormatted
113-
}
114-
115-
start = start === midnightTime ? 'Midnight' : start
116-
end = end === midnightTime ? 'Midnight' : end
117-
11885
return (
11986
<Column style={style}>
12087
<Text style={[styles.time, styles.start]}>{start}</Text>

source/views/calendar/times.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @flow
2+
3+
import moment from 'moment-timezone'
4+
5+
export function times(event: EventType) {
6+
const eventLength = moment
7+
.duration(event.endTime.diff(event.startTime))
8+
.asHours()
9+
10+
const allDay = eventLength === 24
11+
const multiDay = event.startTime.dayOfYear() !== event.endTime.dayOfYear()
12+
const sillyZeroLength = event.startTime.isSame(event.endTime, 'minute')
13+
14+
let startTimeFormatted = event.startTime.format('h:mm A')
15+
let endTimeFormatted = event.endTime.format('h:mm A')
16+
let midnightTime = '12:00 AM'
17+
18+
let start, end
19+
if (event.isOngoing) {
20+
start = event.startTime.format('MMM. D')
21+
end = event.endTime.format('MMM. D')
22+
} else if (multiDay) {
23+
// 12:00 PM to Jun. 25 3:00pm
24+
// Midnight to Jun. 25 <-- assuming the end time is also midnight
25+
start = startTimeFormatted
26+
const endFormat = endTimeFormatted === midnightTime
27+
? 'MMM. D'
28+
: 'MMM. D h:mm A'
29+
end = `to ${event.endTime.format(endFormat)}`
30+
} else if (sillyZeroLength) {
31+
start = startTimeFormatted
32+
end = 'until ???'
33+
} else {
34+
start = startTimeFormatted
35+
end = endTimeFormatted
36+
}
37+
38+
start = start === midnightTime ? 'Midnight' : start
39+
end = end === midnightTime ? 'Midnight' : end
40+
41+
return {start, end, allDay}
42+
}

0 commit comments

Comments
 (0)