GithubHelp home page GithubHelp logo

ibm-watson-data-lab / shopping-list-vanillajs-pouchdb Goto Github PK

View Code? Open in Web Editor NEW
20.0 5.0 18.0 2.26 MB

Shopping List is an Offline First demo Progressive Web App built using Vanilla JS and PouchDB.

Home Page: https://ibm-watson-data-lab.github.io/shopping-list-vanillajs-pouchdb

License: Apache License 2.0

CSS 12.45% HTML 30.37% JavaScript 57.18%
demo-apps offline-first progressive-web-apps vanilla-javascript pouchdb

shopping-list-vanillajs-pouchdb's Introduction

Build Status IBM Cloud Deployments

Create an Offline First Shopping List with JavaScript (aka Vanilla JS) and PouchDB

This code pattern is a reference implementation of an Offline First shopping list app, built as a Progressive Web App using plain JavaScript (affectionately referred to as Vanilla JS) and PouchDB. This app is part of a series of Offline First demo apps, each built using a different stack.

When the reader has completed this Code Pattern and explored the code in this GitHub repository, they will understand how to:

  • create a shopping list web application that stores its data in a local PouchDB database.
  • turn the web application into a Progressive Web App that works with or without an internet connection.
  • make the app sync to and from a remote Cloudant database.

architecture

Flow

  1. Browser loads Progressive Web App's resources from the web server
  2. User interacts with the web app to add shopping lists and list items
  3. Data is stored locally in PouchDB
  4. PouchDB syncs its data with a remote IBM Cloudant database

Included components

  • Cloudant NoSQL DB: A fully-managed data layer designed for modern web and mobile applications that leverages a flexible JSON schema. Based on the open source Apache CouchDB, IBM Cloudant provides additional full text and geospatial capabilities.

Featured technologies

  • PouchDB - an in-browser database that can replicate to and from a remote Apache CouchDB or IBM Cloudant database.
  • JavaScript - this demo uses no frameworks, just plain vanilla JavaScript.
  • Apache CouchDB - modern, document database hosted on your server or in the cloud.

Key concepts

This shopping list app is a small single page web application consisting of an HTML file, a couple of CSS files, and a couple of JavaScript files, and the PouchDB library. No other JavaScript framework framework (such as jQuery or Ember.js) or library other than PouchDB is used. The web page will allow multiple shopping lists to be created (e.g., Groceries, Clothes, etc.) each with a number of shopping list items associated with them (e.g., Bread, Water, etc.).

So what sets this app apart? Its Offline First architecture. The Offline First approach plans for the most constrained network environment first, enabling a great user experience even while the device is offline or has only an intermittent connection, and providing progressive enhancement as network conditions improve. This design also makes the app incredibly performant (fast!) on the best of networks.

PouchDB, CouchDB, and Service Worker are the primary tools that turn our simple shopping list app into a high performance, offline-capable Progressive Web App.

