@amityco/ulta-ui-kit
TypeScript icon, indicating that this package has built-in type declarations

1.1.3 • Public • Published

Ulta-Social-Cloud-Ui-Kit for Web

Getting started

The AmityUIKit provides a comprehensive user interface toolkit to quickly integrate Amity Social features into new or existing websites. This toolkit simplifies the incorporation of social interactions within your projects through an iframe-based integration.

Prerequisites

Before you begin, ensure you have the following installed:

• Node.js (>= 20.x) • React.js (>= 18.x)

Installation

With npm: npm install --save @amityco/ulta-ui-kit

With yarn: yarn add @amityco/ulta-ui-kit

Basic Usage

import { AmityUiKitProvider, AmityUiKitSocial } from "@amityco/ulta-ui-kit";

import "@amityco/ulta-ui-kit/dist/index.css";

export default function App() {
  // Retrieve userId from query parameters
  const urlParams = new URLSearchParams(window.location.search);
  const userId = urlParams.get("userId");
  const apiKey = "<your API key here>";
  const apiRegion = "<your API region here>";

  return (
    <div style={{ width: '100dvw', height: '100dvh' }}>
          <AmityUiKitProvider
            key={userId}
            apiKey={apiKey}
            userId={userId}
            apiRegion={apiRegion}
            ultaConfig={{
              newsCommunityId: 'newsCommunityId', // To configure the news communityId used to navigate from `Ulta Beuty News` banner
              faqCommunityId: 'faqCommunityId', // To configure the FAQs communityId used to navigate from `FAQs & Best Practices` banner
              defaultTab: 'explore', // To configure the default tab. The available default tabs are 'newfeeds', and 'explore'
              termsAndConditionsUrl: 'https://www.ulta.com/foo', // To configure the terms and conditions link on the nickname dialog
              privacyAndPolicyUrl: 'https://www.ulta.com/bar', // To configure the privacy and policy link on the nickname dialog
            }}
          >
            <AmityUiKitSocial />
          </AmityUiKitProvider>
    </div>
  );
}

Iframe Post Message Integration

In React, the component’s state dictates the behavior and rendering of the component. When the state changes, React automatically triggers a re-render of the component, updating the DOM where necessary. In your application, the userId is stored in the component’s state using the useState hook. This state determines the URL of the iframe where the Amity UIKit is loaded. When the userId state changes, React recognizes this change and re-renders the component, including the iframe, with the updated URL.

When an anonymous user click on any buttons to interact within community, the UIKit will emit a ‘anonymousNeedsLogin’ postMessage event. Below is a code example demonstrating how to receive this postMessage event from the UIKit Iframe:

import React, { useState, useEffect } from 'react';

export default function App() {
  // `userId` is managed as a state variable and will be updated post-login
  const [userId, setUserId] = useState('public-xxxx');

  // Handle the postMessage event from the iframe
  useEffect(() => {
    const handleIframeMessage = event => {
      if (event.data === 'anonymousNeedsLogin') {
        // Prompt user to log in
        // Logic to display login dialog should be implemented here
      }

      if (event.data ==== 'uikitRendered') {
        // Logic to handle after uikit has been rendered
      }

      if (event.data ==== 'explorePageLoaded') {
        // Logic to handle after Explore page has been loaded
      }


      if (event.data ==== 'parentNeedsScrollToTop') {
        // `parentNeedsScrollToTop` will be posted from iFrame when the Nickname dialog is opened
        window.scrollTo(0,0);
      }

    };

    // Listen for messages from the iframe
    window.addEventListener('message', handleIframeMessage);

    // Cleanup the event listener on component unmount
    return () => {
      window.removeEventListener('message', handleIframeMessage);
    };
  }, []);

  // Function to handle setting a new `userId` after the user logs in
  const handleSetUserId = newValue => {
    setUserId(newValue);
  };

  return (
    <div>
      // Assume that this is Ulta login prompt
      <input
        type="text"
        id="name"
        placeholder="Enter new User ID"
        onChange={(e) => handleSetUserId(e.target.value)}
      />
      <button onClick={() => handleSetUserId(document.getElementById('name').value)}>
        Set User ID
      </button>
      <iframe
        style={{ width: '100%', height: '80vh' }}
        title="Communities"
        src={`https://urlToUikit.com?userId=${userId}`}
      />
    </div>
  );
}

Anonymous User Flow

Authenticated Users

  1. Access and Authentication:
  • Users enter Ulta Web and navigate to the community tab.
  • Ulta Web contacts Ulta Authentication Service to verify if the user is logged in.
  1. Token Handling:
  • If authenticated, Ulta Web requests an Amity authentication token from Ulta Server.
  • Ulta Server forwards this request to Amity Server, which returns the token.
  1. UIKit Initialization:
  • Ulta Web instructs Ulta Iframe to initialize Amity UIKit using the authenticated Ulta User ID and token.
  • Amity UIKit is then presented to the user, allowing continued interaction under their Ulta ID.

Anonymous Users

  1. Access as Anonymous:
  • If not logged in, the same token request is made to handle session management.
  • Amity UIKit is initialized as ‘public’, allowing limited interaction.
  1. Exceeded Interaction Limit:
  • If anonymous interactions exceed limits, Amity UIKit informs Ulta Iframe, triggering a notification to Ulta Web.
  • A login dialogue is presented to the user.
  1. Login Decision:
  • If the user logs in, Ulta Authentication Service authenticates the user, and the token process repeats to reinitialize Amity UIKit with the Ulta User ID.

Diagram Sept 4 from Mermaid Chart (1)

Readme

Keywords

none

Package Sidebar

Install

npm i @amityco/ulta-ui-kit

Weekly Downloads

25

Version

1.1.3

License

LGPL-2.1-only

Unpacked Size

7.5 MB

Total Files

11

Last publish

Collaborators

  • ekoapp
  • touchaponk
  • pakkawat