machine form basix, fought a typo in my schema for too long :^)

d9e496f · gmorell · 2022-12-16 07:23

15 files +89 -4

Files changed

deleted lib/lambent_ex/schema/lambent_ex/schema/machine.ex
+0 −3
@@ -1,3 +0,0 @@
1 −defmodule LambentEx.Schema.Machines do
2 − @moduledoc false
3 −end
renamed lib/lambent_ex/schema/lambent_ex/schema/links.ex → lib/lambent_ex/schema/links.ex
+1 −0
@@ -40,3 +40,4 @@ defmodule LambentEx.Schema.Links do
40 40 |> validate_required([:name, :source_id, :source_type, :target_id, :target_type])
41 41 end
42 42 end
43 +
added lib/lambent_ex/schema/machines.ex
+22 −0
@@ -0,0 +1,22 @@
1 +defmodule LambentEx.Schema.Machines do
2 + @moduledoc false
3 +
4 + use Ecto.Schema
5 + import Ecto.Changeset
6 +
7 + alias LambentEx.Schema.Machines
8 +
9 + embedded_schema do
10 + field :name, :string
11 + end
12 +
13 + def changeset(source, params) do
14 + source
15 + |> cast(params, ~w(name)a)
16 + |> validate_required([:name])
17 + end
18 +
19 + def change_machine(%Machines{} = machine, attrs \\ %{}) do
20 + Machines.changeset(machine, attrs)
21 + end
22 +end
added lib/lambent_ex_web/live/machines/form_component.ex
+17 −0
@@ -0,0 +1,17 @@
1 +defmodule LambentEx.MachinesLive.FormComponent do
2 + use LambentExWeb, :live_component
3 +
4 + alias LambentEx.Schema.Machines
5 + alias LambentEx.Names
6 +
7 + @impl true
8 + def update(%{machine: machine} = assigns, socket) do
9 + changeset = Machines.change_machine(machine)
10 +
11 + {:ok,
12 + socket
13 + |> assign(assigns)
14 + |> assign(:changeset, changeset)}
15 + end
16 +
17 +end
added lib/lambent_ex_web/live/machines/form_component.html.heex
+17 −0
@@ -0,0 +1,17 @@
1 +<div>
2 + <h1 class="text-3xl font-bold">New Machine</h1>
3 + <.simple_form
4 + :let={f}
5 + for={@changeset}
6 + id="machines-form"
7 + phx-target={@myself}
8 + phx-change="validate"
9 + phx-submit="save"
10 + >
11 +
12 +
13 +<:actions>
14 + <.button phx-disable-with="Saving...">Create Machine</.button>
15 +</:actions>
16 +</.simple_form>
17 +</div>
modified lib/lambent_ex_web/live/machines/index.ex
+16 −1
@@ -27,7 +27,14 @@ defmodule LambentExWeb.MachinesLive.Index do
27 27 @impl true
28 28 def mount(_params, _session, socket) do
29 29 Phoenix.PubSub.subscribe(@pubsub_name, "machines_idx")
30 − {:ok, socket |> assign(:machines, %{})}
30 + {:ok, socket
31 + |> assign(:machine, nil)
32 + |> assign(:machines, %{})
33 + |> assign(:nonew, true)
34 + |> assign(:newctrl, [
35 + %{link: ~p"/cfg/machines/new", icon: "plus"},
36 + ])
37 + }
31 38 end
32 39
33 40 @impl true
@@ -42,6 +49,14 @@ defmodule LambentExWeb.MachinesLive.Index do
42 49 |> assign(:source, nil)
43 50 end
44 51
52 + defp apply_action(socket, :new, _params) do
53 + socket
54 + |> assign(:id, :new)
55 + |> assign(:page_link, "Machines")
56 + |> assign(:page_title, "New Machine")
57 + |> assign(:machine, %LambentEx.Schema.Machines{})
58 + end
59 +
45 60 def handle_info({:machines_pub, machine}, socket) do
46 61 # IO.inspect(machine)
47 62 {:noreply, socket |> assign(:machines, socket.assigns.machines |> Map.put(machine[:id], machine))}
modified lib/lambent_ex_web/live/machines/index.html.heex
+16 −0
@@ -1,4 +1,20 @@
1 1 <div class="overflow-x-auto">
2 + <%= if @live_action in [:new, :edit] do %>
3 + <.modal return_to={~p"/cfg/machines"} id="new"
4 + :if={@live_action in [:new, :edit]}
5 + show
6 + on_cancel={JS.navigate(~p"/cfg/machines")}>
7 + <.live_component
8 + module={LambentEx.MachinesLive.FormComponent}
9 + id={:new}
10 + title={@page_title}
11 + action={@live_action}
12 + machine={@machine}
13 + return_to={~p"/cfg/machines"}
14 + navigate={~p"/cfg/machines"}
15 + />
16 + </.modal>
17 + <% end %>
2 18 <table class="table table-zebra w-full">
3 19 <!-- head -->
4 20 <thead>

Parents: c7914ae