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

Add index edit for belongs to field #227

Merged
merged 3 commits into from
Apr 23, 2024
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
1 change: 1 addition & 0 deletions demo/lib/demo_web/live/post_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ defmodule DemoWeb.PostLive do
full_name: fragment("concat(?, ' ', ?)", user.first_name, user.last_name)
})
end,
index_editable: true,
searchable: true,
live_resource: DemoWeb.UserLive
},
Expand Down
91 changes: 78 additions & 13 deletions lib/backpex/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ defmodule Backpex.Fields.BelongsTo do
alias Backpex.LiveResource
alias Backpex.Router

@impl Phoenix.LiveComponent
def update(assigns, socket) do
%{schema: schema, name: name, field: field} = assigns

%{queryable: queryable, owner_key: owner_key} = schema.__schema__(:association, name)

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}
end

@impl Backpex.Field
def render_value(%{value: value} = assigns) when is_nil(value) do
~H"""
Expand All @@ -44,11 +64,11 @@ defmodule Backpex.Fields.BelongsTo do

@impl Backpex.Field
def render_value(assigns) do
%{field: field, value: value} = assigns
%{value: value, display_field: display_field} = assigns

assigns =
assigns
|> assign(:display_text, Map.get(value, display_field(field)))
|> assign(:display_text, Map.get(value, display_field))
|> assign_link()

~H"""
Expand All @@ -68,18 +88,15 @@ defmodule Backpex.Fields.BelongsTo do

@impl Backpex.Field
def render_form(assigns) do
%{schema: schema, repo: repo, field_options: field_options, name: name} = assigns
%{
repo: repo,
field_options: field_options,
queryable: queryable,
owner_key: owner_key,
display_field_form: display_field_form
} = assigns

%{queryable: queryable, owner_key: owner_key} = schema.__schema__(:association, name)

display_field = Map.get(field_options, :display_field_form, Map.get(field_options, :display_field))

options =
queryable
|> from()
|> maybe_options_query(field_options, assigns)
|> repo.all()
|> Enum.map(&{Map.get(&1, display_field), Map.get(&1, :id)})
options = get_options(repo, queryable, field_options, display_field_form, assigns)

assigns =
assigns
Expand All @@ -105,6 +122,42 @@ defmodule Backpex.Fields.BelongsTo do
"""
end

@impl Backpex.Field
def render_index_form(assigns) do
%{repo: repo, field_options: field_options, queryable: queryable, display_field_form: display_field_form} = assigns

options = get_options(repo, queryable, field_options, display_field_form, assigns)

form = to_form(%{"value" => assigns.value}, as: :index_form)

assigns =
assigns
|> assign(:form, form)
|> assign(:options, options)
|> assign_new(:valid, fn -> true end)
|> assign_prompt(assigns.field_options)

~H"""
<div>
<.form for={@form} class="relative" phx-change="update-field" phx-submit="update-field" phx-target={@myself}>
<select
name={@form[:value].name}
class={["select select-sm", if(@valid, do: "hover:input-bordered", else: "select-error")]}
disabled={@readonly}
>
<option :if={@prompt} value=""><%= @prompt %></option>
<%= Phoenix.HTML.Form.options_for_select(@options, @value && Map.get(@value, :id)) %>
</select>
</.form>
</div>
"""
end

@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, %{} |> Map.put(socket.assigns.owner_key, value))
end

@impl Backpex.Field
def display_field({_name, field_options}) do
Map.get(field_options, :display_field)
Expand All @@ -119,6 +172,18 @@ defmodule Backpex.Fields.BelongsTo do
@impl Backpex.Field
def association?(_field), do: true

defp display_field_form({_name, field_options} = _field, display_field) do
Map.get(field_options, :display_field_form, display_field)
end

defp get_options(repo, queryable, field_options, display_field, assigns) do
queryable
|> from()
|> maybe_options_query(field_options, assigns)
|> repo.all()
|> Enum.map(&{Map.get(&1, display_field), Map.get(&1, :id)})
end
pehbehbeh marked this conversation as resolved.
Show resolved Hide resolved

defp assign_link(assigns) do
%{socket: socket, field_options: field_options, value: value, live_resource: live_resource, params: params} =
assigns
Expand Down
1 change: 1 addition & 0 deletions lib/backpex/live_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ defmodule Backpex.LiveResource do
end

Currently supported by the following fields:
- `Backpex.Fields.BelongsTo`
- `Backpex.Fields.Date`
- `Backpex.Fields.DateTime`
- `Backpex.Fields.Number`
Expand Down