tribe-api-wrapper
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

tribe-api-wrapper

npm npm NPM GitHub issues

This package is designed to offer an accessible and efficient interface for the Tribe Leaderboard API. It encompasses a React component for visualizing leaderboards and a set of functions that were intended to facilitate various interactions with the API endpoints.

Project To-Do List

Test live now in storybook

  • [ ] Update storybook build out
  • [ ] Fix small errors/consistency in props/options
  • [ ] Fix docs to be super efficient and explanatory
  • [ ] Build more components

Table of Contents

Installation

Install the library using npm:

npm install tribe-api-wrapper

Usage

The library provides a set of functions that can be used directly to interact with the Tribe API. It also includes pre-made React components for common tasks. You can choose to use the functions directly or utilize the components based on your needs.

Importing Functions and Components

import {
  getLeaderboard,
  getClientList,
  getPublicClientUserList,
  Leaderboard,
  ClientList,
  UserList,
  ClientCardLG,
  ClientCardSM
} from 'tribe-api-wrapper';

Examples

Using Functions Directly

Fetching List of Clients

const clients = await getClientList();

Fetching Leaderboard Data

const data = await getLeaderboard('example-client-id', { timePeriod: 'week', trial: true, badgeFilter: false });

Fetching Public Client User List

const users = await getPublicClientUserList('example-client-id', { timePeriod: 'all', badgeFilter: false });

Examples

Using the getLeaderboard Function

Note:

The client parameter is required and can be a specific client ID or the special value "all" to get data for all clients.

TypeScript

import { LeaderboardResponse, getLeaderboard } from 'tribe-api-wrapper';
import { useState, useEffect } from 'react';

export default function Home() {
  const [leaderboardData, setLeaderboardData] = useState<LeaderboardResponse | null>(null);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    async function fetchData() {
      // To fetch leaderboard data for all clients, you can use 'all' as the client parameter:
      const data = await getLeaderboard('all', { timePeriod: 'week', trial: true, badgeFilter: false });
      if (data instanceof Error) {
        setError(data.message);
      } else {
        setLeaderboardData(data as LeaderboardResponse);
      }
    }
    fetchData();
  }, []);

  return (
    <div>
      <h1>Leaderboard</h1>
      {error ? (
        <div>Error: {error}</div>
      ) : leaderboardData ? (
        leaderboardData.data.map((user) => (
          <div key={user.username}>
            Username: {user.username} <br />
            Twitter Points: {user.twitter_points} <br />
            Content Points: {user.content_points} <br />
            Total Points: {user.total_points} <br />
            {user.has_badge && `Badge: ${user.badge_icon}`} <br />
            <hr />
          </div>
        ))
      ) : (
        <div>Loading...</div>
      )}
    </div>
  );
}

JavaScript

import { getLeaderboard } from 'tribe-api-wrapper';
import { useState, useEffect } from 'react';

export default function Home() {
  const [leaderboardData, setLeaderboardData] = useState(null);
  const [error, setError] = useState(null);

  useEffect(() => {
    async function fetchData() {
      const data = await getLeaderboard('example-client-id', { timePeriod: 'week', trial: true, badgeFilter: false });
      if (data instanceof Error) {
        setError(data.message);
      } else {
        setLeaderboardData(data);
      }
    }
    fetchData();
  }, []);

  return (
    <div>
      <h1>Leaderboard</h1>
      {error ? (
        <div>Error: {error}</div>
      ) : leaderboardData ? (
      leaderboardData.data.map((user) => (
        <div key={user.username}>
          Username: {user.username} <br />
          Twitter Points: {user.twitter_points} <br />
          Content Points: {user.content_points} <br />
          Total Points: {user.total_points} <br />
          {user.has_badge && `Badge: ${user.badge_icon}`} <br />
          <hr />
        </div>
      ))
      ) : (
        <div>Loading...</div>
      )}
    </div>
  );
}

