Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Cocktails</h1>
<span id="message-container"></span><br>
<button id="reload-data">Reload Data</button>
<button id="clear-local">Clear IndexedDB</button>
<div class="container">
  <div id="cocktail-container" class="row"></div>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const APIROOT = 'https://www.thecocktaildb.com/api/json/v1/1/';
const CONTAINER = document.querySelector('#cocktail-container');
const MESSAGE_CONTAINER = document.querySelector('#message-container');

function getCocktails() {
  const cocktailsStore = db.transaction(["Cocktails"], "readonly").objectStore("Cocktails");
  let getCocktailData = cocktailsStore.getAll();
  getCocktailData.onsuccess = function(event) {
    if (event.target.result.length === 0) {
      MESSAGE_CONTAINER.innerHTML = "(Loading remote data)";
      loadCocktailData();
    }
    else {
      MESSAGE_CONTAINER.innerHTML = "(Displaying local data)";
      displayCocktailData(event.target.result);
    }
  }
}

function loadCocktailData() {
  fetch(APIROOT + 'filter.php?a=Alcoholic').then((response) => {
    if (response.status !== 200) {
      console.log('Status Code: ' + response.status);
      return;
    }
    response.json().then((data) => {
      const cocktailsStore = db.transaction(["Cocktails"], "readwrite").objectStore("Cocktails");
      // delete the old items in the database if there are any
      let clearCocktailRequest = cocktailsStore.clear();
      clearCocktailRequest.onsuccess = function(event) {
        // store the new items in the database
        data.drinks.forEach(item => {
          cocktailsStore.put(item);
        });
      }
      displayCocktailData(data.drinks);
    });
  }).catch((error) => {
    console.log(error);
  });
}

function displayCocktailData(data) {
  data.forEach(item => {
    CONTAINER.innerHTML += `
      <div class="col-xs-12 col-sm-6 col-md-4">
        <figure>
          <img src="${item.strDrinkThumb}" width="200">
          <figcaption>${item.strDrink}</figcaption>
        </figure>
      </div>`;
  });
}

// Indexeddb
let db;
let dbRequest = window.indexedDB.open("Cocktaildb", 1);

dbRequest.onerror = function(event) {
  alert("Database error: " + event.target.errorCode);
};
dbRequest.onsuccess = function(event) {
  db = event.target.result;
  getCocktails();
};
dbRequest.onupgradeneeded = function(event) { 
  const db = event.target.result;

  let cocktailStore = db.createObjectStore("Cocktails", { keyPath : 'idDrink' });
};

function handleReloadData() {
  CONTAINER.innerHTML = "";
  getCocktails();
}

function handleClearLocal() {
  const cocktaildb = db.transaction(["Cocktails"], "readwrite").objectStore("Cocktails");
  clearCocktailRequest = cocktaildb.clear();
  CONTAINER.innerHTML = "";
  getCocktails();
}

document.querySelector('#reload-data').addEventListener('click', handleReloadData);
document.querySelector('#clear-local').addEventListener('click', handleClearLocal);
              
            
!
999px

Console