Skip to content

Customer profile editor

An account team opens a customer’s profile from inside their CRM. The micro-app loads the customer when it starts, lets the person rename it, shows the billing email read-only because changing it needs a separate verification flow, and offers to close the account.

  • Watch the first second: Loading customer… shows while the startup fetch runs, then the profile appears.
  • Press Edit profile. The draft section appears with the current values. The billing email is read-only and says why.
  • Clear the name and press Save changes: the declaration’s own rule blocks it before the host is asked. Try a one-letter name for the minLength rule.
  • Rename the customer to ACME. The stand-in CRM refuses with a conflict, and its message appears under the name.
  • Save a valid name. The button shows Saving…, then the dark summary card changes — only after the host has confirmed.
  • Press Close this account. Mica asks the authored question first; cancel and nothing happens, confirm and the record is cleared.

customer is a crm.customer with a nested contact object. The document uses fetch, update and delete; update and delete act on the current record’s id, which is never an input.

{
"customer": {
"type": "crm.customer",
"cardinality": "one",
"identity": "id",
"fields": {
"id": {
"kind": "string",
"required": true
},
"displayName": {
"kind": "string",
"required": true
},
"plan": {
"kind": "string",
"required": true,
"enum": [
"starter",
"growth",
"scale"
]
},
"contact": {
"kind": "object",
"required": true,
"fields": {
"email": {
"kind": "string",
"required": true,
"format": "email"
}
}
}
},
"capabilities": {
"fetch": {
"inputs": {
"customerId": {
"kind": "string",
"required": true
}
}
},
"update": {
"inputs": {
"displayName": {
"kind": "string",
"required": true,
"minLength": 2,
"maxLength": 60
},
"email": {
"kind": "string",
"required": true,
"format": "email"
}
}
},
"delete": {}
},
"sample": {
"id": "cus_sample",
"displayName": "Sample Customer Ltd",
"plan": "growth",
"contact": {
"email": "billing@sample.example"
}
}
}
}

The startup list fetches the customer whose id is in document state, so a host could seed a different /customerId without changing the document:

{
"state": {
"customerId": "cus_2041",
"drafts": {
"customer": {}
}
},
"lifecycle": {
"startup": [
{
"action": "fetch",
"params": {
"object": "customer",
"inputs": {
"customerId": {
"$state": "/customerId"
}
}
}
}
]
}
}

The loading, error and loaded states are three elements with conditions on the fetch phase:

{
"loading": {
"type": "Text",
"props": {
"content": "Loading customer…",
"size": "md",
"tone": "muted",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/operations/fetch/phase",
"eq": "loading"
}
},
"load-error": {
"type": "Section",
"props": {
"eyebrow": null,
"title": "We couldn't load this customer",
"tone": "outlined",
"padding": "lg"
},
"children": [
"load-error-message",
"load-retry"
],
"visible": {
"$state": "/meta/objects/customer/operations/fetch/phase",
"eq": "error"
}
}
}

Edit profile copies the whole record into /drafts/customer with one setState. The editor section is visible once the draft has an id, the Inputs bind into the draft and show the update’s field errors in their error, and Save changes sends the draft’s values and is busy while the update is loading:

{
"summary-edit": {
"type": "Button",
"props": {
"label": "Edit profile",
"variant": "solid",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": {
"action": "setState",
"params": {
"statePath": "/drafts/customer",
"value": {
"$state": "/objects/customer/data"
}
}
}
},
"visible": {
"$state": "/meta/objects/customer/capabilities/update/allowed",
"eq": true
}
},
"name-input": {
"type": "Input",
"props": {
"label": "Display name",
"placeholder": "Company name",
"inputType": "text",
"required": true,
"helpText": "Shown on invoices and in the customer portal",
"value": {
"$bindState": "/drafts/customer/displayName"
},
"writable": {
"$state": "/meta/objects/customer/fields/displayName/writable"
},
"error": {
"$state": "/meta/objects/customer/operations/update/error/fieldErrors/displayName"
}
},
"children": []
},
"save": {
"type": "Button",
"props": {
"label": "Save changes",
"variant": "solid",
"size": "md",
"disabled": false,
"fullWidth": false,
"busy": {
"$cond": {
"$state": "/meta/objects/customer/operations/update/phase",
"eq": "loading"
},
"$then": true,
"$else": false
},
"busyLabel": "Saving…"
},
"children": [],
"on": {
"press": {
"action": "update",
"params": {
"object": "customer",
"inputs": {
"displayName": {
"$state": "/drafts/customer/displayName"
},
"email": {
"$state": "/drafts/customer/contact/email"
}
}
}
}
}
}
}

The host reports contact.email as not writable for this person. The Input’s writable reads that answer, and a Text shows the host’s reason when it is locked:

