Skip to content

Commit

Permalink
an attempt at refactoring blog feed loader code into a separate modul…
Browse files Browse the repository at this point in the history
…e...
  • Loading branch information
mmmavis committed May 27, 2015
1 parent c55112e commit 809171e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 43 deletions.
65 changes: 65 additions & 0 deletions lib/blog-feed-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var sanitizeHtml = require('sanitize-html');

function loadGoogleAPI(window, callback) {
if (window.google) {
return process.nextTick(callback);
}
var head = document.getElementsByTagName('head')[0];
var gScript = document.createElement('script');
gScript.setAttribute('src', 'https://www.google.com/jsapi');
gScript.onload = callback;
head.appendChild(gScript);
};

function getBlogFeeds(feedUrl, callback) {
if (window.google) {
var google = window.google;
google.load('feeds', '1', {
callback: function() {
var feed = new google.feeds.Feed(feedUrl);
feed.load(function(result) {
callback(formatBlogFeeds(result.feed));
});
}
});
}
}

function formatBlogFeeds(feeds) {
var featured = feeds.entries[0];
var latestPosts = [];
var post;
for (var i = 1; i < 4; i++) {
post = feeds.entries[i];
latestPosts.push({
title: post.title,
publishedDate: post.publishedDate,
link: post.link
});
}
return {
featuredPostData: {
title: featured.title,
author: featured.author,
publishedDate: featured.publishedDate,
contentSnippet: sanitizeHtml(featured.content, {
allowedTags: []
}).split(' ').slice(0, 70).join(' ') + '...',
link: post.link
},
latestPostsData: latestPosts
};
}

var loadBlogFeed = function(requestedComp, window, feedUrl, callback) {
loadGoogleAPI(window, function() {
if (requestedComp.isMounted()) {
getBlogFeeds(feedUrl, function(result) {
callback(result);
});
}
});
};

module.exports = loadBlogFeed;

50 changes: 7 additions & 43 deletions pages/home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var IconButton = require('../components/icon-button.jsx');

var config = require('../lib/config');

var blogFeedLoader = require('../lib/blog-feed-loader');

var CaseStudies = React.createClass({
render: function() {
return (
Expand Down Expand Up @@ -98,51 +100,13 @@ var BlogSection = React.createClass({
latestPostsData: []
}
},
loadGoogleAPI: function(callback) {
var head = document.getElementsByTagName("head")[0];
var gScript = document.createElement("script");
gScript.setAttribute("src", "https://www.google.com/jsapi");
gScript.onload = callback;
head.appendChild(gScript);
this.setState({googleAPILoaded: true});
},
componentDidMount: function() {
var self = this;
var google;
this.loadGoogleAPI(function() {
if (self.isMounted() && window.google) {
google = window.google;
google.load("feeds", "1", {
callback: function() {
var feed = new google.feeds.Feed("https://blog.webmaker.org/tag/teachtheweb/feed");
var latestPosts = [];
feed.load(function(result) {
var featured = result.feed.entries[0];
var post;
for (var i=1; i<4; i++) {
post = result.feed.entries[i];
latestPosts.push({
title: post.title,
publishedDate: post.publishedDate,
link: post.link
});
}
self.setState({
featuredPostData: {
title: featured.title,
author: featured.author,
publishedDate: featured.publishedDate,
contentSnippet: sanitizeHtml(featured.content, {
allowedTags: []
}).split(" ").slice(0,70).join(" ") + "...",
link: post.link
},
latestPostsData: latestPosts
});
});
}
});
}
blogFeedLoader(this, window, "https://blog.webmaker.org/tag/teachtheweb/feed", function(data) {
self.setState({
featuredPostData: data.featuredPostData,
latestPostsData: data.latestPostsData
});
});
},
render: function() {
Expand Down

0 comments on commit 809171e

Please sign in to comment.