MishkaBlogWeb.Live.Admin.Dashboard.VersionsModal (Mishka Blog v0.0.1)

Copy Markdown View Source

The version history of a dashboard list page: a modal that lists one record's versions a page at a time, previews one of them, and restores one. The page keeps the template, its :versions_modal and the one-line event handlers; everything they do is here.

Every function takes the page's socket and returns it, with the assigns the template reads: :versions, :versions_record_id, :versions_record_name, :versions_loading, :versions_page, :versions_per_page, :versions_total, :version_masters and :preview_version_id.

@versions [
  resource: MishkaBlog.Blogger,
  name: :display_name,
  modal: "blogger-versions-modal",
  not_found: gettext_noop("Blogger not found or access denied")
]

def mount(_params, _session, socket) do
  {:ok, VersionsModal.assign_defaults(socket)}
end

def handle_event("restore_version", %{"version-id" => id}, socket),
  do: {:noreply, VersionsModal.restore(socket, @versions, id)}

def handle_info({:row_action, "show_versions", %{"id" => id}}, socket),
  do: {:noreply, VersionsModal.show(socket, @versions, id)}

def handle_async(:load_versions, result, socket),
  do: {:noreply, VersionsModal.loaded(socket, @versions, result)}

The versions are read, and a version is restored, as the signed-in user in :current_user: a master through the master actions and every site, a site's admin through the site's own.

Options

  • :resource - Required. The record's resource. It needs the :get_with_versions and :master_get_with_versions reads, a Version resource beside it, and the :restore_version_api and :master_restore_version_api actions.
  • :name - Required. The attribute shown as the modal's subtitle.
  • :modal - Required. The DOM id of the versions modal.
  • :not_found - Required. The flash for a record that cannot be read.
  • :load - What else the read loads, as [master: [...], site: [...]].
  • :loaded - A (socket, record) function run once the versions are in, to assign more.

Summary

Functions

The assigns a page starts with, no record selected and the modal empty.

Hides the modal id, the way open_modal/2 shows it.

Reads the current page of the selected record's versions in the background.

Takes the result of the :load_versions async started by load/2: the versions on success, a flash on a failure, and the modal closed when the record cannot be read.

Goes to the next page of versions; on the last page the socket is left as it is.

Shows the modal id. A modal opened by :if snaps in and out, gone from the DOM before its own transition can run, so the modals stay rendered and are driven by the gervaz:exec-js bridge the Gervaz form uses.

Goes to the previous page of versions; on the first page the socket is left as it is.

Forgets the record and its versions, as when the modal is dismissed.

Restores the selected record to the version version_id. On success the modal closes and a flash says so; otherwise the modal stays and the flash is the refusal.

Opens the modal for the record record_id and starts reading its first page of versions.

Opens the preview of the version version_id, or closes it when it is already open.

Functions

assign_defaults(socket)

The assigns a page starts with, no record selected and the modal empty.

close_modal(socket, id)

Hides the modal id, the way open_modal/2 shows it.

load(socket, config)

Reads the current page of the selected record's versions in the background.

loaded(socket, config, arg3)

Takes the result of the :load_versions async started by load/2: the versions on success, a flash on a failure, and the modal closed when the record cannot be read.

next_page(socket, config)

Goes to the next page of versions; on the last page the socket is left as it is.

open_modal(socket, id)

Shows the modal id. A modal opened by :if snaps in and out, gone from the DOM before its own transition can run, so the modals stay rendered and are driven by the gervaz:exec-js bridge the Gervaz form uses.

previous_page(socket, config)

Goes to the previous page of versions; on the first page the socket is left as it is.

reset(socket)

Forgets the record and its versions, as when the modal is dismissed.

restore(socket, config, version_id)

Restores the selected record to the version version_id. On success the modal closes and a flash says so; otherwise the modal stays and the flash is the refusal.

show(socket, config, record_id)

Opens the modal for the record record_id and starts reading its first page of versions.

toggle_preview(socket, version_id)

Opens the preview of the version version_id, or closes it when it is already open.