Coding an Advent Calendar: Day 7 (REST API + ReactJS)

This year, I decided to create an Advent Calendar website in HTML5, CSS3, and JavaScript. My hopes for the site are to showcase some of my frontend development abilities as well as make something fun, functional, and reusable.

Day 7

WordPress 4.7 “Vaughan”

WP REST API!!
Today is the big day! WordPress 4.7 launched this morning (in Korea) with the WordPress REST API content endpoints included in core (and a pretty great updated REST API Developer Reference as well). So… you know what that means. Today is the day I get the Advent Calendar to pull my blog posts with the (core) WP REST API! There are X steps:

  1. Revisit Day 2 where I set up the basic infrastructure for ReactJS & REST
  2. Update to WordPress 4.7 – where REST endpoints will work immediately (cool)
  3. Install Postman for Chrome* to test calls to the REST API
  4. Configure the REST URL (I have mine pull from THIS Category)
  5. Add it to the Codepen
  6. Watch the magic happen!! (…almost)

*Side note: Be sure to get Postman’s output as JSON for an easier view.

React Component
Actually, I hadn’t already created my React function to render the retrieved Component. I’d only filled in a blank <div> as a placeholder. So, the next step was to create an HTML structure in the React class that would receive the array of Posts from my REST API query and render them to the page.

Then, I added some basic CSS styling to make them look a little better on the page after loading.

BabelJS  
Finally, after setting everything up and Saving the Codepen, I noticed a small error in my JavaScript console in Chrome: "Unexpected token <". That’s weird, I thought. But then I noticed that the color highlighting in the Codepen was incorrect after a certain point – and the posts from React weren’t displaying quite properly. I checked back at the other Codepens I’d referenced that were using React to load JSON data and noticed that many were using the Babel JavaScript preprocessor. I thought, what the heck, added it to my project and *BAM!* the magic truly DID begin! Smooth loading of posts on the Codepen directly from this blog!

But… that’s Codepen. If I wanted to continue local development, it wasn’t going to be nearly so easy as that. I’d never heard of BabelJS before this, so I started investigating it and found out that it is often used with React and other scripts to create modern JS like ES2016. I realized I’d need to install the Node Modules for React, Babel, and Grunt to run all of these together, so my research and work began. There were X steps to complete this process:

  1. Run npm install --save-dev babel-cli babel-preset-latest babel-preset-react grunt-babel to install all the necessary development dependencies
  2. Run npm install --save react react-dom to save the necessary project dependencies
  3. Update package.json to recognize babel (rather than creating a .babelrc file)
  4. Update Gruntfile.js to “watch” for changes to my React app’s JS file and run the compile task
  5. Separate out my React Component code from the functions.js file to better manage the two separately
  6. Also, include React and React-DOM in the React app’s JS file

So far, I’ve been able to get Grunt to successfully “watch” for changes to my Sass files and my ReactJS files and run their compilation tasks. But, now I’m getting an Uncaught ReferenceError: require is not defined(...) error. Well, looks like fixing this is on the schedule for tomorrow!~

References

The following are some React and BabelJS references I was perusing today as I tried to get things to work. I haven’t finished reading through all of them yet:

  1. Official React site: A JavaScript Library for Building User Interfaces
  2. Writing React components as CommonJS modules
  3. NPM: A RequireJS plugin for loading jsx in require.js and r.js
  4. The React.js Way: Getting Started Tutorial
  5. 9 things every React.js beginner should know
  6. ES6 with Babel.JS
  7. Get To Know Babel The JavaScript Compiler Tutorial
  8. Setting up an ES6 Project Using Babel and Browserify
  9. Babel + Grunt === ES6 Now!
  10. ES6 with Babel & Grunt

Additionally, I just stumbled across a Lynda.com video that addresses using BabelJS to compile React. In this video, Eve Porcello shows how you can do in-browser Babel compilation by importing the script via a CDN, then using <script type="text/babel"> tags around any code you want compiled by Babel.

She does note that in-browser compilation is slow – and shouldn’t be used in a production environment. So, it’s good that I’ve started getting Babel set up as a module to precompile my JS before using it in the webpage.


