Skip to content

Commit 91d3eab

Browse files
committed
User: User page now works and fetches data from HN
1 parent 2a95239 commit 91d3eab

File tree

5 files changed

+278
-133
lines changed

5 files changed

+278
-133
lines changed

src/data/HNDataAPI.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ export function fetchComment(id) {
7373
.catch(reason => logger(`Fetching comment failed: ${reason}`));
7474
}
7575

76+
export function fetchUser(id) {
77+
logger(`Fetching user ${HN_API_URL}/user/${id}.json`);
78+
return api.child(`user/${id}`).once('value')
79+
.then((itemSnapshot) => {
80+
const item = itemSnapshot.val();
81+
if (item !== null && !item.deleted && !item.dead) {
82+
const user = {
83+
id: item.id,
84+
about: item.about,
85+
creationTime: item.created * 1000,
86+
dateOfBirth: null,
87+
email: null,
88+
firstName: null,
89+
hidden: [],
90+
karma: item.karma,
91+
lastName: null,
92+
likes: [],
93+
posts: item.submitted,
94+
};
95+
cache.setUser(user.id, user);
96+
logger(`Created User: ${item.id}`, item);
97+
return user;
98+
}
99+
throw item;
100+
})
101+
.catch(reason => logger(`Fetching user failed: ${reason}`));
102+
}
103+
76104
export function getFeed(feedType) {
77105
logger(`Fetching /${feedType}stories.json`);
78106
return api.child(`${feedType}stories`).once('value')

src/data/SampleData.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ export default {
631631
{
632632
// id: 1, // username is Primary Key
633633
id: 'clintonwoo', // Aka. username
634+
about: 'I am a software engineer who lives in New York.',
634635
creationTime: 1506024614000,
635636
dateOfBirth: 723618000000,
636637
@@ -642,6 +643,7 @@ export default {
642643
},
643644
{
644645
id: 'john', // Aka. username
646+
about: 'Just a bloke',
645647
creationTime: 1506024554000,
646648
dateOfBirth: 554875200000,
647649

src/data/Schema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const typeDefs = `
100100
# The user ID is a string of the username
101101
id: String!
102102
103+
about: String
104+
103105
creationTime: Date!
104106
105107
dateOfBirth: Date

src/data/models/User.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as DB from '../Database';
2+
import * as HNDB from '../HNDataAPI';
23
import cache from '../Cache';
34

45
export default class User {
56
static getUser(id) {
6-
return cache.getUser(id) || DB.getUser(id);
7+
return cache.getUser(id) || HNDB.fetchUser(id);
78
}
89
static getPostsForUser(id) {
910
return DB.getNewsItems()

0 commit comments

Comments
 (0)