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

@padhariyavishal/react-quote-cart

v1.0.5

Published

Add to quote product functionality with fetch quote cart items, update quote cart items, remove quote cart items.

Downloads

12

Readme

react-quote-cart

react-quote-cart is a simple and flexible React context provider for managing a quote cart in your application. It provides hooks and components to add, remove, update, and get products in a quote cart.

Installation

Install the package via npm:

npm i @padhariyavishal/react-quote-cart

Usage

Setting Up the Provider

Wrap your application with the QuoteCartProvider to provide the quote cart context to your components.

import React from 'react';
import ReactDOM from 'react-dom';
import { QuoteCartProvider } from 'react-quote-cart';
import App from './App';

ReactDOM.render(
  <QuoteCartProvider>
    <App />
  </QuoteCartProvider>,
  document.getElementById('root')
);

Adding Items to the Cart

Use the useQuoteCartActions hook to add items to the quote cart.

// src/App.tsx
import React from 'react';
import { useQuoteCartActions, QuoteCart } from 'react-quote-cart';

const App = () => {
  const { addToQuoteCart } = useQuoteCartActions();

  const sampleProduct = { id: 1, name: 'Product 1', uri: 'product-1', imageUrl:'https://placehold.co/150', price: 100 };
  const anotherProduct = { id: 2, name: 'Product 2', uri: 'product-2',imageUrl:'https://placehold.co/150', price: 200 };

  return (
    <div>
      <h1>Quote Cart Example</h1>
      <button onClick={() => addToQuoteCart(sampleProduct)}>Add Product 1</button>
      <button onClick={() => addToQuoteCart(anotherProduct, 3)}>Add Product 2 (3 units)</button>
      <QuoteCart />
    </div>
  );
};

export default App;

Updating Item Quantities in the Cart

Use the updateQuoteCart function from the useQuoteCartActions hook to update item quantities directly in the cart.

Displaying Cart Items

// src/components/QuoteCart.tsx
import React from 'react';
import { useQuoteCart } from 'react-quote-cart';
import { useQuoteCartActions } from 'react-quote-cart';

const QuoteCart = () => {
  const { state } = useQuoteCart();
  const { removeFromCart, updateQuoteCart } = useQuoteCartActions();

  const handleQuantityChange = (id: number, quantity: number) => {
    if (quantity >= 0) {
      updateQuoteCart(id, quantity);
    }
  };

  return (
    <div>
      <h2>Quote Cart</h2>
      <ul>
        {state.items.map(item => (
          <li key={item.id}>
            {item.name} -
            <input
              type="number"
              value={item.quantity}
              onChange={(e) => handleQuantityChange(item.id, parseInt(e.target.value))}
              min="0"
            />
            x ${item.price}
            <button onClick={() => removeFromCart(item.id)}>Remove</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default QuoteCart;

API

QuoteCartProvider

Wrap your application with QuoteCartProvider to enable the quote cart context.

import { QuoteCartProvider } from 'react-quote-cart';

<QuoteCartProvider>
  {children}
</QuoteCartProvider>

useQuoteCart

Hook to access the quote cart state.

import { useQuoteCart } from 'react-quote-cart';

const { state } = useQuoteCart();

useQuoteCartActions

Hook to access actions to modify the quote cart.

import { useQuoteCartActions } from 'react-quote-cart';

const { addToQuoteCart, removeFromCart, updateQuoteCart, emptyQuoteCart } = useQuoteCartActions();

addToQuoteCart(product: Product, quantity: number = 1)

Adds a product to the quote cart or increases the quantity if it already exists.

- product: The product object to add.

- quantity: The quantity of the product to add. Defaults to 1.

removeFromCart(productId: number)

Removes a product from the quote cart based on its ID.

- productId: The ID of the product to remove.

updateQuoteCart(productId: number, quantity: number)

Updates the quantity of a product in the quote cart.

- productId: The ID of the product to update.

- quantity: The new quantity of the product.

emptyQuoteCart

Remove all items from the cart.

Example

Here's a full example of how to use react-quote-cart in your application:

// src/App.tsx
import React from 'react';
import { QuoteCartProvider, useQuoteCartActions, QuoteCart } from 'react-quote-cart';

const App = () => {
  const { addToQuoteCart } = useQuoteCartActions();

  const sampleProduct = { id: 1, name: 'Product 1',uri: 'product-1', imageUrl:'https://placehold.co/150', price: 100 };
  const anotherProduct = { id: 2, name: 'Product 2',uri: 'product-2', imageUrl:'https://placehold.co/150', price: 200 };

  return (
    <QuoteCartProvider>
      <div>
        <h1>Quote Cart Example</h1>
        <button onClick={() => addToQuoteCart(sampleProduct)}>Add Product 1</button>
        <button onClick={() => addToQuoteCart(anotherProduct, 3)}>Add Product 2 (3 units)</button>
        <QuoteCart />
      </div>
    </QuoteCartProvider>
  );
};

export default App;

Displaying Cart Items

// src/components/QuoteCart.tsx
import React from 'react';
import { useQuoteCart } from 'react-quote-cart';
import { useQuoteCartActions } from 'react-quote-cart';

const QuoteCart = () => {
  const { state } = useQuoteCart();
  const { removeFromCart, updateQuoteCart, emptyQuoteCart } = useQuoteCartActions();

  const handleQuantityChange = (id: number, quantity: number) => {
    if (quantity >= 0) {
      updateQuoteCart(id, quantity);
    }
  };

  return (
    <div>
      <h2>Quote Cart</h2>
       <button onClick={() => emptyQuoteCart()}>Empty Cart</button>
      <ul>
        {state.items.map(item => (
          <li key={item.id}>
            {item.name} -
            <input
              type="number"
              value={item.quantity}
              onChange={(e) => handleQuantityChange(item.id, parseInt(e.target.value))}
              min="0"
            />
            x ${item.price}
            <button onClick={() => removeFromCart(item.id)}>Remove</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default QuoteCart;