Skip to main content

useBeeStore

This document provides an overview and guide for the useBeeStore.ts file, utilizing the Zustand library to manage bee-related states and actions. This store allows interactions such as creating, deleting, and managing active and inactive states of bees.

Table of Contents

Imports

import { create } from "zustand";
import { IBee } from "@shared/interfaces";
import { pollingInterval } from "../consts";
  • create: A function from the Zustand library to create the store.
  • IBee: Interface defining the structure of a bee object.
  • pollingInterval: A constant defining the interval for polling operations.

State Types

The state of our store is defined as BeeStoreState:

PropertyTypeDescription
initializingbooleanIndicates if bees are being initialized.
beesIBee[]List of all bees.
activeBeesIBee[]List of active bees.
inActiveBeesIBee[]List of inactive bees.

Action Types

The actions of our store are defined as BeeStoreAction:

ActionTypeDescription
createBee(bee: IBee) => voidPermanently creates a new bee.
deleteBee(id: number) => voidPermanently deletes a bee by ID.
initializeBees() => Promise<void>Fetches and initializes all bees.
setActive(id: number) => Promise<void>Sets a specific bee as active.
setInActive(id: number) => Promise<void>Sets a specific bee as inactive.
startPolling() => voidBegins the polling process for bees.
stopPolling() => voidStops the polling process for bees.

Store Initialization

The store is created using the create function from the Zustand library:

eort const useBeeStore = create<BeeStoreState & BeeStoreAction>((set) => {
// Logic for store initialization and actions
});

State and Actions

State

The initial state includes:

  • initializing: Set to true, indicating the store is in the process of initialization.
  • bees, activeBees, inActiveBees: Arrays initialized as empty, to later be populated with bees.

Actions

  • createBee: Adds a new bee to the store.
  • deleteBee: Removes a bee from the store by its ID.
  • initializeBees: Fetches all bees and updates the state accordingly.
  • setActive: Updates the state to mark a specific bee as active.
  • setInActive: Updates the state to mark a specific bee as inactive.
  • startPolling: Continuously updates the bee data at a predefined interval.
  • stopPolling: Stops the ongoing polling operation.

Utility Functions

  • updateBeesState: Fetches all bees and updates active/inactive states. Used within actions to keep the store current.

Example Usage

useBeeStore.getState().createBee({ id: 1, name: "Buzz", isActive: true });
useBeeStore.getState().setActive(1);

Notes

  • Ensure that the IBee interface is correctly defined in @shared/interfaces for seamless integration.
  • Adjust the pollingInterval within ../consts as required for your application needs.

This concludes the documentation for useBeeStore.ts. For further inquiries or contributions, please refer to the project's contribution guidelines.