Skip to content

Commit 3ec6b05

Browse files
committed
Add timestamp() function to dto.Event.
timestamp() function is used to retrieve the UNIX timestamp of a given event.
1 parent 59740d8 commit 3ec6b05

File tree

10 files changed

+33
-42
lines changed

10 files changed

+33
-42
lines changed

ts/CancelledEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import UiComponent from './UiComponent';
77
import Timestamp from './Timestamp';
88

99
export default class CancelledEvent implements UiComponent {
10-
constructor(private _event: dto.Event,
11-
private _timestamp: string) {
10+
constructor(private _event: dto.Event) {
1211
}
1312

1413
appendTo(entry: HTMLElement | null): void {
1514
new html.Div(
1615
new ComponentsArray([
17-
new Timestamp(this._timestamp),
16+
new Timestamp(this._event.timestamp()),
1817
new html.H1(
1918
new html.Href(
2019
`${this._event.url}`,

ts/CurrentEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import TwitchPlayer from './TwitchPlayer';
88
import Timestamp from './Timestamp'
99

1010
export default class CurrentEvent implements UiComponent {
11-
constructor(private _event: dto.Event,
12-
private _timestamp: string) {
11+
constructor(private _event: dto.Event) {
1312
}
1413

1514
appendTo(entry: HTMLElement | null): void {
1615
new html.Div(
1716
new ComponentsArray([
18-
new Timestamp(this._timestamp),
17+
new Timestamp(this._event.timestamp()),
1918
new html.Div(
2019
new html.Href(
2120
this._event.channel ? this._event.channel : "https://twitch.tv/tsoding",

ts/Event.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ export default class Event implements UiComponent {
1414

1515
appendTo(entry: HTMLElement | null): void {
1616
const secondsDiff = moment().diff(this._event.datetime, 'seconds');
17-
const timestamp = this._event.datetime.utc().unix().toString();
1817

1918
if (this.isCancelled()) {
20-
new CancelledEvent(this._event, timestamp).appendTo(entry);
19+
new CancelledEvent(this._event).appendTo(entry);
2120
} else if (0 <= secondsDiff && secondsDiff < 4 * 60 * 60) {
22-
new CurrentEvent(this._event, timestamp).appendTo(entry);
21+
new CurrentEvent(this._event).appendTo(entry);
2322
} else if (secondsDiff >= 4 * 60 * 60) {
24-
new PastEvent(this._event, timestamp).appendTo(entry);
23+
new PastEvent(this._event).appendTo(entry);
2524
} else {
26-
new FutureEvent(this._event, timestamp).appendTo(entry);
25+
new FutureEvent(this._event).appendTo(entry);
2726
}
2827

29-
const hashId = "#_" + timestamp;
28+
const hashId = "#_" + this._event.timestamp();
3029

3130
if (window.location.hash == hashId) {
3231
window.location.hash = "";

ts/EventsForDay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ export default class EventsForDay implements UiComponent {
6767
.map(
6868
(e) => new Event(
6969
this._state.eventPatches
70-
? new dto.PatchedEvent(e, this._state.eventPatches[e.datetime.utc().unix()])
71-
: e,
70+
? new dto.PatchedEvent(e as dto.Event, this._state.eventPatches[e.datetime.utc().unix()])
71+
: e as dto.Event,
7272
this._state.cancelledEvents
7373
)
7474
)

ts/FutureEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import UiComponent from './UiComponent';
77
import Timestamp from './Timestamp';
88

99
export default class FutureEvent implements UiComponent {
10-
constructor(private _event: dto.Event,
11-
private _timestamp: string) {
10+
constructor(private _event: dto.Event) {
1211
}
1312

1413
appendTo(entry: HTMLElement | null): void {
1514
new html.Div(
1615
new ComponentsArray([
17-
new Timestamp(this._timestamp),
16+
new Timestamp(this._event.timestamp()),
1817
new html.H1(
1918
new html.Href(
2019
`${this._event.url}`,

ts/PastEvent.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import UiComponent from './UiComponent';
77
import Timestamp from './Timestamp';
88

99
export default class PastEvent implements UiComponent {
10-
constructor(private _event: dto.Event,
11-
private _timestamp: string) {
10+
constructor(private _event: dto.Event) {
1211
}
1312

1413
appendTo(entry: HTMLElement | null): void {
1514
new html.Div(
1615
new ComponentsArray([
17-
new Timestamp(this._timestamp),
16+
new Timestamp(this._event.timestamp()),
1817
new html.H1(
1918
new html.Href(
2019
`${this._event.url}`,

ts/Timestamp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import UiComponent from './UiComponent';
77
import TwitchPlayer from './TwitchPlayer';
88
import copyToClipboard from './util/copyToClipboard';
99

10-
export default class CurrentEvent implements UiComponent {
10+
export default class Timestamp implements UiComponent {
1111
constructor(private _timestamp: string) {
1212
}
1313

ts/dto/Event.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as moment from 'moment';
22

3-
export default interface Event {
4-
datetime: moment.Moment,
5-
title: string,
6-
description: string,
7-
url: string,
8-
channel: string,
3+
export default class Event {
4+
constructor(public datetime: moment.Moment,
5+
public title: string,
6+
public description: string,
7+
public url: string,
8+
public channel: string) {
9+
}
10+
11+
timestamp() {
12+
return this.datetime.utc().unix().toString();
13+
}
914
}

ts/dto/PatchedEvent.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@ import * as moment from 'moment';
22
import Event from './Event';
33
import EventPatch from './EventPatch';
44

5-
export default class PatchedEvent implements Event {
6-
datetime: moment.Moment;
7-
title: string;
8-
description: string;
9-
url: string;
10-
channel: string;
11-
5+
export default class PatchedEvent extends Event {
126
constructor(event: Event, eventPatch: EventPatch | undefined) {
13-
this.datetime = event.datetime;
14-
this.title = event.title;
15-
this.description = event.description;
16-
this.url = event.url;
17-
this.channel = event.channel;
7+
super(event.datetime, event.title, event.description, event.url, event.channel);
188

199
if (eventPatch) {
2010
if (eventPatch.title) {

ts/util/copyToClipboard.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Adapted from https://github.com/feross/clipboard-copy
2+
13
export default function copyToClipboard(text: string) {
24
// Use the Async Clipboard API when available. Requires a secure browing context (i.e. HTTPS)
35
if ((navigator as any).clipboard) {
@@ -19,7 +21,7 @@ export default function copyToClipboard(text: string) {
1921
// Make a selection object representing the range of text selected by the user
2022
const selection = window.getSelection()
2123
const range = window.document.createRange()
22-
24+
2325
if (!selection) {
2426
return Promise.reject()
2527
}
@@ -40,8 +42,7 @@ export default function copyToClipboard(text: string) {
4042
selection.removeAllRanges()
4143
window.document.body.removeChild(span)
4244

43-
// The Async Clipboard API returns a promise that may reject with `undefined`
44-
// so we match that here for consistency.
45+
// The Async Clipboard API returns a promise that may reject with `undefined` so we match that here for consistency.
4546
return success
4647
? Promise.resolve()
4748
: Promise.reject()

0 commit comments

Comments
 (0)