Working with Events

Overview

The shared package provides two primary event types:

  • Normal Events

  • Score-Based Events

While both types share the same core data structure and fetching mechanisms, they differ in quest structure. A third type (Collaborative Events) will be available soon via the SDK.

Fetching Events

1. Using the useCommunityEvents Hook

The simplest way to fetch events is using the useCommunityEvents hook:

"use client";
import { useCommunityEvents } from "@xborglabs/ui-shared/dist/client";

export function EventsContainer() {
  const { data: events, status } = useCommunityEvents(clientEnv.NEXT_PUBLIC_COMMUNITY_ID);

  if (status === "pending") {
    return <p>Loading events...</p>;
  }

  if (status === "error") {
    return <p>An error occurred...</p>;
  }

  return (
    <div>
      {events?.map((event) => (
        <div key={event.eventId}>
          <h3>{event.title}</h3>
          <p>Event Type: {event.eventType}</p>
        </div>
      ))}
    </div>
  );
}

Key Points:

  • Uses NEXT_PUBLIC_COMMUNITY_ID environment variable to fetch community-specific events

  • Returns data (event array) and status (pending, success, or error)

  • Requires handling of loading and error states

2. Using Headless Components

The shared package provides several headless components for event management:

Community Event List

Featured Event List

Event Details

Event Rank

3. Using Base API Functions

For advanced scenarios or server-side logic, use these base functions:

Working with Event Data

Rendering Event Details

Both event types share a core data structure. Here's how to render event-specific details:

API Methods

Core methods for event management:

Best Practices

Type Checking

Use TypeScript for type safety:

Implementation Guidelines

  1. Fallbacks: Always provide fallback UI for missing data

  2. Dynamic Rendering: Use conditional rendering for different event types

  3. Testing: Test all states (loading, error, success)

Advanced Topics

1. Extending Event Types

When adding new event types, ensure backward compatibility by:

  • Adding new eventType values

  • Updating component handling logic

2. Custom Hooks

Create custom hooks for specific use cases:

3. Backend Integration

  • Stay updated with SDK and Swagger documentation changes

  • Coordinate with backend teams on new error codes or fields

Last updated