npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

richer

v0.2.2

Published

Shopify richcart with no jquery dep

Downloads

9

Readme

Richer

js-standard-style

What is it?

This library is built on top of slater (which is a fork of shopify slate). It is based on the old implementation of the Timber depreciated richcart. The idea behind this comes from Slate and Timber both being tied to jQuery and wanting to build something that could be used independent of that.

Using It

We use richer to handle API requests to the shopify AJAX cart. How you implement it is entirely up to you. We expose a couple of common routes that allow you to easily handle updating/refreshing/generating your ajax cart.

I'll outline a number of examples in YOYO below

Initialize a cart

import RicherAPI from 'richer'

// Some DOM defaults
const defaults = {
  addToCart: '.js-add-to-cart', // classname
  addToCartForm: 'AddToCartForm', // id
  cartContainer: 'CartContainer', // id
  cartCounter: 'CartCounter', // id
  items: []
}

const config = Object.assign({}, defaults, options)

const dom = {
  addToCartForm: document.getElementById(config.addToCartForm),
  cartContainer: document.getElementById(config.cartContainer),
  cartCounter: document.getElementById(config.cartCounter)
}

RicherAPI.getCart(cartUpdateCallback)

// Updates a cart number and builds our cart
const cartUpdateCallback = (cart) => {
  updateCount(cart)
  buildCart(cart)
}

const updateCount = (cart) => {
  const counter = dom.cartCounter
  counter.innerHTML = cart.item_count
}

const buildCart = (cart) => {
  const cartContainer = dom.cartContainer
  cartContainer.innerHTML = null

  if (cart.item_count === 0) {
    cartContainer.innerHTML = `<p>We're sorry your cart is empty</p>`
    return
  }

  var el = cartBlock(cart.items, cart, update)

  function cartBlock (items, cart, qtyControl) {
    return yo`
      <div class='r-cart'>
        ${items.map((item, index) => {
          const product = cleanProduct(item, index, config)
          return yo`
            <div class="r-cart__product f jcb">
              <div>
                <img src='${product.image}' alt='${product.name}' />
              </div>
              <div class="r-cart__product_info">
                <h5><a href='${product.url}'>${product.name}</a></h5>
                ${product.variation ? yo`<span>${product.variation}</span>` : null}
                ${realPrice(product.discountsApplied, product.originalLinePrice, product.linePrice)}
                ${yo`
                  <div class="r-cart__qty f jcb">
                    <div class="r-cart__qty_control" onclick=${() => qtyControl(product, product.itemMinus)}>
                      <svg width="20" height="20" viewBox="0 0 20 20"><path fill="#444" d="M17.543 11.029H2.1A1.032 1.032 0 0 1 1.071 10c0-.566.463-1.029 1.029-1.029h15.443c.566 0 1.029.463 1.029 1.029 0 .566-.463 1.029-1.029 1.029z"/></svg>
                    </div>
                    <span>${product.itemQty}</span>
                    <div class="r-cart__qty_control" onclick=${() => qtyControl(product, product.itemAdd)}>
                      <svg width="20" height="20" viewBox="0 0 20 20" class="icon"><path fill="#444" d="M17.409 8.929h-6.695V2.258c0-.566-.506-1.029-1.071-1.029s-1.071.463-1.071 1.029v6.671H1.967C1.401 8.929.938 9.435.938 10s.463 1.071 1.029 1.071h6.605V17.7c0 .566.506 1.029 1.071 1.029s1.071-.463 1.071-1.029v-6.629h6.695c.566 0 1.029-.506 1.029-1.071s-.463-1.071-1.029-1.071z"/></svg>
                    </div>
                  </div>
                `}
              </div>
            </div>
          `
        })}
        ${subTotal(cart.total_price, cart.total_cart_discount)}
      </div>
    `
  }

  function subTotal (total, discount) {
    // TODO: handling discounts
    const totalPrice = slate.Currency.formatMoney(total)  // eslint-disable-line
    return yo`
      <div>
        <h5>Subtotal: ${totalPrice}</h5>
      </div>
    `
  }

  function realPrice (discountsApplied, originalLinePrice, linePrice) {
    if (discountsApplied) {
      return yo`
        <div>
          <small className='strike'>${originalLinePrice}</small>
          <br /><span>${linePrice}</span>
        </div>
      `
    } else {
      return yo`
        <span>${linePrice}</span>
      `
    }
  }

  function update (item, quantity) {
     RicherAPI.changeItem((item.index + 1), quantity, refreshCart)
  }

  function refreshCart (cart) {
    let newCart = cartBlock(cart.items, cart, update)
    yo.update(el, newCart)
  }

  cartContainer.appendChild(el)
}
import RicherAPI from 'richer'

const dom = {
  addToCartForm: document.getElementById('AddToCartForm'),
}

const AddToCart = () => {
  const form = dom.addToCartForm

  form.addEventListener('submit', (e) => {
    e.preventDefault()
    form.classList.remove('is-added')
    form.classList.add('is-adding')

    RicherAPI.addItemFromForm(e.target, itemAddedCallback, itemErrorCallback)
  })

  const itemAddedCallback = () => {
    RicherAPI.getCart(cartUpdateCallback)
  }

  const itemErrorCallback = (XMLHttpRequest, textStatus) => {
    console.log('error family')
  }
}

const cartUpdateCallback = (cart) => {
  updateCount(cart)
  buildCart(cart)
  RicherAPI.onCartUpdate(cart)
}

MIT License