Categories: softare development

Build a News App With Javascript ES6

In this tutorial, we are going to build a news app with HTML, Bootstrap and ES6 features of javascript.

We will also being using a free news endpoint from techcruch.

Let’s get started.

We will be working with this endpoint: https://techcrunch.com/wp-json/wp/v2/posts?per_page=100&context=embed

The first thing we’ll do is to create our HTML holding tags and add our Bootstrap dependency

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
    <title></title>
  </head>
  <body>
    <div class="container">
      <center>
      <div class="spinner-border" role="status" id="loading"></div>
    </center>
  <div class="row row-cols-1 row-cols-md-3 g-4" id="news"></div>
</div>
</body>

Next, we will write our Javascript code that consumes the endpoint

window.addEventListener('load', (event) => {
  const api_url =
    "https://techcrunch.com/wp-json/wp/v2/posts?per_page=100&context=embed";

// Defining async function
  async function getapi(url) {

  // Storing response
  const response = await fetch(url);

  // Storing data in form of JSON
  var data = await response.json();
  console.log(data);
  if (response) {
      hideloader();
  }
  show(data);
}
// Calling that async function
getapi(api_url);
})

Let’s do some explanation here.

Here we’re loading our endpoint once the webpage loads that’s why we’re using the load event listener, and we’re using Fetch API to get our response. Now when the page loads, we also have a spinner running simultaneously as well. Then we’re saying, if we have gotten a response, we want to hide the spinner. That’s why we calling the function hideloader()

We’re also have a function show(data) which will be showing our response. But first we have to define it.

  for (let item of data) {
      // let description = item.course_description.substring(0, 100).concat('...')
      tab += `
      <div class="col-12 col-md-6 mb-6 col-lg-4 d-flex">
      <div class="card shadow-light-lg mb-6 mb-md-0 lift lift-lg">
          <img src=${item.jetpack_featured_media_url} alt="" class="card-img-top">
            <div class="card-body position-relative">
              <div class="position-relative text-right mt-n8 mr-n4 mb-3">
                <span class="badge badge-pill badge-success">
                  <span class="h6 text-uppercase"><i class="fa fa-check"></i></span>
                </span>
              </div>

              <h3 class="text-capitalize" style="font-family:poppins" >
                ${item.title.rendered}
              </h3>

              <p class="text-muted">
                ${item.excerpt.rendered}
              </p>
              <button type="button" href="#showModal" id=${item.course_id} data-toggle="modal" class="btn btn-danger font-weight-bold text-decoration-none" >

Here, we are looping through our response and presenting them in our html.

Here’s the FULL WORKING CODE

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
    <title></title>
  </head>
  <body>
    <div class="container">
      <center>
      <div class="spinner-border" role="status" id="loading"></div>
    </center>
  <div class="row row-cols-1 row-cols-md-3 g-4" id="news"></div>
</div>
  <script>

window.addEventListener('load', (event) => {
  const api_url =
    "https://techcrunch.com/wp-json/wp/v2/posts?per_page=100&context=embed";

// Defining async function
  async function getapi(url) {

  // Storing response
  const response = await fetch(url);

  // Storing data in form of JSON
  var data = await response.json();
  console.log(data);
  if (response) {
      hideloader();
  }
  show(data);
}
// Calling that async function
getapi(api_url);

// Function to hide the loader
function hideloader() {
  document.getElementById('loading').style.display = 'none';
}
// Function to define innerHTML for HTML table
function show(data) {
  let tab = ``;

  // Loop to access all rows
  for (let item of data) {
      // let description = item.course_description.substring(0, 100).concat('...')
      tab += `
      <div class="col-12 col-md-6 mb-6 col-lg-4 d-flex">
      <div class="card shadow-light-lg mb-6 mb-md-0 lift lift-lg">
          <img src=${item.jetpack_featured_media_url} alt="" class="card-img-top">
            <div class="card-body position-relative">
              <div class="position-relative text-right mt-n8 mr-n4 mb-3">
                <span class="badge badge-pill badge-success">
                  <span class="h6 text-uppercase"><i class="fa fa-check"></i></span>
                </span>
              </div>

              <h3 class="text-capitalize" style="font-family:poppins" >
                ${item.title.rendered}
              </h3>

              <p class="text-muted">
                ${item.excerpt.rendered}
              </p>
              <button type="button" href="#showModal" id=${item.course_id} data-toggle="modal" class="btn btn-danger font-weight-bold text-decoration-none" >

Result:

So here’s the full result in its basic usage. If you would like to format the date to a more readable format, we’ve covered how to use moment.js here

Recent Posts

South Korea bans downloads of DeepSeek AI until further notice

The South Korean government announced on Monday that it had temporarily halted new downloads of…

5 days ago

Which programming language is best for software development?

As a software developer, choosing the right programming language for software development can be a…

1 week ago

What is a server farm?

A server farm, also known as a server cluster or data center, is a collection…

1 week ago

Elon Musk’s Starlink satellite internet service is awaiting authorization to begin operations in Pakistan.

Pakistan's mobile and broadband internet speeds rank in the bottom 10 percent globally, according to…

2 weeks ago

React Native Styling Guide

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React.…

2 weeks ago

You can now customize your Android home screen shortcut with any widget of your choice

Google is not only passionate about developing innovative apps and services but also about finding…

2 weeks ago