{"id":16234426,"url":"https://github.com/alexanderprod/shopify-react-context","last_synced_at":"2025-04-08T07:56:51.752Z","repository":{"id":57702410,"uuid":"511067924","full_name":"AlexanderProd/shopify-react-context","owner":"AlexanderProd","description":null,"archived":false,"fork":false,"pushed_at":"2022-07-06T12:31:42.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T17:51:22.912Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexanderProd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-06T09:14:53.000Z","updated_at":"2022-07-06T10:02:45.000Z","dependencies_parsed_at":"2022-09-26T21:11:18.374Z","dependency_job_id":null,"html_url":"https://github.com/AlexanderProd/shopify-react-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderProd%2Fshopify-react-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderProd%2Fshopify-react-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderProd%2Fshopify-react-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexanderProd%2Fshopify-react-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexanderProd","download_url":"https://codeload.github.com/AlexanderProd/shopify-react-context/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247801170,"owners_count":20998331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T13:15:58.956Z","updated_at":"2025-04-08T07:56:51.700Z","avatar_url":"https://github.com/AlexanderProd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Installation\n\nAdd the package to your project via `npm install shopify-react-context` or `yarn add shopify-react-context`.\n\n## Usage\n\nImport your the `StoreContextProvider` component and wrap your components with it. For example a Layout component in `GatsbyJS`:\n\n```JS\nimport React from 'react'\nimport { StoreContextProvider }from 'shopify-react-context'\n\nconst Layout = ({ children }) =\u003e {\n  return (\n    \u003cStoreContextProvider\u003e\n      {children}\n    \u003c/StoreContextProvider\u003e\n  )\n}\n\nexport default Layout\n```\n\nNow to access the Context use the React `useContext` hook and pass the `StoreContext` to it.\n\n```JS\nconst {\n  addVariantToCart,\n  store: { client, adding },\n} = useContext(StoreContext)\n```\n\n## Example\n\nThis is an example for a ProductForm component using the Context exposed by `shopify-react-context`.\n\n```JS\nimport React, { useState, useContext, useEffect, useCallback } from 'react'\nimport { StoreContext } from 'shopify-react-context'\nimport isEqual from 'lodash/isEqual'\nimport find from 'lodash/find'\n\nconst ProductForm = ({ product }) =\u003e {\n  const {\n    options,\n    variants,\n    variants: [initialVariant],\n    priceRange: { minVariantPrice },\n  } = product\n  const [variant, setVariant] = useState({ ...initialVariant })\n  const [quantity, setQuantity] = useState(1)\n  const {\n    addVariantToCart,\n    store: { client, adding },\n  } = useContext(StoreContext)\n\n  const productVariant =\n    client.product.helpers.variantForOptions(product, variant) || variant\n  const [available, setAvailable] = useState(productVariant.availableForSale)\n\n  const checkAvailability = useCallback(\n    productId =\u003e {\n      client.product.fetch(productId).then(fetchedProduct =\u003e {\n        // this checks the currently selected variant for availability\n        const result = fetchedProduct.variants.filter(\n          variant =\u003e variant.id === productVariant.shopifyId\n        )\n        setAvailable(\n          result[0]?.available ?? fetchedProduct.variants[0].available\n        )\n      })\n    },\n    [client.product, productVariant.shopifyId]\n  )\n\n  useEffect(() =\u003e {\n    checkAvailability(product.shopifyId)\n  }, [productVariant, checkAvailability, product.shopifyId])\n\n  const handleQuantityChange = ({ target }) =\u003e {\n    setQuantity(target.value)\n  }\n\n  const handleOptionChange = (optionIndex, { target }) =\u003e {\n    const { value } = target\n    const currentOptions = [...variant.selectedOptions]\n\n    currentOptions[optionIndex] = {\n      ...currentOptions[optionIndex],\n      value,\n    }\n\n    const selectedVariant = find(variants, ({ selectedOptions }) =\u003e\n      isEqual(currentOptions, selectedOptions)\n    )\n\n    setVariant({ ...selectedVariant })\n  }\n\n  const handleAddToCart = () =\u003e {\n    addVariantToCart(productVariant.shopifyId, quantity)\n  }\n\n  const checkDisabled = (name, value) =\u003e {\n    const match = find(variants, {\n      selectedOptions: [\n        {\n          name: name,\n          value: value,\n        },\n      ],\n    })\n    if (match === undefined) return true\n    if (match.availableForSale === true) return false\n    return true\n  }\n\n  const price = Intl.NumberFormat(undefined, {\n    currency: minVariantPrice.currencyCode,\n    minimumFractionDigits: 2,\n    style: 'currency',\n  }).format(variant.price)\n\n  return (\n    \u003c\u003e\n      \u003ch3\u003e{price}\u003c/h3\u003e\n      {options.map(({ id, name, values }, index) =\u003e (\n        \u003cReact.Fragment key={id}\u003e\n          \u003clabel htmlFor={name}\u003e{name} \u003c/label\u003e\n          \u003cselect\n            name={name}\n            key={id}\n            onBlur={event =\u003e handleOptionChange(index, event)}\n          \u003e\n            {values.map(value =\u003e (\n              \u003coption\n                value={value}\n                key={`${name}-${value}`}\n                disabled={checkDisabled(name, value)}\n              \u003e\n                {value}\n              \u003c/option\u003e\n            ))}\n          \u003c/select\u003e\n          \u003cbr /\u003e\n        \u003c/React.Fragment\u003e\n      ))}\n      \u003clabel htmlFor=\"quantity\"\u003eQuantity \u003c/label\u003e\n      \u003cinput\n        type=\"number\"\n        id=\"quantity\"\n        name=\"quantity\"\n        min=\"1\"\n        step=\"1\"\n        onChange={handleQuantityChange}\n        value={quantity}\n      /\u003e\n      \u003cbr /\u003e\n      \u003cbutton\n        type=\"submit\"\n        disabled={!available || adding}\n        onClick={handleAddToCart}\n      \u003e\n        Add to Cart\n      \u003c/button\u003e\n      {!available \u0026\u0026 \u003cp\u003eThis Product is out of Stock!\u003c/p\u003e}\n    \u003c/\u003e\n  )\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexanderprod%2Fshopify-react-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexanderprod%2Fshopify-react-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexanderprod%2Fshopify-react-context/lists"}