UNPKG

@ludiks/react

Version:

Complete React library for Ludiks gamification platform - includes SDK and ready-to-use components

61 lines (60 loc) 1.93 kB
import { Ludiks } from '@ludiks/sdk'; import { mockUserProfile, mockLeaderboardData } from '../mock/mockData'; let globalApiKey = null; let globalBaseUrl = null; let isMockMode = false; export function setLudiksConfig(apiKey, baseUrl) { globalApiKey = apiKey; globalBaseUrl = baseUrl || 'https://api.ludiks.io'; isMockMode = apiKey === 'mock' || apiKey === 'demo' || apiKey.startsWith('mock-'); } export function getLudiksConfig() { if (!globalApiKey) { throw new Error('Ludiks API key not configured. Please call setLudiksConfig() first.'); } return { apiKey: globalApiKey, baseUrl: globalBaseUrl, isMockMode, }; } export function enableMockMode() { isMockMode = true; } export function disableMockMode() { isMockMode = false; } export function isMock() { return isMockMode; } export async function fetchLeaderboard(period = 'all', limit = 10) { const config = getLudiksConfig(); if (config.isMockMode) { // Simulate API delay for realistic feel await new Promise(resolve => setTimeout(resolve, 300)); return mockLeaderboardData.slice(0, limit); } const response = await fetch(`${config.baseUrl}/api/leaderboard?period=${period}&limit=${limit}`, { headers: { 'Authorization': `Bearer ${config.apiKey}`, 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error(`Failed to fetch leaderboard: ${response.status}`); } return response.json(); } export async function getUserProfile(userId) { const config = getLudiksConfig(); if (config.isMockMode) { // Simulate API delay for realistic feel await new Promise(resolve => setTimeout(resolve, 500)); return mockUserProfile; } return Ludiks.getProfile({ apiKey: config.apiKey, userId, baseUrl: config.baseUrl, }); }