Customization
The create form starts empty. The edit form starts with a saved item. Reuse the field definitions for both and handle loading, submission and validation separately so editing one field preserves the rest of the record.
The last piece is a create/edit workflow that reuses the same validators and API you built earlier.
mkdir -p apps/mercato/src/modules/inventory/backend/inventory/create
touch apps/mercato/src/modules/inventory/backend/inventory/create/page.tsx
touch apps/mercato/src/modules/inventory/backend/inventory/create/page.meta.ts
import { CrudForm } from '@open-mercato/ui/backend/CrudForm';
import type { CrudFormSchema } from '@open-mercato/ui/backend/CrudForm/types';
import { readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall';
import type { UpsertInventoryItemInput } from '../../data/validators';
const schema: CrudFormSchema<UpsertInventoryItemInput> = {
title: 'Create Inventory Item',
fields: [
{ id: 'sku', label: 'SKU', component: 'text', required: true },
{ id: 'name', label: 'Name', component: 'text', required: true },
{ id: 'quantity', label: 'Quantity', component: 'number', required: true, min: 0 },
{ id: 'location', label: 'Location', component: 'text' },
],
};
async function submit(data: UpsertInventoryItemInput) {
return readApiResultOrThrow('/api/items', {
method: 'POST',
body: JSON.stringify(data),
}, { errorMessage: 'Failed to create inventory item' });
}
const InventoryCreatePage = () => {
return <CrudForm schema={schema} onSubmit={submit} redirectTo="/backend/inventory/list" />;
};
export default InventoryCreatePage;
import type { PageMetadata } from '@open-mercato/shared/modules/registry';
export const metadata: PageMetadata = {
title: 'Add item',
group: 'Operations',
order: 21,
requireAuth: true,
requireFeatures: ['inventory.create'],
};
Create an edit route that fetches row data and submits via PATCH:
mkdir -p apps/mercato/src/modules/inventory/backend/inventory/[id]/edit
touch apps/mercato/src/modules/inventory/backend/inventory/[id]/edit/page.tsx
touch apps/mercato/src/modules/inventory/backend/inventory/[id]/edit/page.meta.ts
import { CrudForm } from '@open-mercato/ui/backend/CrudForm';
import type { CrudFormSchema } from '@open-mercato/ui/backend/CrudForm/types';
import { readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall';
import type { UpsertInventoryItemInput } from '../../../data/validators';
const schema: CrudFormSchema<UpsertInventoryItemInput> = {
title: 'Edit Inventory Item',
fields: [
{ id: 'sku', label: 'SKU', component: 'text', required: true },
{ id: 'name', label: 'Name', component: 'text', required: true },
{ id: 'quantity', label: 'Quantity', component: 'number', required: true, min: 0 },
{ id: 'location', label: 'Location', component: 'text' },
],
};
async function fetchItem(id: string) {
return readApiResultOrThrow(`/api/items/${id}`, undefined, { errorMessage: 'Inventory item not found' });
}
async function submit(id: string, data: UpsertInventoryItemInput) {
return readApiResultOrThrow(`/api/items/${id}`, {
method: 'PATCH',
body: JSON.stringify(data),
}, { errorMessage: 'Failed to update inventory item' });
}
type Props = { params: { id: string } };
const InventoryEditPage = async ({ params }: Props) => {
const item = await fetchItem(params.id);
return (
<CrudForm
schema={schema}
initialValues={item}
onSubmit={(data) => submit(params.id, data)}
redirectTo="/backend/inventory/list"
/>
);
};
export default InventoryEditPage;
import type { PageMetadata } from '@open-mercato/shared/modules/registry';
export const metadata: PageMetadata = {
title: 'Edit item',
group: 'Operations',
order: 22,
requireAuth: true,
requireFeatures: ['inventory.edit'],
};
Update the list page to surface "Create" and "Edit" buttons -- either via DataTable row actions or links in your JSX. Users with the correct features can now manage inventory end to end.
After adding the new pages, regenerate:
yarn generate
yarn mercato configs cache structural --all-tenants
That completes the tutorial sequence. You now have a fully working inventory module with:
index.ts, acl.ts, setup.ts)data/entities.ts)data/validators.ts)api/items/route.ts)backend/inventory/list/page.tsx)backend/inventory/create/page.tsx, backend/inventory/[id]/edit/page.tsx)Create a record without an optional field, reopen it and change only its name. Save and reload it once more. This catches a common edit-form mistake where omitted values in the form overwrite existing record data.
In the rest of the documentation you will dive deeper into the framework primitives that power these steps:
Previous: Custom fields at a glance · Next: List inventory items