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

preference-center

v1.0.1

Published

The **Preferences Widget** allows users to select and manage their product preferences, such as categories, sizes, colors, and brands. This widget can be easily embedded into any HTML page and is customizable via JavaScript. The widget fetches data from a

Downloads

7

Readme


Preferences Widget

The Preferences Widget allows users to select and manage their product preferences, such as categories, sizes, colors, and brands. This widget can be easily embedded into any HTML page and is customizable via JavaScript. The widget fetches data from a backend API, displays user preferences, and allows modifications to be saved back to the server.

Table of Contents

Installation

To use the Preferences Widget, include the JavaScript file in your project.

Manual Integration

  1. Add the widget script to your HTML file.
  2. Include any required backend credentials, such as tenant ID, access token, and customer ID.
<script src="https://cdn.jsdelivr.net/npm/preference-center/script.js"></script>

Usage

To render the widget, simply call the PreferencesWidget.init() method after the DOM is ready. You will need to provide the following parameters:

  • containerId: The ID of the HTML element where the widget will be rendered.
  • tenantId: Your tenant ID from the backend system.
  • accessToken: The access token for API authentication.
  • customerId: The customer ID for whom preferences are being managed.

HTML Structure

Make sure to have a container element where the widget will be rendered.

<div id="preferences-widget-container"></div>

Example Usage

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Preferences Widget</title>
  <script src="https://cdn.jsdelivr.net/npm/preference-center/script.js"></script>
</head>
<body>
  <div id="preferences-widget-container"></div>

  <script>
    const tenantId = "your-tenant-id";
    const accessToken = "your-access-token";
    const customerId = "your-customer-id";

    PreferencesWidget.init("preferences-widget-container", tenantId, accessToken, customerId);
  </script>
</body>
</html>

JavaScript Example (Using npm package)

import PreferencesWidget from 'preferences-widget';

document.addEventListener('DOMContentLoaded', () => {
  const tenantId = "your-tenant-id";
  const accessToken = "your-access-token";
  const customerId = "your-customer-id";

  PreferencesWidget.init("preferences-widget-container", tenantId, accessToken, customerId);
});

Configuration

The widget requires the following parameters for initialization:

  • containerId (string): The ID of the HTML container element where the widget will be rendered.
  • tenantId (string): The tenant ID used to identify the backend service.
  • accessToken (string): The access token required for authenticating API requests.
  • customerId (string): The customer ID representing the user for whom preferences are being retrieved and updated.

API Configuration

Make sure the API endpoints and authentication tokens are properly configured. The widget interacts with the following APIs:

  • GET preferences: Retrieves the current preferences for the customer.
  • POST/PUT preferences: Updates or creates preferences for the customer.

Methods

init(containerId, tenantId, accessToken, customerId)

This method initializes the widget and renders it inside the specified container. It also handles API calls to fetch and update user preferences.

Parameters:

  • containerId (string): The ID of the container element in the DOM where the widget will be inserted.
  • tenantId (string): The unique tenant ID for backend interaction.
  • accessToken (string): A valid access token for API authentication.
  • customerId (string): The ID of the customer whose preferences will be managed.

Example:

PreferencesWidget.init("preferences-widget-container", "tenantId123", "accessTokenABC", "customerIdXYZ");

Widget UI Elements

The widget consists of the following components:

  • Ranges: Buttons for switching between different product ranges (e.g., male/female).
  • Categories and Sizes: Spoilers displaying available categories (e.g., shirts, shoes) and corresponding sizes (e.g., S, M, L).
  • Colors: A section for selecting product colors.
  • Brands: A section for selecting preferred brands.
  • Save Button: A button for saving the selected preferences.

Examples

Here is a basic example to demonstrate how to initialize and use the Preferences Widget on a webpage:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Preferences Widget</title>
  <script src="preferences-widget.js"></script>
</head>
<body>
  <div id="preferences-widget-container"></div>

  <script>
    const tenantId = "your-tenant-id";
    const accessToken = "your-access-token";
    const customerId = "your-customer-id";

    PreferencesWidget.init("preferences-widget-container", tenantId, accessToken, customerId);
  </script>
</body>
</html>

In this example, the widget is rendered in the #preferences-widget-container div, and the necessary data is fetched and managed via the backend API.