7.0.0 • Published 5 years ago

@crystallize/react-basket v7.0.0

Weekly downloads
19
License
MIT
Repository
github
Last release
5 years ago

alt text

react-basket

Shopping basket compatible with the Crystallize headless ecommerce GraphQL service.

Crystallize is a GraphQL based PIM powered by (FAST APIs)https://crystallize.com/product/graphql-commerce-api

Usage

yarn add @crystallize/react-basket

In your app root

import { BasketProvider } from '@crystallize/react-basket';

const basketOptions = {
  validateEndpoint: "/api/basket/validate",
  freeShippingMinimumPurchaseAmount: 600,
  defaultShipping: yourDefaultShipping,
  onEmpty: () => console.log('emptied basket'),
  onAddToBasket: item => console.log('Add to basket', item),
  onRemoveFromBasket: item => console.log('Remove from basket', item),
  t: (...args) => {
    // Translator function. Same API as i18next
    return MyOwnTranslatorService.apply(null, args);
  }
};

export default () => (
  <BasketProvider {...basketOptions}>
    <YourApp />
  </BasketProvider>
);

Consume it with BasketContext (>= React 16.6)

import { BasketContext } from '@crystallize/react-basket';

export default class MyClass extends React.Component {

  static contextType = BasketContext;

  render () {
    const { state, actions } = this.context;

    return (
      <button onClick={() => actions.addItem({ sku: 'product-sku-1', price: 99, quantity: 1 })}>
        {state.totalQuantity
          ? `Your basket (${state.totalQuantity} items, ${state.totalPrice},-)`
          : 'Your basket is empty'}
      </button>
    );
  }
}

Consume it with BasketConsumer

import { BasketConsumer } from '@crystallize/react-basket';

export default () => (
  <BasketConsumer>
    {({ state, actions }) => (
      <button onClick={() => actions.addItem({ sku: 'product-sku-1', price: 99, quantity: 1 })}>
        {state.totalQuantity
          ? `Your basket (${state.totalQuantity} items, ${state.totalPrice},-)`
          : 'Your basket is empty'}
      </button>
    )}
  </BasketConsumer>
);

All exports

  • BasketProvider
  • BasketContext
  • BasketConsumer
  • TinyBasket
  • Coupon
  • parseBasketItem
  • createBasketItem

State

The state object contains:

  • items (array of the product items in the basket)
  • totalPrice (the total price of the basket items)
  • totalPriceMinusDiscount (total price minus discount (if applied))
  • totalToPay (total price - discount + shipping)
  • totalQuantity (the total items including quantity)
  • freeShipping (if free shipping has been granted)
  • remainingUntilFreeShippingApplies (the minimum amount required for free shipping)
  • metadata (additional metadata that you can set)

Actions

  • empty (empty the basket)
  • addItem (add an item)
  • setItems (set all the basket items in one go)
  • removeItem (remove an item)
  • incrementQuantityItem (increment the quantity for a given item)
  • decrementQuantityItem (decrement the quantity for a given item)
  • parseBasketItem (takes a product and a variant model and returns a valid basket item)
  • setValidating (set the validating flag (found in state))
  • setValidatingNewCoupon (set validatingNewCoupon flag (found in state))
  • setCoupon (set the coupon code)
  • setDiscount (set the discount item)
  • setShipping (set the current shipping item)
  • setMetadata (set your metadata)
  • animateItem (jiggles the item in the TinyBasket)
  • onReady (calls then given function when the basket is loaded and ready)

TinyBasket

The tiny basket accepts the following props

  • hideCoupon
  • hideRemainingUntilFreeShipping
  • ItemCmp (The component to use when rendering each basket item)
  • itemImageSizes (The sizes attr for each item image. Ex: '(min-width: 900px) 1000px, 500px')
7.0.0

5 years ago

6.0.0

5 years ago

6.0.0-beta.3

5 years ago

6.0.0-beta.2

5 years ago

6.0.0-beta.1

5 years ago

6.0.0-beta.0

5 years ago

5.6.0

5 years ago

5.5.0

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.2.0

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.4

6 years ago

3.2.3

6 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.4.4

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.4

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.21.2

6 years ago

1.21.1

6 years ago

1.21.0

6 years ago

1.20.0

6 years ago

1.19.2

6 years ago

1.19.1

6 years ago

1.19.0

6 years ago

1.18.2

6 years ago

1.18.1

6 years ago

1.18.0

6 years ago

1.17.3

6 years ago

1.17.2

6 years ago

1.17.1

6 years ago

1.17.0

6 years ago

1.16.1

6 years ago

1.16.0

6 years ago

1.15.1

6 years ago

1.15.0

6 years ago

1.14.0

6 years ago

1.13.1

6 years ago

1.13.0

6 years ago

1.12.0

6 years ago

1.11.0

6 years ago

1.10.1

6 years ago

1.10.0

6 years ago

1.9.9

6 years ago

1.9.8

6 years ago

1.9.7

6 years ago

1.9.6

6 years ago

1.9.5

6 years ago

1.9.4

6 years ago

1.9.3

6 years ago

1.9.2

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.6

6 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago