Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce ok/1 and noreply/1 helpers #783

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions lib/backpex/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ defmodule Backpex.Fields.BelongsTo do
display_field = display_field(field)
display_field_form = display_field_form(field, display_field)

socket =
socket
|> assign(assigns)
|> assign(queryable: queryable)
|> assign(owner_key: owner_key)
|> assign(display_field: display_field)
|> assign(display_field_form: display_field_form)

{:ok, socket}
socket
|> assign(assigns)
|> assign(queryable: queryable)
|> assign(owner_key: owner_key)
|> assign(display_field: display_field)
|> assign(display_field_form: display_field_form)
|> ok()
end

@impl Backpex.Field
Expand Down
38 changes: 16 additions & 22 deletions lib/backpex/fields/has_many.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,16 @@ defmodule Backpex.Fields.HasMany do
"""
use Backpex.Field, config_schema: @config_schema
import Ecto.Query
import Backpex.HTML.Form
alias Backpex.Adapters.Ecto, as: EctoAdapter
alias Backpex.HTML.Form
alias Backpex.Router

@impl Phoenix.LiveComponent
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> apply_action(assigns.type)

{:ok, socket}
socket
|> assign(assigns)
|> apply_action(assigns.type)
|> ok()
end

defp apply_action(socket, :index) do
Expand Down Expand Up @@ -175,7 +173,7 @@ defmodule Backpex.Fields.HasMany do
</div>
</div>
</label>
<.error :for={msg <- @errors}>{msg}</.error>
<Form.error :for={msg <- @errors}>{msg}</Form.error>
<div tabindex="0" class="dropdown-content z-[1] menu bg-base-100 rounded-box w-full overflow-y-auto shadow">
<div class="max-h-72 p-2">
<input
Expand Down Expand Up @@ -257,25 +255,21 @@ defmodule Backpex.Fields.HasMany do
def handle_event("search", params, socket) do
search_input = Map.get(params, to_string(socket.assigns.name) <> "_search", "")

socket =
socket
|> assign(:offset, 0)
|> assign(:search_input, search_input)
|> assign_options()

{:noreply, socket}
socket
|> assign(:offset, 0)
|> assign(:search_input, search_input)
|> assign_options()
|> noreply()
end

@impl Phoenix.LiveComponent
def handle_event("show-more", _params, socket) do
%{assigns: %{field_options: field_options, offset: offset, options: options}} = socket

socket =
socket
|> assign(:offset, field_options[:query_limit] + offset)
|> assign_options(options)

{:noreply, socket}
socket
|> assign(:offset, field_options[:query_limit] + offset)
|> assign_options(options)
|> noreply()
end

@impl Backpex.Field
Expand Down Expand Up @@ -543,7 +537,7 @@ defmodule Backpex.Fields.HasMany do
errors = if Phoenix.Component.used_input?(form[name]), do: form[name].errors, else: []
translate_error_fun = Map.get(field_options, :translate_error, &Function.identity/1)

assign(socket, :errors, translate_form_errors(errors, translate_error_fun))
assign(socket, :errors, Form.translate_form_errors(errors, translate_error_fun))
end

defp display_field_form({_name, field_options} = field),
Expand Down
46 changes: 18 additions & 28 deletions lib/backpex/fields/has_many_through.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ defmodule Backpex.Fields.HasManyThrough do

@impl Phoenix.LiveComponent
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> apply_action(assigns.type)

{:ok, socket}
socket
|> assign(assigns)
|> apply_action(assigns.type)
|> ok()
end

defp apply_action(socket, :form) do
Expand Down Expand Up @@ -370,12 +368,10 @@ defmodule Backpex.Fields.HasManyThrough do

put_assoc(association.pivot.field, all_assocs)

socket =
socket
|> assign(return_to_change: change)
|> assign(edit_relational: Enum.count(existing))

{:noreply, socket}
socket
|> assign(return_to_change: change)
|> assign(edit_relational: Enum.count(existing))
|> noreply()
end

@impl Phoenix.LiveComponent
Expand Down Expand Up @@ -413,21 +409,17 @@ defmodule Backpex.Fields.HasManyThrough do
index = String.to_integer(index)
change = Changeset.get_change(changeset, association.pivot.field, existing)

socket =
socket
|> assign(edit_relational: index)
|> assign(return_to_change: change)

{:noreply, socket}
socket
|> assign(edit_relational: index)
|> assign(return_to_change: change)
|> noreply()
end

@impl Phoenix.LiveComponent
def handle_event("complete-relational", _params, socket) do
socket =
socket
|> assign(edit_relational: nil)

{:noreply, socket}
socket
|> assign(edit_relational: nil)
|> noreply()
end

@impl Phoenix.LiveComponent
Expand All @@ -436,11 +428,9 @@ defmodule Backpex.Fields.HasManyThrough do

put_assoc(association.pivot.field, return_to_change)

socket =
socket
|> assign(edit_relational: nil)

{:noreply, socket}
socket
|> assign(edit_relational: nil)
|> noreply()
end

defp action_fields(fields, assigns, action), do: LiveResource.filtered_fields_by_action(fields, assigns, action)
Expand Down
8 changes: 3 additions & 5 deletions lib/backpex/fields/inline_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ defmodule Backpex.Fields.InlineCRUD do

@impl Phoenix.LiveComponent
def update(assigns, socket) do
socket =
socket
|> assign(assigns)

{:ok, socket}
socket
|> assign(assigns)
|> ok()
end

@impl Backpex.Field
Expand Down
55 changes: 23 additions & 32 deletions lib/backpex/fields/multi_select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,19 @@ defmodule Backpex.Fields.MultiSelect do
]
"""
use Backpex.Field, config_schema: @config_schema

