Ask before an action
v0.0.12
A row action, a bulk action or a form's Save button can ask a question before it acts. A string confirm asks the browser's own dialog. A confirm written as a keyword list asks in a question window that Gervaz draws through the UI adapter, with a title, a sentence, a tone and your own button labels, and it can ask the reader to type a word before the button answers. This guide asks each way for a row action, a bulk action and a save.
Before you begin
- Have a table with row actions or bulk actions, or a form with a
submitblock. See Add row and bulk actions and Build an admin form. - Use the default templates, or end a custom table template with
Shared.render_overlays/1, which draws the window. See Write a table or form template.
Ask in the browser's dialog
A string, or a function of the record that returns one, is the browser's own question, sent through data-confirm. Gervaz shows the text as written.
action :delete, type: :destroy, confirm: "Delete this item permanently?"Ask in the question window
Give confirm a keyword list with a title. These keys are read:
| Key | Meaning |
|---|---|
title | Required. The question, such as "Delete this product?". |
body | A sentence under it: what the action does and what is lost. |
tone | :danger (the default), :warn or :info. It sets the colour of the answering button. |
answer | A word the reader types before the button answers, such as the record's name. The button stays off until what was typed matches, case included. The spaces around it are ignored. |
answer_label | The label of the field the answer is typed into. The default is "Type <answer> to confirm". |
confirm_label | The label of the button that answers. The default is "Confirm". |
cancel_label | The label of the button that closes the window. The default is "Cancel". |
Each value but tone is a string, which is translated, or a function of one argument. The server draws the window and takes it away, and nothing in it is JavaScript. Cancel, the backdrop and Escape close it without running the action.
Ask about a row
For a row action the function receives the row. The table reads the row again first with the table's get action, so a row that is gone asks nothing.
row_actions do
action :delete, type: :destroy,
confirm: [
title: fn record -> "Delete #{record.name}?" end,
body: "Its files and its sales history are deleted with it.",
tone: :danger,
answer: fn record -> record.name end,
confirm_label: "Delete product"
]
endThe action runs once the question is answered. The window of a table has the id "<table id>-question".
Ask about the selected rows
For a bulk action, {count} in a string becomes the number of rows the action runs on. A function receives that number, or nil when the table cannot tell, as with select all on a :load_more table with show_total false.
bulk_actions do
action :archive, type: :destroy,
confirm: [
title: "Archive {count} products?",
body: "They leave the shop until you restore them.",
tone: :warn,
confirm_label: "Archive"
]
endAsk before a save
A create or update button in the form's submit block asks the same way. A function receives the record the form edits, and nil on create. The form draws the window under its own <form> with the id "<form id>-question". The save runs with the values the form was submitted with once the question is answered, and Cancel saves nothing.
submit do
update confirm: [
title: "Publish the changes?",
body: fn product -> "Buyers of #{product.name} see them at once." end,
tone: :warn,
confirm_label: "Publish"
]
endOn a cancel or discard button, a string confirm asks the browser's dialog, and only while the form holds changes. A keyword list is read only on create and update.
Ask from a custom action type
A custom action type puts the action's confirm on its button with MishkaGervaz.Question.bind/4. Call it after the assigns hold :phx_click and the phx_value_* values. A string or a function becomes :data_confirm. A keyword list makes the press send ask to the table, which keeps the event and its values and runs them once the question is answered.
assigns
|> assign(:phx_click, "row_action")
|> assign(:phx_value_id, record.id)
|> MishkaGervaz.Question.bind(action, record)Next steps
- Show a toast: tell the reader what happened after the action.
- Write a custom type: build an action type.
- Table actions DSL and Form uploads, submit and presentation DSL: the options of
confirm.