Skip to content

Commit

Permalink
Save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
cibernox committed Nov 8, 2024
1 parent 9254ff7 commit 9dabb8e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ defmodule Beacon.LiveAdmin.PropertiesSidebarComponent do
<%= if @attributes_editable do %>
<%!-- Editable attributes --%>
<%= for {{name, value}, index} <- Enum.with_index(@selected_ast_element["attrs"]) do %>
<.live_component module={PropertiesSidebarSectionComponent} id="class-section" parent={@myself} name={name} value={value} edit_name={false} index={index} placeholder="Some placeholder"/>
<.live_component module={PropertiesSidebarSectionComponent} id="class-section" parent={@myself} name={name} value={value} edit_name={false} index={index} />
<% end %>
<%!-- New attributes --%>
<%= for {%{name: name, value: value}, index} <- Enum.with_index(@new_attributes) do %>
<.live_component module={PropertiesSidebarSectionComponent} id={"new-attribute-section-#{index}"} parent={@myself} name={name} value={value} edit_name={true} index={index} placeholder="Some placeholder"/>
<.live_component module={PropertiesSidebarSectionComponent} id={"new-attribute-section-#{index}"} parent={@myself} name={name} value={value} edit_name={true} index={index} />
<% end %>
<% end %>
<div class="p-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,75 @@ defmodule Beacon.LiveAdmin.PropertiesSidebarSectionComponent do
use Beacon.LiveAdmin.Web, :live_component
require Logger

def handle_event("update_attribute_name", %{"index" => index, "name" => name}, socket) do
Logger.debug("Updating attribute name: #{index} - #{name}")
index = String.to_integer(index)
new_attributes = Enum.map(socket.assigns.new_attributes, fn
{attr, i} when i == index -> %{attr | name: name}
attr -> attr
end)
{:noreply, assign(socket, :new_attributes, new_attributes)}
end
# def handle_event("update_attribute_name", %{"index" => index, "name" => name}, socket) do
# Logger.debug("Updating attribute name: #{index} - #{name}")
# index = String.to_integer(index)
# new_attributes = Enum.map(socket.assigns.new_attributes, fn
# {attr, i} when i == index -> %{attr | name: name}
# attr -> attr
# end)
# {:noreply, assign(socket, :new_attributes, new_attributes)}
# end

# def handle_event("update_attribute_value", %{"index" => index, "value" => value}, socket) do
# Logger.debug("Updating attribute value: #{index} - #{value}")
# index = String.to_integer(index)
# new_attributes = Enum.map(socket.assigns.new_attributes, fn
# {attr, i} when i == index -> %{attr | value: value}
# attr -> attr
# end)
# {:noreply, assign(socket, :new_attributes, new_attributes)}
# end

def handle_event("update_attribute_value", %{"index" => index, "value" => value}, socket) do
Logger.debug("Updating attribute value: #{index} - #{value}")
index = String.to_integer(index)
new_attributes = Enum.map(socket.assigns.new_attributes, fn
{attr, i} when i == index -> %{attr | value: value}
attr -> attr
end)
{:noreply, assign(socket, :new_attributes, new_attributes)}
def handle_event("update_attribute", %{ "name" => name, "value" => value}, socket) do
Logger.debug("Updating attribute: #{name} - #{value}")
Logger.debug("Assigns: #{inspect(socket.assigns)}")
index = socket.assigns.index

Check warning on line 30 in lib/beacon/live_admin/components/properties_sidebar_section_component.ex

View workflow job for this annotation

GitHub Actions / test: OTP 25.1 | Elixir 1.14.0 | Phoenix 1.7.0 | LV 0.20.2 | PG 13.0-alpine

variable "index" is unused (if the variable is not meant to be used, prefix it with an underscore)
# new_attributes = Enum.map(socket.assigns.new_attributes, fn
# {attr, i} when i == index -> %{attr | name: name, value: value}
# attr -> attr
# end)
# {:noreply, assign(socket, :new_attributes, new_attributes)}
{:noreply, socket}
end

def render(assigns) do
~H"""
<section class="p-4 border-b border-b-gray-100 border-solid">
<header class="flex items-center text-sm mb-2 font-medium">
<div class="w-full flex items-center justify-between gap-x-1 p-1 font-semibold group">
<span class="flex-grow">
<span class="hover:text-blue-700 active:text-blue-900">
<%= if @edit_name do %>
<input
type="text"
class="w-full py-1 px-2 bg-gray-100 border-gray-100 rounded-md leading-6 text-sm"
value={@name}
name="name"
phx-input="update_attribute_name"
phx-value-index={@index}
phx-target={@parent}
/>
<% else %>
<%= @name %>
<% end %>
<form phx-change="update_attribute" phx-blur="check_and_save" phx-target={@myself}>
<header class="flex items-center text-sm mb-2 font-medium">
<div class="w-full flex items-center justify-between gap-x-1 p-1 font-semibold group">
<span class="flex-grow">
<span class="hover:text-blue-700 active:text-blue-900">
<%= if @edit_name do %>
<input
type="text"
class="w-full py-1 px-2 bg-gray-100 border-gray-100 rounded-md leading-6 text-sm"
value={@name}
name="name"
phx-input="update_attribute_name"
phx-value-index={@index}
phx-target={@parent}
/>
<% else %>
<%= @name %>
<% end %>
</span>
</span>
</span>
<.delete_button index={@index} parent={@parent}/>
<.toggle_button/>
</div>
</header>
<input
type="text"
class="w-full py-1 px-2 bg-gray-100 border-gray-100 rounded-md leading-6 text-sm"
value={@value}
name="value"
phx-input="update_attribute_value"
phx-value-index={@index}
phx-target={@parent}
/>
<.delete_button index={@index} parent={@parent}/>
<.toggle_button/>
</div>
</header>
<input
type="text"
class="w-full py-1 px-2 bg-gray-100 border-gray-100 rounded-md leading-6 text-sm"
value={@value}
name="value"
phx-input="update_attribute_value"
phx-value-index={@index}
phx-target={@parent}
/>
</form>
</section>
"""
end
Expand Down

0 comments on commit 9dabb8e

Please sign in to comment.