Skip to content
KajayDocs
CreatorTypeScript SDKReact

Load and save definitions

Connect the controlled Creator definition to storage owned by your application.

Load a definition

Fetch and validate a survey in your application, then set it as the controlled value. The Creator does not fetch documents or choose a storage format for you.

Open a survey from your backend
const [definition, setDefinition] =
  useState<SurveyDefinition>(initialDefinition);

async function openSurvey(id: string): Promise<void> {
  const response = await fetch('/api/surveys/' + id);
  const next = (await response.json()) as SurveyDefinition;
  setDefinition(next);
}

<SurveyCreator
  value={definition}
  onChange={setDefinition}
/>

Save a definition

Pass a save function that returns true when persistence succeeds and false when it fails. It may be asynchronous. Without a saver, the default assembly does not show a save button.

Save through a host-owned endpoint
import type { SurveyDefinition } from '@kajay/core';
import { SurveyCreator } from '@kajay/creator-react';

async function saveDefinition(
  definition: SurveyDefinition,
): Promise<boolean> {
  const response = await fetch('/api/surveys/customer-feedback', {
    method: 'PUT',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify(definition),
  });

  return response.ok;
}

<SurveyCreator
  value={definition}
  onChange={setDefinition}
  save={saveDefinition}
  isAutoSave
/>

The default save control announces saving, saved, and failed states. A rejected promise counts as a failed save; logging and retry policy remain your application’s responsibility.

Autosave behavior

Set isAutoSave to request a save after each Creator edit. Kajay does not add a timer or debounce. When changes arrive during an in-flight save, it keeps only the latest definition and runs one more save when the current request finishes.

If your backend needs a delay or batch window, wrap the saver in your own scheduling policy. That keeps timing, cost, and cancellation decisions beside the backend they affect.

Incoming changes and conflicts

Changing value to a definition that differs from the Creator’s most recent output opens that document through the same edit path and preserves undo. Kajay does not merge concurrent server revisions. Resolve revision conflicts before sending the winning definition back as value.