Using the Leaderboard Component

TypeScript

import { Leaderboard } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <div>
      <h1>Leaderboard</h1>
        <Leaderboard
          client="example-client-id"
          timePeriod="week"
          trial={true}
          badgeFilter={false}
          limit={10}
          className="custom-leaderboard"
          errorClassName="custom-error"
          loadingClassName="custom-loading"
          tableClassName="custom-table"
          headerClassName="custom-header"
          rowClassName="custom-row"
          badgeClassName="custom-badge"
          titleClassName="custom-title"
          textClassName="custom-text"
          badge_icon="path/to/badge/icon.png"
          style={{ border: '1px solid #ccc' }}
        />
    </div>
  );
}

JavaScript

import { Leaderboard } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <div>
      <h1>Leaderboard</h1>
        <Leaderboard
          client="example-client-id"
          timePeriod="week"
          trial={true}
          badgeFilter={false}
          limit={10}
          className="custom-leaderboard"
          errorClassName="custom-error"
          loadingClassName="custom-loading"
          tableClassName="custom-table"
          headerClassName="custom-header"
          rowClassName="custom-row"
          badgeClassName="custom-badge"
          titleClassName="custom-title"
          textClassName="custom-text"
          badge_icon="path/to/badge/icon.png"
          style={{ border: '1px solid #ccc' }}
        />
    </div>
  );
}

Using the UserList Component

TypeScript

import { UserList } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <UserList
      client="example-client-id"
      containerClassName="custom-container"
      userClassName="custom-user"
      textClassName="custom-text"
      style={{ padding: '10px' }}
    />
  );
}

JavaScript

import { UserList } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <UserList
      client="example-client-id"
      containerClassName="custom-container"
      userClassName="custom-user"
      textClassName="custom-text"
      style={{ padding: '10px' }}
    />
  );
}

Using the ClientList Component

TypeScript

import { ClientList } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientList
      className="custom-list"
      clientClassName="custom-client"
      avatarClassName="custom-avatar"
      backgroundClassName="custom-background"
      textClassName="custom-text"
      style={{ margin: '20px' }}
    />
  );
}

JavaScript

import { ClientList } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientList
      className="custom-list"
      clientClassName="custom-client"
      avatarClassName="custom-avatar"
      backgroundClassName="custom-background"
      textClassName="custom-text"
      style={{ margin: '20px' }}
    />
  );
}

Using the ClientCardLG Component

TypeScript

import { ClientCardLG } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientCardLG
      client="example-client-id"
      cardClassName="custom-card-lg"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      nameClassName="custom-name"
      style={{ boxShadow: '0 0 10px rgba(0,0,0,0.1)' }}
    />
  );
}

JavaScript

import { ClientCardLG } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientCardLG
      client="example-client-id"
      cardClassName="custom-card-lg"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      nameClassName="custom-name"
      style={{ boxShadow: '0 0 10px rgba(0,0,0,0.1)' }}
    />
  );
}

Using the ClientCardSM Component

TypeScript

import { ClientCardSM } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientCardSM
      client="example-client-id"
      cardClassName="custom-card-sm"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      nameClassName="custom-name"
      style={{ boxShadow: '0 0 5px rgba(0,0,0,0.1)' }}
    />
  );
}

JavaScript

import { ClientCardSM } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientCardSM
      client="example-client-id"
      cardClassName="custom-card-sm"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      nameClassName="custom-name"
      style={{ boxShadow: '0 0 5px rgba(0,0,0,0.1)' }}
    />
  );
}

Using the ClientProfile Component

TypeScript

import { ClientProfile } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientProfile
      client="example-client-id"
      containerClassName="custom-container"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      leaderboardClassName="custom-leaderboard"
      style={{ boxShadow: '0 0 5px rgba(0,0,0,0.1)' }}
    />
  );
}

JavaScript

import { ClientProfile } from 'tribe-api-wrapper';

