-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackernews.tsx
93 lines (77 loc) · 2.51 KB
/
hackernews.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
// ReactQL Hacker News GraphQL example
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
import * as React from "react";
// Use the `<Query>` component from the React Apollo lib to declaratively
// fetch the GraphQL data, to display as part of our component
import { Query } from "react-apollo";
/* Local */
// Styled component lib for generating CSS in lieu of using SASS
import styled from "@/lib/styledComponents";
// Query to get top stories from HackerNews
import hackerNewsQuery from "@/queries/getHackerNewsTopStories";
// ----------------------------------------------------------------------------
// Typescript types
// Represents a HackerNews story - id, title and url
interface IHackerNewsStory {
id: string;
title: string;
url: string;
}
// Represents the data returned by the Hacker News GraphQL query
interface IHackerNewsTopStories {
hn: {
topStories: IHackerNewsStory[];
};
}
// Style the list item so it overrides the default font
const Story = styled.li`
font-size: 16px;
a:hover {
/* shows an example of how we can use themes */
color: ${props => props.theme.colors.orange};
}
`;
// Execute the GraphQL query. With SSR, the server will await the returned
// result before rendering out the initial HTML. On the browser, it will re-use
// whatever the server has sent it - or, if it's a client-navigated route that
// doesn't already have data from the server -- it'll display a loading message
// while the data is being retrieved
export default () => (
<Query<IHackerNewsTopStories> query={hackerNewsQuery}>
{
result => {
// Any errors? Say so!
if (result.error) {
return (
<h1>Error retrieving news stories! — {result.error}</h1>
);
}
// If the data is still loading, return with a basic
// message to alert the user
if (result.loading) {
return (
<h1>Loading Hacker News stories...</h1>
);
}
// Otherwise, we have data to work with... map over it with a
// bullet-point list
return (
<>
<h3>Top stories from Hacker News</h3>
<ul>
{
result.data!.hn.topStories.map(story => (
<Story key={story.id}>
<a href={story.url} target="_blank">{story.title}</a>
</Story>
))
}
</ul>
</>
);
}
}
</Query>
);