ProSchedulerSign in

Scheduler · Loading events

Load events from a remote API

The same pattern against a real route: each page fetches its range, and a newer page aborts the request it replaces.

Live demo

Click next several times quickly — only the last page loads

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 RemoteApiDemo() {
  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 /api/demo-events…" : `${events.length} events from the API`}</p>
      <ProScheduler
        data={events}
        resources={studioResources}
        view={{ scheduler: { type: "week", startTime: "07:00", endTime: "20:00" } }}
        onPageChange={async ({ firstDay, lastDay }) => {
          request.current?.abort() // a newer page wins
          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,
            })
            // ISO strings go straight into `data`: start/end accept them.
            setEvents(await res.json())
            setLoading(false)
          } catch {
            // aborted by a newer page
          }
        }}
        height={560}
        {...handlers}
      />
    </div>
  )
}

About this demo

The same pattern against a real route: each page fetches its range, and a newer page aborts the request it replaces.

Things to try

  • Click next several times quickly — only the last page loads

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

Use ProScheduler in your project