Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@makaira/storefront-react

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@makaira/storefront-react

Contains the makaira react layer to connect makaira shop adapters to connect your storefront with

  • 1.6.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
71
increased by787.5%
Maintainers
2
Weekly downloads
 
Created
Source

@makaira/storefront-react

This package is a wrapper to bring a reactive shop adapter to your react storefront. This react wrapper is SSR compatible. It is completely build by the usage of react hooks.

The package will automatically handle loading data like the user, the cart or the wishlist. Also it handles modifications in the state change by listening to the events emitted by the shop adapter client.

This package gives you using hooks access to the loaded data and holds some common data like the total amount of products in the cart.

Installation

yarn install @makaira/storefront-react

or

npm i @makaira/storefront-react

Adding to your project

For simplicity we are here using the local shop adapter to demonstrate how to add the react wrapper to your storefront.

import { StorefrontShopAdapterLocal } from '@makaira/storefront-shop-adapter-local'
import { ShopProvider } from '@makaira/storefront-react'

const client = new StorefrontShopAdapterLocal()

function Index() {
  return (
    <ShopProvider client={shopClient}>
      <App />
    </ShopProvider>
  )
}

In addition if you are using typescript in your project and want to get the correct autosuggestion you have to create a new declaration file (e.g index.d.ts) with the following content:

import '@makaira/storefront-react'
import { StorefrontShopAdapterLocal } from '@makaira/storefront-shop-adapter-local'

declare module '@makaira/storefront-react' {
  interface StorefrontReactCustomClient {
    client: StorefrontShopAdapterLocal
  }
}

Props for the ShopProvider

PropertyRequired/OptionalDescriptionType
clientrequiredThe shop adapter client.StorefrontReactCustomClient['client']
bootstrapoptionalBy default the react wrapper loads the the main data that is required for most storefronts. These can be disabled or customized.object
- cartoptionalBy default the current cart will be loaded by calling client.cart.getCart({input:{}}). If you set this value to false loading is disabled. If your turn it back to true it will automatically loads the cart. If you want to customize the data that is loaded you can also pass a function that returns a Promise<MakairaResponse>. The dataproperty will then be written into the state.boolean | (() => Promise<MakairaResponse<StorefrontReactCustomTypes['cart'], any, Error> >)
- useroptionalBy default the current user will be loaded by calling client.user.getUser({input:{}}). If you set this value to false loading is disabled. If your turn it back to true it will automatically loads the user. If you want to customize the data that is loaded you can also pass a function that returns a Promise<MakairaResponse>. The dataproperty will then be written into the state.boolean | (() => Promise<MakairaResponse<StorefrontReactCustomTypes['user'], any, Error> >)
- wishlistoptionalBy default the current wishlist will be loaded by calling client.wishlist.getWishlist({input:{}}). If you set this value to false loading is disabled. If your turn it back to true it will automatically loads the wishlist. If you want to customize the data that is loaded you can also pass a function that returns a Promise<MakairaResponse>. The dataproperty will then be written into the state.boolean | (() => Promise<MakairaResponse<StorefrontReactCustomTypes['wishlist'], any, Error> >)

Usage with typescript

In the above section we already explained how to set the correct shop adapter. In addition to these you can also adjust the typescript definition of the in the state stored data.

By default you don't need to do so. But if you set a custom bootstrap loader this might by helpful.

To do so add to your declaration file the following content:

import '@makaira/storefront-react'
import { StorefrontShopAdapterLocal } from '@makaira/storefront-shop-adapter-local'

declare module '@makaira/storefront-react' {
  // only overwrite one specific data
  interface StorefrontReactCustomTypes {
    cart: { id: string }
  }

  // update overwrite specific data
  interface StorefrontReactCustomTypes {
    cart: { id: string }
    user: { id: string }
  }

  // or overwrite them all
  interface StorefrontReactCustomTypes {
    cart: { id: string }
    user: { id: string }
    wishlist: { id: string }
  }
}

Available Hooks

useShopClient

This hook can be used to access the shop adapter client passed to the ShopProvider.

function App() {
  const { client } = useShopClient()

  return <></>
}

Arguments

No Arguments to pass

Returned properties

PropertyDescriptionType
clientThe client passed to the ShopProvider.StorefrontReactCustomClient['client']

useShopCart

This hook can be used to access the current cart.

function App() {
  const { cart } = useShopCart()

  return <></>
}

Arguments

No Arguments to pass

Returned properties

PropertyDescriptionType
cartThe current cart.null | undefined | StorefrontReactCustomTypes['user']
productsInCartThe current amount of products in the cart. It counts the number of unique products but not their quantity in the cart. When in the cart product A and product B with each have a quantity set to 3 is, productsInCart will 2.number
quantityInCartThe current total amount of products with their quantity. When in the cart product A and product B with each have a quantity set to 3 is, quantityInCart will 6.number
totalPriceInCartThe current total price of all products in the cart including their quantities.number

useShopUser

This hook can be used to access the current user.

function App() {
  const { user } = useShopUser()

  return <></>
}

Arguments

No Arguments to pass

Returned properties

| Property | Description | Type | | -------- | ----------------- | ----- | --------- | ----------------------------------- | | user | The current user. | null | undefined | StorefrontReactCustomTypes['user'] |

useShopWishlist

This hook can be used to access the current wishlist.

function App() {
  const { wishlist } = useShopWishlist()

  return <></>
}

Arguments

No Arguments to pass

Returned properties

PropertyDescriptionType
wishlistThe current wishlist.null | undefined | StorefrontReactCustomTypes['cart']
isProductInWishlistA function to check if a product is in the wishlist.(id: string) => MakairaResponse<boolean, undefined, Error>

useShopReviews

This hook can be used to reviews for a specific product

function App() {
  const { loading, error, data, raw } = useShopWishlist({
    product: { id: 'foo' },
  })

  return <></>
}

Arguments

PropertyRequired/OptionalDescriptionType
productrequiredThe product to get the reviews from.object
- idrequiredThe id of the product to get reviews from.string
paginationoptionalTo allow pagination you can here set the pagination informationobject
- offsetoptionalThe pagination offset.number
- limitoptionalThe pagination limit.number
refetchOnReviewCreatedoptionalIndicator if the review list should be refetched if the ReviewCreateEvent was emitted. Default is false.boolean

Returned properties

PropertyDescriptionType
loadingIndicator if the reviews are currently fetching or if the next pagination is loading.boolean
errorIndicator if an error occurred while loading the reviews.Error | undefined
dataThe data property returned by the client.reviews.getReviews() containing the reviews.Unified data response of client.reviews.getReviews()
rawThe raw property returned by the client.reviews.getReviews().Shop adapter specific raw response of client.reviews.getReviews()
refetchFunction to force a reload with the current product and pagination information() => void

Keywords

FAQs

Package last updated on 14 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc