Topics discussed this week:
• Structure for a basic SPA
• XMLHttpRequests
• API calls
Deadline Thursday
This homework is more extensive and challenging than previous homework! Please read the instructions below carefully and follow them with great attention to detail. Start this homework as soon as you can and allow time for discussion and questions (slack!).
In this assignment you will built upon some existing code that is already pre-written by your teachers. Your homework consist of writing the code to make the application work as requested per week.
You are going to write a Single Page Application (SPA) that uses the GitHub API.
This application should display information about the available HYF GitHub repositories. The functionalities we would like to see in your application are as follows:
- The user should be able to select a repository from a list of available repositories.
- The application should display high-level information about the selected repository and show a list of its contributors.
- When clicking on the name of the selected repository the GitHub page for the corresponding repository should be opened in a new browser tab.
- When clicking on a contributor, the GitHub page for the contributor should be opened in a new browser tab.
Figure 1 below shows an example of what your application could look like. Note that this is just an example. If you find it boring or unimaginative, please improve on it! On the other hand, a simpler version is OK too, so long as you implement the expected functionality.
Figure 1. Example User Interface using Material Design principles.
You can fetch a list of HYF repositories through this API endpoint (What is an API Endpoint?):
https://api.github.com/orgs/HackYourFuture/repos?per_page=100
If you open this URL in the browser (try it!) you will receive JSON data about the available HYF repositories. This is the data that you will need to work with in this assignment.
Note the query string ?per_page=100
in the above URL. If you don't specify this query string you will only get the first 30 repositories (the default per_page
is 30). HackYourFuture has more than 30 repositories but less than 100.
The returned JSON data contains some basic information about each repository, such as name
, full_name
, description
etc. There are also many properties that contain URLs that can be used to obtain detail information about certain aspects of the repository.
The JSON data that is returned from the initial request to get repository information includes a property named contributors_url
. Use the value of this property to fetch a list of contributors.
You can find detailed information about the GitHub API by means of the link listed below. However, the documentation is very extensive and not easy to digest. For this homework it is not necessary to study the GitHub API documentation. We provide the link here for completeness.
GitHub API documentation: https://developer.github.com/v3/
In this homework we will be introducing a preferred coding style and supporting tools to help you write "clean code". A number of popular JavaScript Style Guides have recently emerged of which the one developed by Airbnb has been chosen for this homework and is recommended for subsequent use during the HYF curriculum. It is documented here:
While you do not need to read this guide in detail, it is recommended that you review sections 1-8, 12-13, 15-21 and 23. The tools installed during the project preparation step below will help you to implement these guidelines in your code. You will see error and warning messages in the VSCode editor when your code deviates from the recommended style. An additional check will be done when you submit your homework as a pull request on GitHub.
You will be working on the same application during the next three weeks. For each week you will need to create a new Git branch, as listed in the Table 1 below.
Week | Branch | Assignment |
---|---|---|
1 | week1 |
- Create a basic application using callbacks to handle network requests. |
2 | week2 |
Based on the week1 branch:- Refactor the callbacks to promises. - Make the UI responsive. |
3 | week3 |
Based on the week2 branch:- Refactor the application to use ES6 Classes and async/await. - Make the app ARIA-compliant. |
Table 1. Homework schedule
Instructions
-
Fork the JavaScript3 repository (this repository) to your own GitHub account.
-
Clone the fork to your laptop.
-
Open the newly created
JavaScript3
folder from the cloned repository in VSCode. -
Install the following extension in VSCode:
Prettier - Code formatter.
-
Open a Terminal window in VSCode and type the following command to install Prettier and ESLint tools as required for the homework:
npm install
-
Create a new branch for the week 1 homework with the following command:
git checkout -b week1
The files that make up the application are located in the homework
folder. It contains the following files:
Filename | Description |
---|---|
hyf.png |
Contains the HackYourFuture logo. |
index.html |
The application's HTML file. |
index.js |
A starter JavaScript file. |
style.css |
A starter CSS file. |
Although you should only modify files in the homework
folder, we recommend that you always open the JavaScript3
folder rather than directly opening the homework
folder in VSCode. The JavaScript3
folder contains the actual git repository and the configuration files required by the installed tools.
Do not change or delete any files outside of the homework
folder!
-
Open
index.html
and examine its contents (but don't modify anything). Notice that the HTMLbody
looks like this:<body> <div id="root"></div> <script src="./index.js"></script> </body>
The
body
tag contains a singlediv
to which you will need to dynamically append HTML elements through your JavaScript code inindex.js
. -
Open
index.js
. This file contains a starter set of code for you to expand. It contains the following three functions:Function Description fetchJSON
Uses XMLHttpRequest
to fetch JSON data from an API end point. This function uses an asynchronous callback.createAndAppend
A utility function for easily creating and appending HTML elements. main
Contains the start-up code for the application. index.js
also contains a constant with the URL for the HYF repositories as listed in section 2.2.1:const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
-
Open the
index.html
file in your browser. Notice that it produces the same JSON output that you saw previously when you opened the URL directly in the browser. -
Review the
main()
function inindex.js
and examine how this code renders the JSON output in the browser by means of apre
HTML element (for demonstration purposes).
The assignment is to produce an application similar to the one illustrated in Figure 1 above.
It should include the following components:
-
An HTML
select
element from which the user can select a HYF repository. Thisselect
element must be populated withoption
elements, one for each HYF repository. -
A left-hand column that displays basic information about the selected repository.
-
A right-hand column that displays a list of contributors to the repository.
In case you run out of time, you can also do the contributors list in week 2.
Functional Requirements:
- The list of repositories in the
select
element should be sorted (case-insensitive) on repository name. - At start-up your application should display information about the first repository as displayed in the
select
element. - When the user changes the selection, the information in the web page should be refreshed for the newly selected repository.
- You should be able to click on the repository name of the selected repository to open a new browser tab with the GitHub page for that repository.
- You should be able to click on a contributor to open a new browser tab with the GitHub page for that contributor.
- You should render network errors to the DOM (see Figure 2 below for an example). Do not use
console.log
as regular users will not see the console output. Use the predefinedalert-error
class fromstyle.css
to style your error. - Your UI should be responsive. Try it with Chrome Developer Tools in the browser, using a mobile phone format and a tablet format, portrait and landscape. If necessary, you can also do this work in week 2.
Figure 2. Rendering of network errors.
Code modifications:
index.js
- Add new functions and modify function
main()
as you see fit. It is not likely that you will need to modifyfetchJSON()
andcreateAndAppend()
.
style.css
- Add your own styling.
Hints:
-
Add one
option
element per repository to theselect
element, where eachoption
element has the array index of the repository as itsvalue
attribute and the name of the repository as its text content:<select> <option value="0">alumni</option> <option value="1">angular</option> <!-- etc --> </select>
-
To sort the list repositories use
.sort()
and.localeCompare()
. -
Use CSS media queries and Flexbox to make the UI responsive.
-
To force a
404
network error so that you can test the rendering of errors, change the URL to make an invalid GitHub request, e.g. append anx
toorgs
:orgsx
.
If necessary, review the instructions how to Hand in homework using GitHub pull request.
To test whether your code will be accepted when you submit your homework as a pull request you need to ensure that it does not contain ESLinr errors. Open a terminal window in VSCode and type the following command:
npm test
If any errors or warnings are reported by this command you need to fix them before submitting a pull request.
In addition, check for the following:
- Have you removed all commented out code (should never be present in a PR)?
- Do the variable, function and argument names you created follow the Naming Conventions?
- Is your code well-formatted (see Code Formatting)?
If the answer is 'yes' to the preceding questions you are ready to follow these instructions:
-
Push your
week1
branch to GitHub:git push -u origin week1
-
Create a pull request for your
week1
branch.
BONUS : Code Kata Race
Deadline Sunday morning
Go through the reading material in the README.md to prepare for your next class.