-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
35 lines (30 loc) · 1.09 KB
/
main.js
File metadata and controls
35 lines (30 loc) · 1.09 KB
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
require("babel-polyfill");
var USER_URL = 'https://s3.amazonaws.com/constellational-users';
var POST_URL = 'https://d2gs3048w5buml.cloudfront.net';
var views = require('./views');
var React = require('react');
var splitPathname = window.location.pathname.split('/');
splitPathname.shift();
let username = splitPathname.shift().toLowerCase();
let id = splitPathname.shift();
function fetchUser(username) {
return fetch(USER_URL + '/' + username).then(res => res.json());
}
function fetchPost(username, url) {
return fetch(POST_URL + '/' + username + '/' + url).then(res => res.json());
}
function load(username, id) {
return fetchUser(username).then((user) => {
if ((id) && (user.posts.indexOf(id) > 0)) {
user.posts.splice(user.posts.indexOf(id), 1);
user.posts.unshift(id);
}
var promiseArr = user.posts.map(id => fetchPost(username, id));
Promise.all(promiseArr).then((data) => {
user.posts = data;
let mountNode = document.getElementById("react-mount");
React.render(React.createElement(views.User, user), mountNode);
});
});
}
load(username, id);