Cinder.Refresh (Cinder v0.18.1)

Copy Markdown View Source

Helper functions for refreshing Cinder collection data from parent LiveViews.

These functions refresh collection data after performing CRUD operations, ensuring the collection reflects the latest state without requiring a full page reload.

Usage

After performing operations that modify data displayed in a collection:

def handle_event("delete", %{"id" => id}, socket) do
  MyApp.MyResource
  |> Ash.get!(id)
  |> Ash.destroy!()

  {:noreply, Cinder.refresh_table(socket, "my-table-id")}
end

Or to refresh multiple collections:

def handle_event("bulk_delete", params, socket) do
  # ... perform bulk operations ...

  {:noreply, Cinder.refresh_tables(socket, ["users-table", "orders-table"])}
end

Refresh Behavior

When a collection is refreshed:

  • Current filters are maintained
  • Sort order is preserved
  • Pagination state is kept (user stays on current page if possible)
  • Loading state is shown during refresh by default
  • Data is reloaded using the same query parameters

Pass silent: true to keep the currently rendered data visible without the loading treatment while its asynchronous replacement query runs. If that query fails, the error is logged, the existing data stays visible, and no error indicator is shown. Collections without existing rows use normal loading and error behavior, even when silent: true is requested.

Summary

Functions

refresh_table(socket, collection_id, opts \\ [])

Refreshes a specific collection by its ID.

Sends a refresh message to the LiveComponent with the given ID. The collection will reload its data while maintaining current filters, sorting, and pagination state.

Parameters

  • socket - The LiveView socket
  • collection_id - The ID of the collection to refresh (string)
  • opts - Optional keyword list (defaults to [])

Options

  • :silent - Boolean, defaults to false. When true and the collection has existing rows, keeps those rows visible without a loading indicator until the replacement data arrives. A failed silent refresh logs the error and retains the rows without showing an error indicator. Empty collections use normal loading and error behavior.

Raises ArgumentError if :silent is not a boolean.

Returns

The socket (unchanged, but refresh message has been sent).

Examples

# Refresh a specific collection
{:noreply, Cinder.refresh_table(socket, "users-table")}

# Refresh existing rows without a loading indicator
{:noreply, Cinder.refresh_table(socket, "users-table", silent: true)}

# In a handle_event callback
def handle_event("delete_user", %{"id" => id}, socket) do
  MyApp.User
  |> Ash.get!(id)
  |> Ash.destroy!()

  {:noreply, Cinder.refresh_table(socket, "users-table")}
end

refresh_tables(socket, collection_ids, opts \\ [])

Refreshes multiple collections by their IDs.

Convenience function to refresh several collections at once while maintaining granular control over which collections are refreshed.

Parameters

  • socket - The LiveView socket
  • collection_ids - List of collection IDs to refresh
  • opts - Optional keyword list (defaults to []), with the same :silent option as refresh_table/3

With silent: true, each collection with existing rows keeps them visible while loading replacement data. Failures are logged without displaying an error indicator or clearing those rows. Collections without existing rows retain normal loading and error behavior.

Raises ArgumentError if :silent is not a boolean.

Returns

The socket (unchanged, but refresh messages have been sent to all specified collections).

Examples

{:noreply, Cinder.refresh_tables(socket, ["users-table", "orders-table"])}

{:noreply, Cinder.refresh_tables(socket, ["users-table", "orders-table"], silent: true)}

update_if_visible(socket, collection_id, id, update_fn)

See Cinder.Update.update_if_visible/4.

update_item(socket, collection_id, id, update_fn)

See Cinder.Update.update_item/4.

update_items(socket, collection_id, ids, update_fn)

See Cinder.Update.update_items/4.

update_items_if_visible(socket, collection_id, ids, update_fn)

See Cinder.Update.update_items_if_visible/4.