Code Monkey home page Code Monkey logo

cart's Introduction

Cart

Headless cart management library

npm i cart

All Contributors: 2 Codecov Test Coverage Contributor Covenant License: MIT Style: Prettier TypeScript: Strict

โš ๏ธ Expect some breaking changes, Use at your own risk

๐Ÿ›’ Demo

๐Ÿ“ฆ Requirements

  • React or Nextjs Project โš›๏ธ

โœจ Installation

  • Install using the below command (with your package manager of choice)
# npm
npm install cart --save

# yarn
yarn add cart

#pnpm
pnpm add cart

# bun
bun install cart

๐Ÿ’ก Usage Example

import React from "react";
import { useCart } from "cart";

const item = {
  productId: "123",
  name: "Product 1",
  quantity: 1,
  price: 10,
};

function Cart() {
  const {
    addToCart,
    cartItems,
    clearCart,
    decreaseItem,
    toggleCart,
    isCartOpen,
  } = useCart();

  return (
    <div>
      <p>{isCartOpen ? "Open" : "Closed"}</p>
      <button onClick={() => toggleCart()}>Toggle</button>
      <button onClick={() => addToCart(item)}>Add</button>

      <button onClick={() => clearCart()}>Clear</button>
      <button onClick={() => decreaseItem("123", 1)}>Decrease</button>

      <p>{JSON.stringify(cartItems)}</p>
    </div>
  );
}

export default Cart;

๐Ÿ’ก SSR Example

import { useCart, withSSR } from "cart";
import React from "react";

const item = {
  productId: "123",
  name: "Product 1",
  quantity: 1,
  price: 10,
};

function MyCart() {
  const cart = withSSR(useCart, (state) => state);

  const handleToggle = () => {
    cart?.toggleCart();
  };

  const itemadd = () => {
    cart?.addToCart(item);
  };

  return (
    <div>
      <p>{cart?.isCartOpen ? "Open" : "Closed"}</p>
      <button onClick={() => handleToggle()}>Toggle</button>
      <button onClick={() => itemadd()}>Add</button>

      <button onClick={() => cart?.clearCart()}>Clear</button>
      <button onClick={() => cart?.decreaseItem("123", 1)}>Decrease</button>

      <p>{JSON.stringify(cart?.cartItems)}</p>
    </div>
  );
}

export default MyCart;

API Reference

Name Type Default Value Description Example
isCartOpen boolean false Indicates whether the cart is open or not. isCartOpen ? "Yes" : "No"
toggleCart function - Toggles the visibility of the shopping cart. toggleCart();
openCart function - Sets the cart open state to true. openCart();
closeCart function - Sets the cart open state to false. closeCart();
cartItems array [] An array of items in the cart. cartItems.map((item) => ( <div key={item.productId}> <p>{item.name}</p> <p>Quantity: {item.quantity}</p> <p>Price: ${item.price}</p> </div> ))
addToCart function - Adds an item to the shopping cart or updates its quantity if already in the cart. addToCart({ productId: 'product1', name: 'Product 1', quantity: 2, price: 20 });
decreaseItem function - Decreases the quantity of an item in the shopping cart or removes it if the quantity becomes zero. decreaseItem('product1', 1);
removeFromCart function - Removes an item from the shopping cart. removeFromCart('product1');
clearCart function - Clears all items from the shopping cart. clearCart();

๐Ÿ™ Credits

Huge thanks to Peter Krumins for the package name cart. Please checkout Browserling - Online cross-browser testing platform.

(Btw, This is not a sponsored message, Just my way of saying thank you)

Contributors

Josh Goldberg โœจ
Josh Goldberg โœจ

๐Ÿ”ง
MC Naveen
MC Naveen

๐Ÿ’ป ๐Ÿ–‹ ๐Ÿ“– ๐Ÿค” ๐Ÿš‡ ๐Ÿšง ๐Ÿ“† ๐Ÿ”ง

๐Ÿ’š Message

I hope you find this useful. If you have any questions, please create an issue.


๐Ÿ’™ This package is based on @JoshuaKGoldberg's create-typescript-app.

cart's People

Contributors

dependabot[bot] avatar mcnaveen avatar

Stargazers

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

Watchers

 avatar

cart's Issues

๐Ÿš€ Feature: Support React Native AsyncStorage

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Overview

Cart partially works on React Native. But there is some issue.

[zustand persist middleware] Unable to update item 'cart', the given storage is currently unavailable.

Right now, Our code doesn't know if it's running on the web or in a React Native app.

So that we can use @react-native-async-storage/async-storage to make sure our code works with AsyncStorage in React Native.

Refer: pmndrs/zustand#394 (comment)

Additional Info

No response

๐Ÿ› Bug: Wrong CartItems type

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

Getting wrong type / autocomplete suggestions for CartItems

// my type def
interface ViewListing { id: string; user_id: number; listing_category_id: string; listing_sub_category_id?: any; service_offering: string; description: string; listing_images: string[]; minimum_offer: string; slug: string; status: string; size: string; vehicle_id: number; country_id: string; listing_type: string; created_at: string; updated_at: string; }

Actual

// default type def
interface CartItems {
cartQuantity?: number;
imagesrc?: string;
inStock?: boolean;
name: string;
price?: number;
productId: number | string | undefined;
quantity: number;
}

Additional Info

Running on:

Nextjs app router
Cart v1.1.1

How can i overwrite or extend the default types for CartItems? the default defaults do not match the items i am saving to cart

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.