Work Completed (to date)

  • December 7, 2016
    • Pull post data with the WP REST API in WordPress core!!
    • Write structural code for the React Component to be rendered
    • Install and setup BabelJS to compile the React code
  • December 6, 2016
    • Add README.md
    • Add a GitHub Issue to hold usable images
    • Add LICENSE
  • December 5, 2016
    • Create a GitHub repository and full site files for easier management
    • Setup Grunt.js to compile my Sass into CSS
    • Begin blogging about the process
  • December 4, 2016
    • Countdown clock (JS Date class & jQuery Easing) with SVGs
    • Dynamic text output for Year based on the current date
    • CSS only slider (off by 5px each slide)
  • December 3, 2016
    • CSS bow & ribbon
    • Footer with FontAwesome presents
    • Hover, active, and “Christmas Day” styles for footer presents
  • December 2, 2016
    • React.js + Axios.js initial code structure
    • Color palette
    • CSS → Sass
  • December 1, 2016
    • Let it snow
    • Typography choices
    • CSS presents (first design)
    • Design notes menu

Coding an Advent Calendar: Day 5 (Maintenance)

This year, I decided to create an Advent Calendar website in HTML5, CSS3, and JavaScript. My hopes for the site are to showcase some of my frontend development abilities as well as make something fun, functional, and reusable.

Day 5: Maintenance

GitHub Repository
After coding for 1-2 hours each day for 4 days, I found my code getting quite long:

  1. HTML: 344 lines
  2. SCSS: 602 lines
  3. JS: 187 lines

This quickly starts to become a lot to manage within a Codepen – given that the 3 editors are all small boxes to the side of the screen. I prefer to work on bigger projects in a good code editor like Atom or Brackets (I use Netbeans for really big projects like WordPress themes or plugins). So, I decided it was a good time to upgrade the project to “bigger” and continue development primarily in Atom (which has a convenient Live Server package that really aids in development).

I’ll continue to update the Codepen with my code updates – at least for now.

Setup a Grunt Taskrunner
Codepens are nice in that the Sass, or Less, or PostCSS is instantly compiled and usable as CSS as soon as you write it. But, things don’t work like that in stand-alone projects. So my first step after pulling my files from Codepen and creating a GitHub repository for them was to start preparing to use Grunt.js as a taskrunner to watch and compile my Sass into web-browser-readable CSS. Here are the steps I took (which will be outlined in further detail in a later post):

  1. In the global environment
    1. Install Node.js (Windows)
    2. Install Ruby (Windows)
    3. Install Sass: gem install sass
    4. Install Grunt-cli: npm install -g grunt-cli
  2. In the project folder
    1. git init, commit, and push to remote repository
    2. npm init to setup package.json which keeps track of dependencies
    3. npm install grunt-contrib-sass --save-dev installs the package locally (without --save-dev it would be a global installation)
    4. npm install grunt-contrib-watch --save-dev
    5. npm install grunt-autoprefixer --save-dev to autoprefix the CSS
    6. Create the Gruntfile.js that controls grunt and the Sass file compilation
    7. Run grunt from the command line (in my project folder) to start the taskrunner and allow me to modify my Sass files (CTRL + C ends the taskrunner)

For more information on this process, check out Morten Rand-Hendriksen’s helpful Course @Lynda.com:

WordPress: Developing with Sass and Grunt.js

Begin blogging  
Finally, in order to begin attempting to get the WP-API running well on this site, I needed some content on a WordPress site to begin pulling from – hence the creation of this series of blog posts.

As mentioned previously, I’m also hoping to create a 25-day series for Advent that highlights my favorite typefaces. The goal is to allow users to switch the series of posts that the REST API pulls from. They’d be able to either load the “Coding an Advent Calendar” series, or the “Advent Typography” series.


Work Completed (to date)

  • December 5, 2016
    • Create a GitHub repository and full site files for easier management
    • Setup Grunt.js to compile my Sass into CSS
    • Begin blogging about the process
  • December 4, 2016
    • Countdown clock (JS Date class & jQuery Easing) with SVGs
    • Dynamic text output for Year based on the current date
    • CSS only slider (off by 5px each slide)
  • December 3, 2016
    • CSS bow & ribbon
    • Footer with FontAwesome presents
    • Hover, active, and “Christmas Day” styles for footer presents
  • December 2, 2016
    • React.js + Axios.js initial code structure
    • Color palette
    • CSS → Sass
  • December 1, 2016
    • Let it snow
    • Typography choices
    • CSS presents (first design)
    • Design notes menu