x-swapi

A Star Wars API exercise.

This exercise does not need a package.json but you will need a server in order to use AJAX.

If you don’t have it already installed, I recommend to use live-server (which can be installed with npm i -g live-server and then run from the terminal with the live-server command in the directory of your project).

Steps

HTML

Create an index.html file with:

JavaScript

CSS

result

More Javascript

In your index.js:

Scaffolding Don't copy paste without thinking about what this code is meant for. ;) ````js function createModal() { // ... return element; } function showModal(contentElement) { // ... } function hideModal() { // ... } // you can copy that safely, just pay attention to where it has to be paste var modalElement = createModal(); var modalContentElement = modalElement.querySelector('.content'); var modalCloseButton = modalElement.querySelector('.controls button'); modalCloseButton.addEventListener('click', hideModal); document.body.appendChild(modalElement); var mainElement = //... function loadData(url, done) { // ... } function loadPeople(done) { loadData('https://swapi.co/api/people', done); } function loadPlanet(url, done) { loadData(url, done); } function renderPeople(people) { // ... sectionElement .querySelector('button') .addEventListener('click', function() { loadPlanet(person.homeworld, renderPlanet); }); mainElement.appendChild(sectionElement); }); } function renderPlanet(planet) { // ... } loadPeople(renderPeople); ````

More CSS

Add the following code to your style.css and complete it where necessary.

/* -------------------------------- */

.modal,
.modal>.underlay,
.modal>.body {
  position: absolute;
}

.modal,
.modal>.underlay {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.modal {
  height: 0;
  z-index: 10;
  overflow: hidden;
  transition: height 300ms ease;
}

.modal.open {
  height: 100%;
}

.modal>.underlay {
  z-index: 10;
  background-color: rgba(255, 255, 255, 0.7);
}


.modal>.body {
  /* fill here */
}

.modal .controls {
  /* fill here */
}

.modal .content {
  /* fill here */
}

star wars browser 1

Pager

As you have seen, the “people” object that you recieve from your AJAX call is looking a bit like:

{
  results: [
    {name: /* ... */},
    {name: /* ... */},
    {name: /* ... */},
    /* ... */
  ],
  count: 87,
  next: 'https://swapi.co/api/people/?page=2',
  previous: null
}

In the “results” array they are only 10 records, but the total amount of people is 87. So basically, it does not give you the complete list of people but only the first 10. In order to load (and display) the next 10 people, you will need to make an other request to the “people.next” URL.

In your CSS, make changes so that it looks like the previous layout and make styles for the pager buttons (so that they look a bit nicer).

BONUS: Refactoring

You probably have a lot of code which looks similar accross your functions. Try to clean that up. :)

BONUS 2: Add a loader indicator

Add a visual indication of the loading (between the click on the menu link until the content renders).

BONUS 3: Use the hashchange event

Using the hashchange.

BONUS 4: Opening crawl

Animate the opening crawl of the films in a star wars fashion.

Credits