import Backpex.HTML.Form
alias Backpex.HTML.Form

@impl Phoenix.LiveComponent
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:not_found_text, assigns.field_options[:not_found_text] || Backpex.translate("No options found"))
|> assign(:prompt, prompt(assigns, assigns.field_options))
|> assign(:search_input, "")
|> assign_options()
|> assign_selected()
|> maybe_assign_form()

{:ok, socket}
socket
|> assign(assigns)
|> assign(:not_found_text, assigns.field_options[:not_found_text] || Backpex.translate("No options found"))
|> assign(:prompt, prompt(assigns, assigns.field_options))
|> assign(:search_input, "")
|> assign_options()
|> assign_selected()
|> maybe_assign_form()
|> ok()
end

defp assign_options(socket) do
Expand Down Expand Up @@ -145,7 +142,7 @@ defmodule Backpex.Fields.MultiSelect do
<:label align={Backpex.Field.align_label(@field_options, assigns)}>
<Layout.input_label text={@field_options[:label]} />
</:label>
<.multi_select
<Form.multi_select
field={@form[@name]}
prompt={@prompt}
not_found_text={@not_found_text}
Expand Down Expand Up @@ -181,12 +178,10 @@ defmodule Backpex.Fields.MultiSelect do

show_select_all = length(new_selected) != length(field_options.options.(socket.assigns))

socket =
socket
|> assign(:selected, new_selected)
|> assign(:show_select_all, show_select_all)

{:noreply, socket}
socket
|> assign(:selected, new_selected)
|> assign(:show_select_all, show_select_all)
|> noreply()
end

@impl Phoenix.LiveComponent
Expand All @@ -199,12 +194,10 @@ defmodule Backpex.Fields.MultiSelect do
field_options.options.(assigns)
|> maybe_apply_search(search_input)

socket =
socket
|> assign(:options, options)
|> assign(:search_input, search_input)

{:noreply, socket}
socket
|> assign(:options, options)
|> assign(:search_input, search_input)
|> noreply()
end

@impl Phoenix.LiveComponent
Expand All @@ -213,12 +206,10 @@ defmodule Backpex.Fields.MultiSelect do

new_selected = if show_select_all, do: field_options.options.(assigns), else: []

socket =
socket
|> assign(:selected, new_selected)
|> assign(:show_select_all, not show_select_all)

{:noreply, socket}
socket
|> assign(:selected, new_selected)
|> assign(:show_select_all, not show_select_all)
|> noreply()
end

defp maybe_apply_search(options, search_input) do
Expand Down
20 changes: 8 additions & 12 deletions lib/backpex/item_actions/delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,18 @@ defmodule Backpex.ItemActions.Delete do

Enum.each(deleted_items, fn deleted_item -> socket.assigns.live_resource.on_item_deleted(socket, deleted_item) end)

socket =
socket
|> clear_flash()
|> put_flash(:info, success_message(socket.assigns, deleted_items))

{:ok, socket}
socket
|> clear_flash()
|> put_flash(:info, success_message(socket.assigns, deleted_items))
|> ok()
rescue
error ->
Logger.error("An error occurred while deleting the resource: #{inspect(error)}")

socket =
socket
|> clear_flash()
|> put_flash(:error, error_message(socket.assigns, error, items))

{:ok, socket}
socket
|> clear_flash()
|> put_flash(:error, error_message(socket.assigns, error, items))
|> ok()
end

defp success_message(assigns, [_item]) do
Expand Down
Loading
Loading