From 809171e0f20019813730510453827958af906456 Mon Sep 17 00:00:00 2001 From: Mavis Ou Date: Wed, 27 May 2015 12:33:47 -0700 Subject: [PATCH] an attempt at refactoring blog feed loader code into a separate module... --- lib/blog-feed-loader.js | 65 +++++++++++++++++++++++++++++++++++++++++ pages/home.jsx | 50 +++++-------------------------- 2 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 lib/blog-feed-loader.js diff --git a/lib/blog-feed-loader.js b/lib/blog-feed-loader.js new file mode 100644 index 000000000..286a6021e --- /dev/null +++ b/lib/blog-feed-loader.js @@ -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; + diff --git a/pages/home.jsx b/pages/home.jsx index c38cf6e1b..2be0aa9bb 100644 --- a/pages/home.jsx +++ b/pages/home.jsx @@ -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 ( @@ -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() {