2. Cratis Components: Register an author
Add an author from your library screen with CommandDialog, a Cratis Components form that runs an Arc command when the user confirms. You will supply the command’s required id before validation, then check that the registration succeeds and the author appears in the observable table.
Continue from List the authors, with src/Authors.tsx mounted inside the existing providers and the backend running. The table must already show a successful result, even if it is empty. Keep the provider and CSS setup from Getting started.
You also need src/Authors/RegisterAuthor.ts, the TypeScript command proxy generated by Arc’s first slice. If it is missing, regenerate the proxies using that chapter’s backend instructions. Import the generated RegisterAuthor class as a runtime value: CommandDialog creates the command from it. The librarian supplies name; your dialog supplies id.
Add an author
Section titled “Add an author”-
Create
src/AddAuthor.tsx.CommandDialogprovides the action footer and runs the generated command on confirmation. You choose the fields. The accessor(command) => command.namebinds the input to the generatedRegisterAuthor.nameproperty, so TypeScript checks that property access:src/AddAuthor.tsx import { useState } from 'react';import { Guid } from '@cratis/fundamentals';import { CommandDialog } from '@cratis/components/CommandDialog';import { InputTextField } from '@cratis/components/CommandForm';import { RegisterAuthor } from './Authors/RegisterAuthor'; // generated proxyexport const AddAuthor = () => {const [id] = useState(() => Guid.create());return (<CommandDialog<RegisterAuthor>command={RegisterAuthor}title='Add author'okLabel='Add'initialValues={{ id }}><InputTextField<RegisterAuthor>value={(command) => command.name}title='Name'placeholder='Sample Author'/></CommandDialog>);};RegisterAuthorrequires anid, but there is no input for it.useStatekeeps the generatedGuidstable while the librarian edits the name;initialValuesputs it on the command before validation. Without that value, Add stays disabled even after the name is filled in.Keep required values in
initialValues.onBeforeExecutecan transform values immediately before execution, but it runs only on submit. It cannot fill a missing required value that already prevents submission. -
Replace
src/Authors.tsxwith this screen. Arc’suseDialogreturns a wrapper component and a function that opens it. Render the wrapper alongside the table, and call the function from Add author:src/Authors.tsx import { useDialog } from '@cratis/arc.react/dialogs';import { Button, Page } from '@cratis/components/Common';import { Column, DataTableForObservableQuery } from '@cratis/components/DataTables';import { AllAuthors } from './Authors/Author';import { AddAuthor } from './AddAuthor';export const Authors = () => {const [AddAuthorDialog, showAddAuthor] = useDialog(AddAuthor);return (<Page title='Authors' showTitle><Button label='Add author' onClick={() => void showAddAuthor()} /><DataTableForObservableQuery query={AllAuthors} emptyMessage='No authors yet'><Column field='name' header='Name' sortable /></DataTableForObservableQuery><AddAuthorDialog /></Page>);}; -
Open the form. Keep
<Authors />mounted in your application and click Add author. With the required id already supplied, entering a valid name enables Add. Submit a recognizable synthetic name such as Sample Author. The dialog is busy whileRegisterAuthorexecutes and closes on success.
useDialog mounts AddAuthor only while open and unmounts it on close. The next registration therefore gets a new id, while edits within one registration keep the same one. CommandDialog closes through this dialog context after a successful command. Before submitting, Cancel, the close button, or Escape dismisses the form without executing it.
Check command success, then the query result
Section titled “Check command success, then the query result”Check the form and the table separately. Submitting a command writes through the backend; the table learns about that write from AllAuthors, not from the dialog.
- Open Add author, leave Name empty, and check that Add is disabled. Enter a name; confirm becomes available once client validation passes.
- Choose Cancel once and confirm that no author was added. Reopen the form and submit a new name. While execution is pending, the dialog is busy; on success it closes.
- Watch the table for the new name. It appears when
AllAuthorsdelivers the updated result. Register a second author through a new dialog and inspect the query data to confirm the two ids differ.
If submission is rejected, the dialog stays open. Read any field messages and inspect the command response; use CommandDialog’s result callbacks to handle failures that need application feedback. If submission succeeds but no row arrives, check the observable query connection and backend read-model update before trying again. A retry sends another command with a fresh id; it does not refresh the table.
The dialog can also close through cancellation, so closure alone is not proof of a successful registration. Check the command response when diagnosing a missing row. For a Chronicle-backed application, its projection must update the queried read model before the query can report the author. The table provides no fixed timing or write/read consistency guarantee.
Rename and remove are just more commands
Section titled “Rename and remove are just more commands”To edit an author, give a command both the target id and the values being changed. The Arc tutorial does not define rename or remove commands, so the following excerpt is an extension pattern, not another file to add in this chapter. It assumes you have defined and generated RenameAuthor, and that author holds the selected row. Its initialValues supplies both the id and the current name:
<CommandDialog<RenameAuthor> command={RenameAuthor} title='Rename author' okLabel='Save' initialValues={{ id: author.id, name: author.name }}> <InputTextField<RenameAuthor> value={(command) => command.name} title='Name' /></CommandDialog>A remove command with nothing but the id is often a confirmation rather than a form; pass initialFocus={DialogInitialFocus.Cancel} (from @cratis/components/Dialogs) so a stray Enter doesn’t confirm it. The table reflects these changes when its observable query delivers the updated result. In the next chapter you’ll get the selected row from DataPage and pass it to a real command from the Arc tutorial, AddBook.
What you built
Section titled “What you built”You can now register authors without leaving the list. Your dialog supplies the hidden id before validation, Arc executes the command, and the table renders the later query result without application refresh code.
Keep src/AddAuthor.tsx. Next, build the list-and-detail screen →: select an author, show their books beside the list, and pass that author’s id to an Add book dialog.