Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 5x 8x 8x 47x 8x 47x 4x 3x 18x 13x 13x 12x 12x 5x 2x 2x 2x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 5x 2x 2x 1x 1x 5x | import axios from 'axios'
import type { EventConfig } from './types'
export class WhiteLabelStore {
private static instance: WhiteLabelStore | null = null
private globalConfig: EventConfig | null = null
private eventConfig: EventConfig | null = null
private constructor() {}
public static getInstance(): WhiteLabelStore {
if (!WhiteLabelStore.instance) {
WhiteLabelStore.instance = new WhiteLabelStore()
}
return WhiteLabelStore.instance
}
public static reset(): void {
// Resetting singleton for testing
WhiteLabelStore.instance = null
}
public setGlobalConfig(config: EventConfig | null): void {
this.globalConfig = config
}
public setEventConfig(config: EventConfig | null): void {
this.eventConfig = config
}
public getGlobalConfig(): EventConfig | null {
return this.globalConfig
}
public getEventConfig(): EventConfig | null {
return this.eventConfig
}
}
/**
* Helper accessors
*/
export const getGlobalConfig = () => WhiteLabelStore.getInstance().getGlobalConfig()
export const getEventConfig = () => WhiteLabelStore.getInstance().getEventConfig()
/**
* Fetches global configuration and updates the store.
*/
export const fetchGlobalConfig = async (apiUrl: string): Promise<EventConfig | null> => {
try {
const store = WhiteLabelStore.getInstance()
const { data } = await axios.get(`${apiUrl}/settings/global`, {
headers: { Accept: 'application/json' },
})
store.setGlobalConfig(data)
return data
} catch (error) {
console.error('Failed to fetch global config:', error)
}
return null
}
/**
* Fetches event configuration and updates the store.
*/
export const fetchEventConfig = async (
apiUrl: string,
slug: string,
): Promise<EventConfig | null> => {
try {
const store = WhiteLabelStore.getInstance()
const { data } = await axios.get(`${apiUrl}/events/${slug}/settings`, {
headers: { Accept: 'application/json' },
})
store.setEventConfig(data)
return data
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 404) {
console.warn(`Event config for "${slug}" not found.`)
WhiteLabelStore.getInstance().setEventConfig(null)
} else {
console.error('Failed to fetch event config:', error)
}
}
return null
}
/**
* Initializes the white-labeling library by fetching configuration from the backend.
* This function now expects specific endpoints for global and event settings.
*/
export const initWhiteLabeling = async (apiUrl: string, slug?: string): Promise<void> => {
await fetchGlobalConfig(apiUrl)
if (slug) {
await fetchEventConfig(apiUrl, slug)
} else {
WhiteLabelStore.getInstance().setEventConfig(null)
}
}
/**
* Resets the white-labeling store for testing purposes.
*/
export const resetWhiteLabelStore = () => WhiteLabelStore.reset()
|