⚠️
Important! We’ve moved to V3! This documentation is for V2 only and will be updated in May.
Developer Hub
🔮 For applications
SDK
Watch Hooks
useConditionsState

useConditionsState

The useConditionsState hook is used for maintain updated states for a conditions.

Conditions have a state field that indicates their current state. For instance, ConditionState.Created signifies the condition is available for betting, while ConditionState.Stopped indicates it has temporarily stopped accepting bets. This hook monitors this state and returns an isLocked flag, indicating whether placing a bet on this condition is currently possible or not.

Usage

⚠️

Before utilizing useConditionsState, it is essential to initialize the FeedSocketProvider and ConditionUpdatesProvider:

  import { ChainProvider, FeedSocketProvider, ConditionUpdatesProvider, useWatchers } from '@azuro-org/sdk'
 
  function Providers(props: { children: React.ReactNode }) {
    const { children } = props
 
    return (
      <ChainProvider>
        <FeedSocketProvider>
          <ConditionUpdatesProvider>
            {children}
          </ConditionUpdatesProvider>
        </FeedSocketProvider>
      </ChainProvider>
    )
  }
ℹ️

Example of usage: Betslip.

We've stored user's selections and need to make sure that conditions in a state to accept bets. The useConditionsState hook offers a method to react to changes in the condition states.

import { useConditionsState } from '@azuro-org/sdk'
import { ConditionState } from '@azuro-org/tookit'
import { useMemo } from 'react'
 
const items = [{...}]
 
const { data: states, isFetching: isStatesFetching } = useConditionsState({
  conditionIds: items.map(({ conditionId }) => conditionId),
})
 
const isConditionsInActiveState = useMemo(() => {
  return Object.values(states).every(state => state === ConditionState.Active)
}, [ states ])

Props

{
  conditionIds: string[]
  initialStates?: Record<string, ConditionState> // key is conditionId
}
ℹ️

The states is an object where the key combination corresponds to conditionId:

states[conditionId]
ℹ️

The initialStates is optional. If it's not provided, the useConditionsState hook will automatically retrieve the initial value.

enum ConditionState {
  Active = 'Active',
  Canceled = 'Canceled',
  Removed = 'Removed',
  Resolved = 'Resolved',
  Stopped = 'Stopped'
}

Return Value

{
  data: Record<string, ConditionState> // key is conditionId
  isFetching // flag indicates initial states fetching
}