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

Fix index form does not update form value #534

Merged
merged 3 commits into from
Aug 29, 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
23 changes: 23 additions & 0 deletions guides/upgrading/v0.7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Upgrading to v0.7

## Update calls to [`Backpex.Field.handle_index_editable/2`](Backpex.Field.html#handle_index_editable/3)

We have updated the arity and syntax of [`Backpex.Field.handle_index_editable/2`](Backpex.Field.html#handle_index_editable/3). It is now [`Backpex.Field.handle_index_editable/3`](Backpex.Field.html#handle_index_editable/3) and accepts the `socket`, the `value` and the `change`. We now need the value to update the form accordingly.

If you had code like this, e.g. for custom fields:

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

it should now look like this

```elixir
@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value))
end
```
3 changes: 2 additions & 1 deletion lib/backpex/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ defmodule Backpex.Field do
@doc """
Handles index editable.
"""
def handle_index_editable(socket, change) do
def handle_index_editable(socket, value, change) do
if not Backpex.LiveResource.can?(socket.assigns, :edit, socket.assigns.item, socket.assigns.live_resource) do
raise Backpex.ForbiddenError
end
Expand Down Expand Up @@ -274,6 +274,7 @@ defmodule Backpex.Field do
_error ->
assign(socket, :valid, false)
end
|> assign(:form, Phoenix.Component.to_form(%{"value" => value}, as: :index_form))

{:noreply, socket}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/backpex/fields/belongs_to.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ defmodule Backpex.Fields.BelongsTo do

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

Expand All @@ -155,7 +155,7 @@ defmodule Backpex.Fields.BelongsTo do

@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))
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.owner_key, value))
end

@impl Backpex.Field
Expand Down
4 changes: 2 additions & 2 deletions lib/backpex/fields/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ defmodule Backpex.Fields.Date do

assigns =
assigns
|> assign(:form, form)
|> assign_new(:form, fn -> form end)
|> assign(:valid, Map.get(assigns, :valid, true))

~H"""
Expand All @@ -117,6 +117,6 @@ defmodule Backpex.Fields.Date do

@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, %{} |> Map.put(socket.assigns.name, value))
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value))
end
end
4 changes: 2 additions & 2 deletions lib/backpex/fields/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ defmodule Backpex.Fields.DateTime do

assigns =
assigns
|> assign(:form, form)
|> assign(:valid, Map.get(assigns, :valid, true))
|> assign_new(:form, fn -> form end)

~H"""
<div>
Expand All @@ -117,6 +117,6 @@ defmodule Backpex.Fields.DateTime do

@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, %{} |> Map.put(socket.assigns.name, value))
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value))
end
end
4 changes: 2 additions & 2 deletions lib/backpex/fields/number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Backpex.Fields.Number do

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

~H"""
Expand All @@ -76,7 +76,7 @@ defmodule Backpex.Fields.Number do

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

@impl Backpex.Field
Expand Down
6 changes: 3 additions & 3 deletions lib/backpex/fields/select.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ defmodule Backpex.Fields.Select do

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

Expand All @@ -84,7 +84,7 @@ defmodule Backpex.Fields.Select do
disabled={@readonly}
>
<option :if={@prompt} value=""><%= @prompt %></option>
<%= Phoenix.HTML.Form.options_for_select(@options, @value) %>
<%= Phoenix.HTML.Form.options_for_select(@options, @form[:value].value) %>
</select>
</.form>
</div>
Expand All @@ -93,7 +93,7 @@ defmodule Backpex.Fields.Select do

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

defp get_label(value, options) do
Expand Down
4 changes: 2 additions & 2 deletions lib/backpex/fields/text.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Backpex.Fields.Text do

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

~H"""
Expand All @@ -74,6 +74,6 @@ defmodule Backpex.Fields.Text do

@impl Phoenix.LiveComponent
def handle_event("update-field", %{"index_form" => %{"value" => value}}, socket) do
Backpex.Field.handle_index_editable(socket, %{} |> Map.put(socket.assigns.name, value))
Backpex.Field.handle_index_editable(socket, value, Map.put(%{}, socket.assigns.name, value))
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ defmodule Backpex.MixProject do
"guides/custom_labels_and_translations/custom-labels-and-translations.md",

# Upgrade Guides
"guides/upgrading/v0.7.md",
"guides/upgrading/v0.6.md",
"guides/upgrading/v0.5.md",
"guides/upgrading/v0.3.md",
Expand Down