export default function Home() {
  return (
    <ClientProfile
      client="example-client-id"
      containerClassName="custom-container"
      bannerClassName="custom-banner"
      avatarClassName="custom-avatar"
      leaderboardClassName="custom-leaderboard"
      style={{ boxShadow: '0 0 5px rgba(0,0,0,0.1)' }}
    />
  );
}

API Docs

Functions API Docs

Function Name Description Parameters / Options
getLeaderboard(client: string, ...) Fetches the leaderboard data for the given client. - client: (Required)
- timePeriod: Options 'week', 'month', 'all'. Default: 'all' (Optional)
- trial: Default: true (Optional)
- badgeFilter: Default: false (Optional)
- limit: Default: 20 (Optional)
getClientList(): Promise<...> Fetches the list of all clients. None
getPublicClientUserList(client,...) Fetches the public client user list for the given client. - client: (Required)
- timePeriod: Options 'week', 'month', 'all'. Default: 'all' (Optional)
- badgeFilter: Default: false (Optional)

Component API Docs

Component Name Description Props
Leaderboard A React component that displays a leaderboard. - client: The client ID. Defaults to "all" if not provided. If the client prop is omitted, the component will display the leaderboard for all clients. To display the leaderboard for a specific client, you must provide the client ID. (Optional)
- timePeriod: Options 'week', 'month', 'all'. Default: 'all' (Optional)
- trial: Default: true (Optional)
- badgeFilter: Default: false (Optional)
- limit: Default: 20 (Optional)
- className, errorClassName, loadingClassName, tableClassName, headerClassName, rowClassName, badgeClassName, titleClassName, textClassName (Optional)
- badge_icon: String representing the badge icon, can be a URL to an image (e.g., SVG, PNG, GIF), a local image path, or a text string. (Optional)
- style: Custom inline styles (Optional)
UserList Renders a grid of users with customizable styles. - client: The client ID. (Required)
- containerClassName, userClassName, textClassName: CSS class names. (Optional).
- style: Inline styles for the main container. (Optional).
ClientList Renders a list of clients with all provided properties. - className, clientClassName, avatarClassName, backgroundClassName, textClassName: CSS class names. (Optional).
- style: Inline styles for the main container. (Optional).
ClientCardLG Renders a larger profile card for a client. - client: Client ID to fetch client data. (Required)
- cardClassName, bannerClassName, avatarClassName, nameClassName: CSS class names. (Optional).
- style: Inline styles for the card container. (Optional).
ClientCardSM Renders a smaller profile card for a client, including the banner. - client: Client ID to fetch client data. (Required)
- cardClassName, bannerClassName, avatarClassName, nameClassName: CSS class names. (Optional).
- style: Inline styles for the card container. (Optional).
ClientProfile Renders client profile and a leaderboard. - client: Client ID to fetch client data. (Required)
- containerClassName: CSS class for the main container. (Optional)
- bannerClassName: CSS class for the banner. (Optional)
- avatarClassName: CSS class for the avatar. (Optional)
- leaderboardClassName: CSS class for the leaderboard container. (Optional)
- style: Inline styles for the main container. (Optional).

Types

Refer to types.ts for the detailed type definitions.

Contributing

Contributions are welcome! Please follow the contributing guidelines for more information.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Disclaimer

Please note that this library was developed independently and is not associated with or endorsed by Tribe. It is intended for responsible and ethical use and is not designed to violate or circumvent Tribe's terms of service.

The library is provided "as-is," and users are encouraged to review Tribe's terms of service and ensure compliance with all applicable regulations. Use of this library is at your own risk, and the authors make no warranties or representations regarding its legality, reliability, or fitness for a particular purpose.

Package Sidebar

Install

npm i tribe-api-wrapper

Weekly Downloads

3

Version

1.3.2

License

MIT

Unpacked Size

123 kB

Total Files

39

Last publish

Collaborators

  • bankkroll