-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
news-list.tsx
99 lines (83 loc) · 2.76 KB
/
news-list.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Component, h, Prop, State, Ionic } from '@stencil/core';
@Component({
tag: 'news-list'
})
export class NewsList {
apiRootUrl: string = 'https://node-hnapi.herokuapp.com';
@Prop() type: any[];
@State() fakeData: any[] = [];
fakeFetch(url: string): Promise<any[]> {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.addEventListener('load', function () {
resolve(JSON.parse(this.responseText));
});
request.addEventListener('error', function () {
reject(`error: ${this.statusText} / ${this.status}`);
});
request.open('GET', url, true);
request.send();
});
}
comments(story: any) {
if (Ionic.isServer) return;
Ionic.controller('loading', { content: 'fetching comments...' }).then(loading => {
loading.present();
this.fakeFetch(`${this.apiRootUrl}/item/${story.id}`).then((data: { [key: string]: any }) => {
setTimeout(() => {
loading.dismiss().then(() => {
Ionic.controller('modal', { component: 'comments-page', componentProps: { comments: data.comments, storyId: story.id } }).then(modal => {
console.log('modal created');
modal.present().then(() => {
console.log('modal finished transitioning in, commments: ', modal.componentProps.comments);
});
});
});
}, 300);
});
});
}
render() {
if (this.type.length === 0) {
return (
<ion-list>
{Array.from(Array(10)).map(() =>
<ion-item>
<div class='points' slot='start'>
<ion-skeleton-text width='20px'></ion-skeleton-text>
</div>
<ion-label>
<h2 class='list-header'>
<ion-skeleton-text width='90%'></ion-skeleton-text>
<ion-skeleton-text width='85%'></ion-skeleton-text>
</h2>
<h3 class='comments-text'>
<ion-skeleton-text width='60%'></ion-skeleton-text>
</h3>
</ion-label>
</ion-item>
)}
</ion-list>
);
}
return (
<ion-list>
{this.type.map((story: any) => (
<ion-item>
<div class='points' slot='start'>
{story.points || 0}
</div>
<ion-label>
<h2 class='list-header'>
<a href={story.url}>{story.title}</a>
</h2>
<h3 class='comments-text' onClick={() => this.comments(story)}>
Posted by {story.user} {story.time_ago} | {story.comments_count} comments
</h3>
</ion-label>
</ion-item>
))}
</ion-list>
);
}
}