Giter VIP home page Giter VIP logo

product-context's Introduction

๐Ÿ“ข Use this project, contribute to it or open issues to help evolve it using Store Discussion.

Product Context

All Contributors

The Product Context app is designed to provide essential product data to all child blocks within your store theme.

Configuration

  1. Add the product-context app as a dependency to your store theme's manifest.json file:

      "dependencies": {
    +   "vtex.product-context": "0.x"
      }
  2. Install the TypeScript types exported by the app by running the following command:

    vtex setup

Now, you're ready to import components and hooks from the app. Check this example component that displays the product name using data stored in the nearest ProductContext:

// Notice that this is TypeScript, and this code should be in a .tsx file
import React, { FC } from 'react'
import { useProduct } from 'vtex.product-context'

const MyComponent: FC = () => {
  const productContextValue = useProduct();

  return (
    <Fragment>
      {productContextValue?.product?.productName}
    </Fragment>
  )
}

export default MyComponent

Hooks

useProduct

The useProduct hook allows your app to retrieve data from the nearest ProductContext relative to its caller. Expect an object with the structure below as the return value:

interface ProductContextState {
  selectedItem?: Item | null
  product: MaybeProduct
  selectedQuantity: number
  skuSelector: {
    selectedImageVariationSKU: string | null
    isVisible: boolean
    areAllVariationsSelected: boolean
  }
  buyButton: BuyButtonContextState
  assemblyOptions: {
    items: Record<GroupId, AssemblyOptionItem[]>
    inputValues: Record<GroupId, InputValues>
    areGroupsValid: Record<GroupId, boolean>
  }
}

Note that if the hook is called outside a ProductContextProvider, the return value may be undefined or an empty object.

useProductDispatch

The useProductDispatch hook provides a dispatch function to manipulate the nearest ProductContext. The function supports the following actions:

  • SELECT_IMAGE_VARIATION: Sets the value for the skuSelector.selectedImageVariationSKU property.
  • SET_QUANTITY: Sets the value for the selectedQuantity property.
  • SKU_SELECTOR_SET_VARIATIONS_SELECTED: Sets the value for the skuSelector.areAllVariationsSelected property.
  • SET_BUY_BUTTON_CLICKED: Sets the value for the buyButton.clicked property.
  • SKU_SELECTOR_SET_IS_VISIBLE: Sets the value for the skuSelector.isVisible property.
  • SET_SELECTED_ITEM: Sets the value for the selectedItem property.
  • SET_ASSEMBLY_OPTIONS: Sets the value for the assemblyOptions property.
  • SET_PRODUCT: Sets the value for the product property.
  • SET_LOADING_ITEM: Sets the value of whether a selected item is loading.

Contributors โœจ

Thanks goes to these wonderful people:


Sahan Jayawardana

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

product-context's People

Contributors

allcontributors[bot] avatar augustolazaro avatar brunoabreu avatar carolinamenezes avatar guerreirobeatriz avatar hiagolcm avatar iaronaraujo avatar igorbrasileiro avatar jeymisson avatar jgfidelis avatar kaisermann avatar klynger avatar klzns avatar lbebber avatar sahanljc avatar victorhmp avatar vtex-io-ci-cd[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

product-context's Issues

Dispatch is not a function

Hey guys, I'm trying to make use of product context dispatch, but I'm always getting "dispatch is not a function".

I am importing these forms
import {useProductDispatch} from 'vtex.product-context / ProductDispatchContext'
or
import {useProduct, useProductDispatch} from 'vtex.product-context'

and I'm declaring the hook this way
const dispatch = useProductDispatch ()

however in all ways I am always receiving feedback
"dispatch is not a function"

Context aways returns 10000 products available in Shelfs

Describe the bug
When you tries to access product context in shelfs, it aways returns 10000 products available in seller comertial offer.
To Reproduce
Use product context in any shelf, see the error.

Expected behavior
Show the returned SKU real quantity left in stock.

ProductContextState does not have product/selectedItem property

Describe the bug
A clear and concise description of what the bug is.

The declaration const { product, selectedItem } = useProduct() cause a TS error that indicate "Property 'selectedItem' does not exist on type 'Partial"

To Reproduce
Steps to reproduce the behavior:

  1. import Product Context
  2. use useProduct Hook with destructuring syntax

Expected behavior

no type error

Desktop environment:

System:
OS: Windows 10 10.0.19042
CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz Memory: 2.13 GB / 15.79 GB
Binaries:
Node: 14.15.5 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.19041.964.0), Chromium (90.0.818.66) Internet Explorer: 11.0.19041.1

Is product-description-short missing?

Hi, I'm trying to show the product short descrtiption on a PDP but I've found out it's not part of this product-contex. So my question is how is it supposed to be used natively?

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.