Data stays safe on your device, even while it's offline.
Persistance of shopping lists and item data entered by the user is achieved using the in-browser database PouchDB. This will allow your data to survive between sessions and when disconnected from the network. (Whether you remember that you need juice while you're on your trusty home Wi-Fi or in the middle of the wilderness, you can still add it your list.)

Data syncs between devices when a connection is available.
When a connection is available, the data is synced from the local device to a CouchDB database in the cloud, and can thus be shared across multiple devices or users. (Need to share your grocery list with your roommate or access it on both your phone and your laptop? No problem!)

The app loads quickly, even while offline.
To keep the app itself functional while offline, a Service Worker is used to cache page resources (the most important HTML, CSS, and JavaScript files) when the web application is first visited. Each device must have a connection for this first visit, after which the app will be fully functional even while offline or in shoddy network conditions. (No more error messages or frustratingly slow page loads.)

The app can be installed on a mobile device.
In combination with the Service Worker used for caching, a manifest file containing metadata allows the app to become a Progressive Web App, an enhanced website that can be installed on a mobile device and can then be used with or without an internet connection. (It's secretly still a website, but you can access it through one of those handy dandy little app icons on your homescreen!)

Explore the code in this GitHub repository to see how the Offline First design is applied.

Watch the Video

Tutorial

Refer to the tutorial for step-by-step instructions on how to build your own Offline First shopping list Progressive Web App with Vanilla JS (aka plain old JavaScript) and PouchDB.

Live demo

To see this app in action without installing anything, simply visit https://ibm-watson-data-lab.github.io/shopping-list-vanillajs-pouchdb/ in a web browser or on your mobile device.

Steps

Want to check out the end product on your own machine? Follow these steps to deploy your own instance of the shopping list app.

This app can be deployed to IBM Cloud. You can also run this app on your local machine for development purposes using either a local Apache CouchDB instance or an IBM Cloudant service instance from the IBM Cloud Catalog.

Deploy to IBM Cloud

Deploy to IBM Cloud

  1. Press the above Deploy to IBM Cloud button and then click on Deploy.

  2. In Toolchains, click on Delivery Pipeline to watch while the app is deployed. Once deployed, the app can be viewed by clicking View app.

  3. To see the app and services created and configured for this code pattern, use the IBM Cloud dashboard. The app is named shopping-list-vanillajs-pouchdb with a unique suffix. The following services are created and easily identified by the sljsp- prefix:

    • sljsp-CloudantNoSQLDB

Run locally

  1. Clone the repo
  2. Run the server
  3. Create a Cloudant or CouchDB service

1. Clone the repo

Clone the shopping-list-vanillajs-pouchdb locally. In a terminal, run:

$ git clone https://github.com/ibm-watson-data-lab/shopping-list-vanillajs-pouchdb

2. Run the Server

Assuming you have pre-installed Node.js and npm, run the following commands:

$ cd shopping-list-vanillajs-pouchdb
$ npm install
$ npm start

3. Create a Cloudant or CouchDB service

PouchDB can synchronize with CouchDB and compatible servers. To run and test locally, you can install CouchDB. Alternatively, you can use a hosted Cloudant NoSQL DB service for your remote DB.

Installing Apache CouchDB

Install CouchDB 2.1. Instructions are available for installing CouchDB 2.1 on Unix-like systems, on Windows, on Mac OS X, on FreeBSD, and via other methods.

Configure CouchDB for a single-node setup, as opposed to a cluster setup. Once you have finished setting up CouchDB, you should be able to access CouchDB at http://127.0.0.1:5984/. Ensure that CouchDB is running and take note of your admin username and password.

Creating a Cloudant NoSQL DB service

To provision a managed Cloudant NoSQL DB

  • Log in to IBM Cloud.

    Sign up for an account, if you do not already have one.

  • Provision a Cloudant NoSQL DB Lite plan instance, which is free.

    If desired, you can also re-use an existing Cloudant NoSQL DB service instance. (Open the Data & Analytics resources dashboard to see a list of pre-provisioned instances that you have access to.)

  • Open the Service credentials tab.
  • Add new credentials for this service instance if no credentials have been defined yet.
  • View the credentials and note the value of the url property, which has the following format: https://username:[email protected].

Tip: Select the Manage tab and click Launch to open the Cloudant dashboard and manage the service instance.

Database and replication setup

  1. Create the remote database
  2. Enable CORS
  3. Set the replication target

1. Create the remote database

  • Use the Cloudant or CouchDB dashboard to create a database.

  • Select the Databases tab on the left and then use the Create Database button to create the "shopping-list" database. The Shopping List app can be used locally before the database exists, but cannot sync until the remote database is completed.

2. Enable CORS

  • Open the Cloudant or CouchDB dashboard to enable Cross-Origin Resource Sharing (CORS).

  • Select the Account Settings (or config) tab and open the CORS tab.

  • Enable CORS and restrict the domain as needed for security.

3. Set the replication target

Run the Shopping List app and use the Settings form to enter your database URL. If you use the IBM Cloud Cloudant URL taken from the service credentials as described above, the URL includes user name and password.

Add /shopping-list to the URL to connect to the database that you created.

Using the app

The app allows you to create a shopping list by clicking on the plus sign. Click on the list to see its items. Then, you can add items to the list by clicking the plus sign. There is a checkbox to allow you to mark the items complete as you buy load up your cart.

When you have not configured your Replication Target or when you are offline, the lists will not sync. One good way to test this is to run two browsers. You can use Chrome and Firefox and have different lists in each.

When you go online and have the database and CORS enabled and the Replication Target is set, the shopping lists will sync. You will then be able to use both lists from either browser.

Running the tests

This repository uses the Karma Test Runner. Our continuous integration will run the tests when you push to GitHub. To test your code on your machine, simply run npm test.

This project does not, at present, have any automated tests. Tests can be added to test/test.js. If you'd like to contribute some then please raise an issue and submit a pull-request - we'd be very happy to add them!

Coding style

This repository's JavaScript code is built to the JavaScript Standard Style. Our continuous integration will check your code against these standards when you push to GitHub. To test your code on your machine, simply run npm test.

Deploying to GitHub Pages

The shopping list can be deployed to and accessed from GitHub Pages.

  1. Create a new gh-pages branch and check it out, with the command: git checkout -b gh-pages.
  2. Edit the gh-pages branch keeping only files needed to serve up the application contain i.e., delete all files except:
    • index.html
    • worker.js
    • manifest.json
    • files in the css directory
    • files in the favicons directory
    • files in the js directory
  3. Push the gh-pages branch to GitHub: git push origin gh-pages

Once gh-pages has been pushed, it may take a few minutes, but the shopping list should be accessible via the repository's GitHub Pages url.

Privacy Notice

Refer to https://github.com/IBM/metrics-collector-service#privacy-notice.

Disabling Deployment Tracking

To disable tracking, simply remove require('metrics-tracker-client').track() from the app.js file in the top level directory.

Links

License

Apache 2.0

shopping-list-vanillajs-pouchdb's People

Contributors

bradley-holt avatar glynnbird avatar lornajane avatar markwatsonatx avatar ptitzler avatar terichadbourne avatar vabarbosa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

shopping-list-vanillajs-pouchdb's Issues

README "Run locally" Section

Using the README template for reference, change the following in the "Run locally" section of the README:

  • Change the repo name in the "clone the repo" section.
  • Change the "install the prerequisites" section as needed.
  • Change the "run the server" section as needed.
  • Change the "Create a Cloudant or CouchDB service" section as needed.

Sync Error when pasting in Shopping Lists Settings

Everything is fine until the "Set the Replication Target" step which I can't get through. I've followed the directions in the README and watched two separate videos going through the steps repeatedly and meticulously but get a Sync Error every time.

Feature: Multi-User / Multi-Device

  • Multi-User / Multi-Device
    • As a <new user>, I want to <sign up for the app> so that <I can use the app>.
    • As an <existing user>, I want to <sign in to the app> so that <I can use the app>.
    • As an <existing user>, I want to <sign out of the app> so that <I can protect my privacy>.

README "Database and replication setup" Section

Using the README template for reference, change the following in the "Database and replication setup" section of the README:

  • Ensure that the referenced screenshots are added to the repository.
  • Change the "create the remote database" section as needed.
  • Change the "enable CORS" section as needed.
  • Change the "set the replication target" section as needed.

512x512 icon missing

In your manifest file you reference a 512x512 icon which isn't there.

  • either remove the reference from manifest.json
  • or, add a 512x512 icon

I only noticed because I'm copying your service worker ;)

Accessibility please!

Using my standard tools (Vimium in Chrome), I'm unable to focus or click on any part of the web interface.

IBM Code Patterns Review

Review to ensure compliance with IBM Code Pattern standards, compliance with our team's established standards for these demo apps, and overall quality.

Apache License

  • Ensure that a LICENSE file has been added that licenses this code under the Apache License 2.0.
  • Ensure that a "License" section is included in the README, using the README template for reference.

Bug: New item temporarily added to wrong sublist on other devices

I'm running the app as hosted at https://ibm-watson-data-lab.github.io/shopping-list-vanillajs-pouchdb/# and connecting to my Cloudant DB, with one Chrome window and one Firefox window running to represent different devices. If I visit a different sublist in one browser than the other and add a new item to it, it appears on the screen I'm currently looking at in the other browser, meaning it puts itself temporarily in the wrong sublist. If I refresh or go back to the list of lists and back in, the issue is resolved and the new item is shown in the right place.

For example, in the screenshot below I added an item to the electronics sublist in Chrome and it appeared immediately in the Groceries sublist on the right.

image

Maintainers

Using the MAINTAINERS template for reference, create a MAINTAINERS.md file with the following:

  • Introduction
  • Methodology
  • Merge approval
  • Reviewing Pull Requests

Feature: Shopping

  • Shopping
    • As a <person shopping for groceries>, I want to <view items on my shopping list> so that <I can remember what items to buy>.
    • As a <person shopping for groceries>, I want to <add an item to my shopping list> so that <I can remember to buy that item>.
    • As a <person shopping for groceries>, I want to <remove an item from my shopping list> so that <I can change my mind on what to buy>.
    • As a <person shopping for groceries>, I want to <check off an item on my shopping list> so that <I can keep track of what items I have bought>.

Feature: Planning

  • Planning
    • As a <person planning to shop for groceries>, I want to <create a shopping list> so that <I can add items to this shopping list>.
    • As a <person planning to shop for groceries>, I want to <add an item to my shopping list> so that <I can remember to buy that item when I am at the grocery store later>.
    • As a <person planning to shop for groceries>, I want to <remove an item from my shopping list> so that <I can change my mind on what to buy when I am at the grocery store later>.

Improve Clarity of Reference Implementation Usefulness

  • Cover key concepts such as Offline First, Service Workers, etc. (can be pulled from tutorial)
  • Explain why Offline First makes an app (in this case, a shopping list app) more useful through the lens of user experience
  • Explain that syncing can be used across multiple devices (laptop, phone, tablet) or users
  • Explain why the stack used in this implementation is well suited to Offline First tech
  • Place tutorial section to above fold (if it exists)
  • Place live demo above fold (if it exists)
  • Clarify that what they're seeing is a "reference implementation" - just code
  • Make it clear that the "Steps" are for viewing the app as an end user, NOT for building the app
  • Add "and explored the code in this GitHub repo" after "When the reader has completed this Code Pattern" so that promise about learning becomes true
  • Offer alternative (eg if you're not already familiar with React, you may want to check out this Vanilla JS...)

(This issue is written so that we can apply it to the template as well - not all checklist items are relevant to the vanilla version.)

README "Using the app" Section

Using the README template for reference, change the following in the "Using the app" section of the README:

  • Change the introductory section as needed.
  • Ensure that the referenced screenshot is added to the repository.
  • Write the "running the app" section. (NOT NEEDED, replaced by Run locally section)
  • Write the "running the tests" section.
  • Write the "deploying to GitHub Pages" section.

README Badges

Using the README template for reference, add badges to the top of the README being sure to:

  • Change the "Build Status" badge repository source URL.
  • Change the "Build Status" badge repository link URL.
  • Change the "IBM Cloud Deployments" badge tracking number in the URL.

Code Comments

Ensure that code is well documented and useful as a reference implementation.

README "Deploy to IBM Cloud" Section

Using the README template for reference, change the following in the "Deploy to IBM Cloud" section of the README:

  • Change the "Deploy to IBM Cloud" button source URL.
  • Change the "Deploy to IBM Cloud" button link URL.
  • Update the app name.
  • Update the prefix name.
  • Document the service(s) created by the deployment.
  • Test the "Deploy to IBM Cloud" button to ensure it works as expected.

Feature: Geolocation

  • Geolocation
    • As a <person planning to shop for groceries>, I want to <associate a shopping list with a grocery store> so that <I can be notified of this shopping list when I am physically at that grocery store>.
    • As a <person associating a shopping list with a physical store>, I want to <access previously-used locations> so that <I can quickly find the physical store for which I am searching>.
    • As a <person shopping for groceries>, I want to <be notified of a shopping list when I am physically at the grocery store associated with that shopping list> so that <I can quickly find the shopping list for my current context>.

Contributing

Using the CONTRIBUTING template for reference, create a CONTRIBUTING.md file with the following:

  • Welcoming introduction
  • Proposing new features section
  • Fixing bugs section
  • Merge approval section
  • Setup section

README Introduction

Using the README template for reference, change the following in the introductory section of the README:

  • Change the title to indicate the technologies used.
  • Change the first paragraph to indicate the technologies used.
  • Document two or three goals for this Code Pattern.
  • Produce an architecture diagram and add it to the correct path.
  • Add the flow steps based on the architecture diagram.
  • Add the included components.
  • Add the featured technologies.

Nothing stored locally

Installed, configed, started everything locally. Clicking pink button opens Create a Shopping List form but then clicking Add (after closing the form) shows nothing. Some details:

  • When installing CouchDB, it never asked me to choose standalone or clustered.
  • Futon interface looks very different from your screenshots and other tutes. The only option for cors is either true or false

Feature: Offline First

  • Offline First
    • As a <person shopping for groceries>, I want to <have the app installed on my device> so that <I can continue to utilize my shopping list when no internet connection is available>.
    • As a <person shopping for groceries>, I want to <have my shopping list stored locally on my device> so that <I can continue to utilize my shopping list when no internet connection is available>.
    • As a <person shopping for groceries>, I want to <sync my shopping list with the cloud> so that <I can manage and utilize my shopping list on multiple devices>.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.