ProSchedulerSign in

Agenda · Loading events

Load events from a remote API

Each month's list fetches its own range from the API when it comes into view.

Live demo

Page to next month and watch the status

Fullscreen
import { useRef, useState } from "react"
import { ProScheduler, type SchedulerEvent } from "@andreisaac/pro-scheduler"
import { studioResources } from "./data/studio"
import { useEvents } from "./kit"

export function AgendaRemoteDemo() {
  const { events, setEvents, handlers } = useEvents<SchedulerEvent>([])
  const [loading, setLoading] = useState(true)
  const request = useRef<AbortController | null>(null)

  const day = (d: Date) =>
    `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`

  return (
    <div className="docs-demo__stack">
      <p className="demo-status">{loading ? "Fetching…" : `${events.length} events loaded for this page`}</p>
      <ProScheduler
        data={events}
        resources={studioResources}
        view={{ agenda: { type: "month" } }}
        onPageChange={async ({ firstDay, lastDay }) => {
          request.current?.abort()
          const controller = new AbortController()
          request.current = controller
          setLoading(true)
          try {
            const res = await fetch(`/api/demo-events?from=${day(firstDay)}&to=${day(lastDay)}`, {
              signal: controller.signal,
            })
            setEvents(await res.json())
            setLoading(false)
          } catch {
            // superseded by a newer page
          }
        }}
        height={520}
        {...handlers}
      />
    </div>
  )
}

About this demo

Each month's list fetches its own range from the API when it comes into view.

Things to try

  • Page to next month and watch the status

The props behind it are explained in Loading and saving events.

Use ProScheduler in your project