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

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.

elixir
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:

KeyMeaning
titleRequired. The question, such as "Delete this product?".
bodyA 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.
answerA 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_labelThe label of the field the answer is typed into. The default is "Type <answer> to confirm".
confirm_labelThe label of the button that answers. The default is "Confirm".
cancel_labelThe 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.

elixir
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"
    ]
end

The 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.

elixir
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"
    ]
end

Ask 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.

elixir
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"
         ]
end

On 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.

elixir
assigns
|> assign(:phx_click, "row_action")
|> assign(:phx_value_id, record.id)
|> MishkaGervaz.Question.bind(action, record)

Next steps