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-brand-widget

v1.0.2

Published

```markdown # Preference Widget

Readme

Preference Widget

The Preference Widget allows users to manage their favorite brands by adding or removing brands from their preferences. The widget communicates with the preferences API to fetch, add, and remove brands, updating them.

Features

  • Fetch Preferences: Retrieve the user's current preferences, including favorite brands.
  • Toggle Brand: Add or remove a brand from all ranges simultaneously.
  • Real-Time Updates: Checkbox interface updates the list of favorite brands in real-time as users interact with the widget.

Installation

  1. Include the script in your HTML: Add the widget script to your project.

    <script src="https://cdn.jsdelivr.net/npm/preference-brand-widget/script.js"></script>
  2. Initialize the widget: Create an instance of PreferenceWidget and provide the required parameters: tenantId, customerId, and accessToken.

    const widget = new PreferenceWidget(
        "yourTenantId",
        "yourCustomerId",
        "yourAccessToken"
    );

Usage

HTML Setup

Add a section in your HTML where the checkboxes for brands will be rendered, and a list for displaying the current favorite brands.

<h1>Choose Your Favorite Brands</h1>
<div id="brand-checkboxes">
    <!-- Checkboxes will be dynamically populated here -->
</div>

<h2>Your Favorite Brands</h2>
<ul id="favorite-brands-list">
    <!-- Favorite brands will be dynamically populated here -->
</ul>

JavaScript Setup

  1. Initialize the widget: Fetch the user’s current preferences and display the brand checkboxes.

  2. Checkbox Interaction: When a checkbox is checked or unchecked, the widget will add or remove the brand from the user's preferences.

const allBrands = ["Samsung", "Apple", "Nike", "Adidas", "Sony", "LG", "New Balance"];

widget.init().then((favoriteBrands) => {
    populateCheckboxes(favoriteBrands);
    updateFavoriteBrandsList();
});

function populateCheckboxes(favoriteBrands) {
    const checkboxContainer = document.getElementById("brand-checkboxes");
    checkboxContainer.innerHTML = "";

    allBrands.forEach((brand) => {
        const checkbox = document.createElement("input");
        checkbox.type = "checkbox";
        checkbox.id = brand;
        checkbox.name = brand;
        checkbox.value = brand;

        if (favoriteBrands.includes(brand)) {
            checkbox.checked = true;
        }

        checkbox.addEventListener("change", function() {
            widget.addBrand(brand).then(() => updateFavoriteBrandsList());
        });

        const label = document.createElement("label");
        label.htmlFor = brand;
        label.appendChild(document.createTextNode(brand));

        const div = document.createElement("div");
        div.appendChild(checkbox);
        div.appendChild(label);
        checkboxContainer.appendChild(div);
    });
}

function updateFavoriteBrandsList() {
    const favoriteBrandsList = document.getElementById("favorite-brands-list");
    favoriteBrandsList.innerHTML = "";

    const favoriteBrands = widget.getFavoriteBrands();

    favoriteBrands.forEach((brand) => {
        const listItem = document.createElement("li");
        listItem.textContent = brand;
        favoriteBrandsList.appendChild(listItem);
    });
}

Methods

init()

Fetches the user’s current preferences and initializes the widget.

  • Returns: A promise that resolves with an array of the user's favorite brands.

addBrand(brand)

Adds or removes a brand from all ranges simultaneously based on whether the brand already exists.

  • Parameters:
    • brand (string): The name of the brand to toggle.
  • Returns: A promise that resolves once the preferences are updated.

getFavoriteBrands()

Retrieves the current list of favorite brands from the widget’s internal state.

  • Returns: An array of favorite brand names.

Example

const widget = new PreferenceWidget(
    "tenantId",
    "customerId",
    "accessToken"
);

// Initialize the widget and populate checkboxes
widget.init().then((favoriteBrands) => {
    populateCheckboxes(favoriteBrands);
    updateFavoriteBrandsList();
});

API Endpoints

The widget interacts with the following API endpoints:

  1. GET Preferences: Fetch the user's current preferences.

    GET /services/customerservice/api/v2/customer/preference?customerId={customerId}
  2. PUT Preferences: Update the user's preferences by adding or removing brands.

    PUT /services/customerservice/api/v2/customer/preference