{
"email-input": {
"type": "Input",
"props": {
"label": "Billing email",
"placeholder": "billing@company.example",
"inputType": "email",
"required": true,
"helpText": null,
"value": {
"$bindState": "/drafts/customer/contact/email"
},
"writable": {
"$state": "/meta/objects/customer/fields/contact.email/writable"
},
"error": {
"$state": "/meta/objects/customer/operations/update/error/fieldErrors/email"
}
},
"children": []
},
"email-locked": {
"type": "Text",
"props": {
"content": {
"$state": "/meta/objects/customer/fields/contact.email/reason"
},
"size": "sm",
"tone": "muted",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/fields/contact.email/writable",
"eq": false
}
}
}

The whole section is hidden unless the host allows delete, and the button carries the plain-text question Mica asks before anything is sent:

{
"danger": {
"type": "Section",
"props": {
"eyebrow": null,
"title": "Close account",
"tone": "outlined",
"padding": "lg"
},
"children": [
"danger-copy",
"danger-button",
"danger-done"
],
"visible": {
"$state": "/meta/objects/customer/capabilities/delete/allowed",
"eq": true
}
},
"danger-button": {
"type": "Button",
"props": {
"label": "Close this account",
"variant": "danger",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": {
"action": "delete",
"params": {
"object": "customer",
"confirmation": "Close this customer account? This cannot be undone."
}
}
},
"visible": {
"$state": "/objects/customer/data"
}
}
}
Complete document — examples/documents/customer-profile.json
examples/documents/customer-profile.json
{
"envelopeVersion": 1,
"ui": {
"root": "app",
"elements": {
"app": {
"type": "PageStack",
"props": {
"name": "Customer profile"
},
"children": [
"page-profile"
]
},
"page-profile": {
"type": "Page",
"props": {
"title": "Profile",
"background": "mist"
},
"children": [
"loading",
"load-error",
"summary",
"editor",
"danger"
]
},
"loading": {
"type": "Text",
"props": {
"content": "Loading customer…",
"size": "md",
"tone": "muted",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/operations/fetch/phase",
"eq": "loading"
}
},
"load-error": {
"type": "Section",
"props": {
"eyebrow": null,
"title": "We couldn't load this customer",
"tone": "outlined",
"padding": "lg"
},
"children": [
"load-error-message",
"load-retry"
],
"visible": {
"$state": "/meta/objects/customer/operations/fetch/phase",
"eq": "error"
}
},
"load-error-message": {
"type": "Text",
"props": {
"content": {
"$state": "/meta/objects/customer/operations/fetch/error/message"
},
"size": "md",
"tone": "accent",
"align": "left",
"italic": false
},
"children": []
},
"load-retry": {
"type": "Button",
"props": {
"label": "Try again",
"variant": "outline",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": {
"action": "fetch",
"params": {
"object": "customer",
"inputs": {
"customerId": {
"$state": "/customerId"
}
}
}
}
}
},
"summary": {
"type": "Section",
"props": {
"eyebrow": "CUSTOMER",
"title": null,
"tone": "ink",
"padding": "xl"
},
"children": [
"summary-name",
"summary-plan",
"summary-email",
"summary-edit"
],
"visible": [
{
"$state": "/meta/objects/customer/operations/fetch/phase",
"eq": "success"
},
{
"$state": "/objects/customer/data"
}
]
},
"summary-name": {
"type": "Heading",
"props": {
"content": {
"$state": "/objects/customer/data/displayName"
},
"level": "1",
"align": "left",
"accent": true
},
"children": []
},
"summary-plan": {
"type": "Text",
"props": {
"content": {
"$state": "/objects/customer/data/plan"
},
"size": "lg",
"tone": "inverse",
"align": "left",
"italic": false
},
"children": []
},
"summary-email": {
"type": "Text",
"props": {
"content": {
"$state": "/objects/customer/data/contact/email"
},
"size": "md",
"tone": "inverse",
"align": "left",
"italic": false
},
"children": []
},
"summary-edit": {
"type": "Button",
"props": {
"label": "Edit profile",
"variant": "solid",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": {
"action": "setState",
"params": {
"statePath": "/drafts/customer",
"value": {
"$state": "/objects/customer/data"
}
}
}
},
"visible": {
"$state": "/meta/objects/customer/capabilities/update/allowed",
"eq": true
}
},
"editor": {
"type": "Section",
"props": {
"eyebrow": "DRAFT",
"title": "Edit profile",
"tone": "raised",
"padding": "lg"
},
"children": [
"name-input",
"email-input",
"email-locked",
"save",
"saved",
"save-error"
],
"visible": [
{
"$state": "/drafts/customer/id"
},
{
"$state": "/objects/customer/data"
}
]
},
"name-input": {
"type": "Input",
"props": {
"label": "Display name",
"placeholder": "Company name",
"inputType": "text",
"required": true,
"helpText": "Shown on invoices and in the customer portal",
"value": {
"$bindState": "/drafts/customer/displayName"
},
"writable": {
"$state": "/meta/objects/customer/fields/displayName/writable"
},
"error": {
"$state": "/meta/objects/customer/operations/update/error/fieldErrors/displayName"
}
},
"children": []
},
"email-input": {
"type": "Input",
"props": {
"label": "Billing email",
"placeholder": "billing@company.example",
"inputType": "email",
"required": true,
"helpText": null,
"value": {
"$bindState": "/drafts/customer/contact/email"
},
"writable": {
"$state": "/meta/objects/customer/fields/contact.email/writable"
},
"error": {
"$state": "/meta/objects/customer/operations/update/error/fieldErrors/email"
}
},
"children": []
},
"email-locked": {
"type": "Text",
"props": {
"content": {
"$state": "/meta/objects/customer/fields/contact.email/reason"
},
"size": "sm",
"tone": "muted",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/fields/contact.email/writable",
"eq": false
}
},
"save": {
"type": "Button",
"props": {
"label": "Save changes",
"variant": "solid",
"size": "md",
"disabled": false,
"fullWidth": false,
"busy": {
"$cond": {
"$state": "/meta/objects/customer/operations/update/phase",
"eq": "loading"
},
"$then": true,
"$else": false
},
"busyLabel": "Saving…"
},
"children": [],
"on": {
"press": {
"action": "update",
"params": {
"object": "customer",
"inputs": {
"displayName": {
"$state": "/drafts/customer/displayName"
},
"email": {
"$state": "/drafts/customer/contact/email"
}
}
}
}
}
},
"saved": {
"type": "Text",
"props": {
"content": "Saved. The profile above shows what the host stored.",
"size": "md",
"tone": "accent",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/operations/update/phase",
"eq": "success"
}
},
"save-error": {
"type": "Text",
"props": {
"content": {
"$state": "/meta/objects/customer/operations/update/error/message"
},
"size": "md",
"tone": "accent",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/operations/update/phase",
"eq": "error"
}
},
"danger": {
"type": "Section",
"props": {
"eyebrow": null,
"title": "Close account",
"tone": "outlined",
"padding": "lg"
},
"children": [
"danger-copy",
"danger-button",
"danger-done"
],
"visible": {
"$state": "/meta/objects/customer/capabilities/delete/allowed",
"eq": true
}
},
"danger-copy": {
"type": "Text",
"props": {
"content": "Closing removes the customer record. Invoices already sent are kept.",
"size": "md",
"tone": "muted",
"align": "left",
"italic": false
},
"children": []
},
"danger-button": {
"type": "Button",
"props": {
"label": "Close this account",
"variant": "danger",
"size": "md",
"disabled": false,
"fullWidth": false
},
"children": [],
"on": {
"press": {
"action": "delete",
"params": {
"object": "customer",
"confirmation": "Close this customer account? This cannot be undone."
}
}
},
"visible": {
"$state": "/objects/customer/data"
}
},
"danger-done": {
"type": "Text",
"props": {
"content": "The account is closed.",
"size": "md",
"tone": "accent",
"align": "left",
"italic": false
},
"children": [],
"visible": {
"$state": "/meta/objects/customer/operations/delete/phase",
"eq": "success"
}
}
},
"state": {
"customerId": "cus_2041",
"drafts": {
"customer": {}
}
}
},
"meta": {
"pack": {
"id": "mica.core",
"version": "1.0.0"
}
},
"objects": {
"customer": {
"type": "crm.customer",
"cardinality": "one",
"identity": "id",
"fields": {
"id": {
"kind": "string",
"required": true
},
"displayName": {
"kind": "string",
"required": true
},
"plan": {
"kind": "string",
"required": true,
"enum": [
"starter",
"growth",
"scale"
]
},
"contact": {
"kind": "object",
"required": true,
"fields": {
"email": {
"kind": "string",
"required": true,
"format": "email"
}
}
}
},
"capabilities": {
"fetch": {
"inputs": {
"customerId": {
"kind": "string",
"required": true
}
}
},
"update": {
"inputs": {
"displayName": {
"kind": "string",
"required": true,
"minLength": 2,
"maxLength": 60
},
"email": {
"kind": "string",
"required": true,
"format": "email"
}
}
},
"delete": {}
},
"sample": {
"id": "cus_sample",
"displayName": "Sample Customer Ltd",
"plan": "growth",
"contact": {
"email": "billing@sample.example"
}
}
}
},
"lifecycle": {
"startup": [
{
"action": "fetch",
"params": {
"object": "customer",
"inputs": {
"customerId": {
"$state": "/customerId"
}
}
}
}
]
}
}