-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for creating project items
- Loading branch information
1 parent
1879be0
commit 8a79af0
Showing
16 changed files
with
382 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
defmodule Systems.Project.CreateItemPopup do | ||
use CoreWeb, :live_component | ||
|
||
alias Frameworks.Pixel.Selector | ||
|
||
alias Systems.{ | ||
Project | ||
} | ||
|
||
# Handle Tool Type Selector Update | ||
@impl true | ||
def update( | ||
%{active_item_id: active_item_id, selector_id: :tool_selector}, | ||
%{assigns: %{tool_labels: tool_labels}} = socket | ||
) do | ||
%{id: selected_tool} = Enum.find(tool_labels, &(&1.id == active_item_id)) | ||
|
||
{ | ||
:ok, | ||
socket | ||
|> assign(selected_tool: selected_tool) | ||
} | ||
end | ||
|
||
# Initial Update | ||
@impl true | ||
def update(%{id: id, node: node, target: target}, socket) do | ||
title = dgettext("eyra-project", "create.item.title") | ||
|
||
{ | ||
:ok, | ||
socket | ||
|> assign(id: id, node: node, target: target, title: title) | ||
|> init_tools() | ||
|> init_buttons() | ||
} | ||
end | ||
|
||
defp init_tools(socket) do | ||
selected_tool = :empty | ||
tool_labels = Project.Tools.labels(selected_tool) | ||
socket |> assign(tool_labels: tool_labels, selected_tool: selected_tool) | ||
end | ||
|
||
defp init_buttons(%{assigns: %{myself: myself}} = socket) do | ||
socket | ||
|> assign( | ||
buttons: [ | ||
%{ | ||
action: %{type: :send, event: "proceed", target: myself}, | ||
face: %{ | ||
type: :primary, | ||
label: dgettext("eyra-project", "create.proceed.button") | ||
} | ||
}, | ||
%{ | ||
action: %{type: :send, event: "cancel", target: myself}, | ||
face: %{type: :label, label: dgettext("eyra-ui", "cancel.button")} | ||
} | ||
] | ||
) | ||
end | ||
|
||
@impl true | ||
def handle_event( | ||
"proceed", | ||
_, | ||
%{assigns: %{selected_tool: selected_tool}} = socket | ||
) do | ||
create_item(socket, selected_tool) | ||
|
||
{:noreply, socket |> close()} | ||
end | ||
|
||
@impl true | ||
def handle_event("cancel", _, socket) do | ||
{:noreply, socket |> close()} | ||
end | ||
|
||
defp close(%{assigns: %{target: target}} = socket) do | ||
update_target(target, %{module: __MODULE__, action: :close}) | ||
socket | ||
end | ||
|
||
defp create_item(%{assigns: %{node: node}}, tool) do | ||
name = Project.Tools.translate(tool) | ||
Project.Assembly.create_item(name, node, tool) | ||
end | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div> | ||
<Text.title3><%= @title %></Text.title3> | ||
<.spacing value="S" /> | ||
<.live_component | ||
module={Selector} | ||
id={:tool_selector} | ||
items={@tool_labels} | ||
type={:radio} | ||
optional?={false} | ||
parent={%{type: __MODULE__, id: @id}} | ||
/> | ||
<.spacing value="M" /> | ||
<div class="flex flex-row gap-4"> | ||
<%= for button <- @buttons do %> | ||
<Button.dynamic {button} /> | ||
<% end %> | ||
</div> | ||
</div> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule Systems.Project.ItemForm do | ||
use CoreWeb.LiveForm | ||
|
||
alias Systems.{ | ||
Project | ||
} | ||
|
||
# Handle initial update | ||
@impl true | ||
def update( | ||
%{id: id, entity: item, target: target}, | ||
socket | ||
) do | ||
changeset = Project.ItemModel.changeset(item, %{}) | ||
|
||
close_button = %{ | ||
action: %{type: :send, event: "close"}, | ||
face: %{type: :icon, icon: :close} | ||
} | ||
|
||
{ | ||
:ok, | ||
socket | ||
|> assign( | ||
id: id, | ||
entity: item, | ||
target: target, | ||
close_button: close_button, | ||
changeset: changeset | ||
) | ||
} | ||
end | ||
|
||
# Handle Events | ||
@impl true | ||
def handle_event("close", _params, socket) do | ||
send(self(), %{module: __MODULE__, action: :close}) | ||
{:noreply, socket} | ||
end | ||
|
||
@impl true | ||
def handle_event("save", %{"item_model" => attrs}, %{assigns: %{entity: entity}} = socket) do | ||
{ | ||
:noreply, | ||
socket | ||
|> save(entity, attrs) | ||
} | ||
end | ||
|
||
# Saving | ||
|
||
def save(socket, entity, attrs) do | ||
changeset = Project.ItemModel.changeset(entity, attrs) | ||
|
||
socket | ||
|> save(changeset) | ||
end | ||
|
||
@impl true | ||
def render(assigns) do | ||
~H""" | ||
<div> | ||
<div class="flex flex-row"> | ||
<div> | ||
<Text.title3><%= dgettext("eyra-project", "item.form.title") %></Text.title3> | ||
</div> | ||
<div class="flex-grow" /> | ||
<Button.dynamic {@close_button} /> | ||
</div> | ||
<.form id={@id} :let={form} for={@changeset} phx-change="save" phx-target={@myself} > | ||
<.text_input form={form} field={:name} label_text={dgettext("eyra-project", "item.form.name.label")} /> | ||
</.form> | ||
</div> | ||
""" | ||
end | ||
end |
Oops, something went wrong.