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

@billgangcom/frontend-lib

v4.2.0

Published

## Overview

Downloads

443

Readme

@billgangcom/frontend-lib

Overview

@billgangcom/frontend-lib is a comprehensive library designed to streamline the integration and interaction with the Billgang e-commerce platform. This library provides an assortment of hooks, components, and utility functions to simplify tasks such as handling shopping carts, interacting with customers, managing reCaptcha, and fetching data related to products, orders, settings, etc.

Installation

To install @billgangcom/frontend-lib, run:

npm install @billgangcom/frontend-lib

Features

  • BillgangProvider: Context provider for initializing the Billgang environment!
  • CustomerDashboard: Pre-built dashboard component that integrates various customer-related functionalities.
  • ReCaptchaProvider & useReCaptcha: Components and hooks for integrating Google reCaptcha.
  • useCart: Hook for managing cart operations like adding/removing products, applying coupons, etc.
  • Methods for fetching data: Utility methods for fetching settings, products, orders, announcements, etc.

Usage

Initial Setup

To get started, wrap your top-level component with the BillgangProvider. This ensures that the Billgang context is available throughout your application.

import { BillgangProvider } from '@billgangcom/frontend-lib'

const shopDomain = 'my-shop-domain'
const shopId = 'my-shop-id'
const shopPassword = 'password'

function App() {
	return (
		<BillgangProvider shopDomain={shopDomain} shopId={shopId} shopPassword={shopPassword}>
			{/* Your application components go here */}
		</BillgangProvider>
	)
}
export default App

Customer Dashboard

You can use the pre-built CustomerDashboard component to quickly set up a customer interface:

import { CustomerDashboard } from '@billgangcom/frontend-lib'

function App() {
	return (
		<BillgangProvider shopDomain={'my-shop-domain'} shopId={'my-shop-id'} shopPassword={''}>
			<CustomerDashboard />
		</BillgangProvider>
	)
}
export default App

ReCaptcha Integration

To integrate Google reCaptcha in your forms or components:

  1. Wrap your component tree with the ReCaptchaProvider.
  2. Use the useReCaptcha hook to interact with reCaptcha (e.g., executing the captcha challenge).
import { ReCaptchaProvider, useReCaptcha } from '@billgangcom/frontend-lib'

const MyComponent = () => {
	const { executeRecaptcha } = useReCaptcha()

	const handleSubmit = async () => {
		const recaptchaToken = await executeRecaptcha()
		// Use recaptchaToken for further actions
	}

	return <button onClick={handleSubmit}>Submit</button>
}

function App() {
	return (
		<BillgangProvider shopDomain={'my-shop-domain'} shopId={'my-shop-id'} shopPassword={''}>
			<ReCaptchaProvider>
				<MyComponent />
			</ReCaptchaProvider>
		</BillgangProvider>
	)
}
export default App

Cart Management

The useCart hook simplifies managing cart operations like adding/removing products, applying coupons, etc.

import { useCart } from '@billgangcom/frontend-lib'

const MyCartComponent = () => {
	const {
		cart,
		products,
		coupon,
		pending,
		addProductToCart,
		applyCoupon,
		getTotalAndDiscount,
		submitCart,
		reset,
		removeProduct,
		getPossibleGateways,
		setRecapcha,
		updateQuantityOfProduct,
		setGateway,
		setDiscordSocialConnectId,
		setCustomFields,
	} = useCart('[email protected]')

	const handleAddProduct = async () => {
		if (!pending && products.length) {
			try {
				addProductToCart({
					productId: 100000098,
					productVariantId: 100000125,
					quantity: 1,
				})
				await applyCoupon('MY_COUPON')
				await submitCart()
			} catch (error) {
				console.error(error)
			}
		}
	}

	return <button onClick={handleAddProduct}>Add Product</button>
}

Fetching Data

Use the provided utility methods to fetch various types of data:

import {
	fetchSettings,
	fetchProducts,
	fetchOrder,
	fetchAnnouncements,
} from '@billgangcom/frontend-lib/methods'

useEffect(() => {
	const fetchData = async () => {
		try {
			const [settings, products, order, announcements] = await Promise.all([
				fetchSettings(),
				fetchProducts(),
				fetchOrder('order_id'),
				fetchAnnouncements(),
			])
			console.log({ settings, products, order, announcements })
		} catch (error) {
			console.error(error)
		}
	}
	fetchData()
}, [])

API Reference

Components

BillgangProvider

Props:

  • shopDomain: The domain of the shop.
  • shopId: Unique identifier of the shop.
  • shopPassword?: Optional password for the shop.

CustomerDashboard

Pre-built dashboard component that integrates various customer-related functionalities.

ReCaptchaProvider

Wraps components to provide reCaptcha functionality.

Hooks

useCart(customerEmail: string)

Returns:

  • cart: Current state of the shopping cart.
  • products: Array of products available.
  • coupon: Applied coupon details.
  • pending: Boolean indicating if there are any pending operations.
  • getProducts(ids?): Function to fetch products, optionally filtered by IDs.
  • applyCoupon(coupon): Function to apply a coupon to the cart.
  • addProductToCart(cartItem): Function to add a product item to the cart.
  • getTotalAndDiscount(): Function to get total price and discount details.
  • submitCart(): Function to submit the current cart as an order.
  • reset(): Function to reset the cart.
  • removeProduct(partOrder): Function to remove a product from the cart.
  • getPossibleGateways(): Function to get available payment gateways for the current cart items.
  • setRecapcha(recaptcha): Function to set reCaptcha token for the cart.
  • updateQuantityOfProduct(cartItem): Function to update the quantity of a specific product in the cart.
  • setGateway(gateway): Function to set the gateway for the cart.
  • setPaymentMethod(paymentMethod): Function to set the payment method for the cart.
  • setDiscordSocialConnectId(discordSocialConnectId): Function to set Discord Social Connect ID for the cart.
  • setCustomFields(customFields): Function to set custom fields for the cart submission.

useReCaptcha()

Returns:

  • An object with method executeRecaptcha.

Methods

Utility methods for fetching data provided by @billgangcom/frontend-lib/methods:

  • fetchAnnouncements()

    • Fetches announcements from the store.
  • fetchGatewaysDetail(gateways)

    • Fetches details of specified payment gateways.
  • fetchListings(ids?)

    • Fetches product listings, optionally filtered by IDs.
  • fetchMetadata(body)

    • Fetches metadata based on specified parameters.
  • fetchOrder(orderId)

    • Fetches order details by order ID.
  • fetchOrderWithToken(orderId, token)

    • Fetches order details by order ID and token.
  • fetchPrivacyPolicy()

    • Fetches privacy policy details from the store.
  • fetchProducts(ids?)

    • Fetches product details, optionally filtered by IDs.
  • fetchRefundPolicy()

    • Fetches refund policy details from the store.
  • fetchSettings()

    • Fetches store settings.
  • fetchTerms()

    • Fetches terms and conditions from the store.
  • postOrder(orderRequest)

    • Submits a new order with specified order details.
  • validateCoupon(coupon)

    • Validates a coupon for specified products and gateway.