init commit
This commit is contained in:
83
lib/here_i_am_web/live/device_live/form_component.ex
Normal file
83
lib/here_i_am_web/live/device_live/form_component.ex
Normal file
@@ -0,0 +1,83 @@
|
||||
defmodule HereIAmWeb.DeviceLive.FormComponent do
|
||||
use HereIAmWeb, :live_component
|
||||
|
||||
alias HereIAm.Devices
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
<%= @title %>
|
||||
<:subtitle>Use this form to manage device records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="device-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={@form[:ip_address]} type="text" label="IP Address" />
|
||||
<.input field={@form[:audio]} type="text" label="Audio" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Device</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{device: device} = assigns, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_new(:form, fn ->
|
||||
to_form(Devices.change_device(device))
|
||||
end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"device" => device_params}, socket) do
|
||||
changeset = Devices.change_device(socket.assigns.device, device_params)
|
||||
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"device" => device_params}, socket) do
|
||||
save_device(socket, socket.assigns.action, device_params)
|
||||
end
|
||||
|
||||
defp save_device(socket, :edit, device_params) do
|
||||
case Devices.update_device(socket.assigns.device, device_params) do
|
||||
{:ok, device} ->
|
||||
notify_parent({:saved, device})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Device updated successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_device(socket, :new, device_params) do
|
||||
case Devices.create_device(device_params) do
|
||||
{:ok, device} ->
|
||||
notify_parent({:saved, device})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Device created successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
end
|
47
lib/here_i_am_web/live/device_live/index.ex
Normal file
47
lib/here_i_am_web/live/device_live/index.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule HereIAmWeb.DeviceLive.Index do
|
||||
use HereIAmWeb, :live_view
|
||||
|
||||
alias HereIAm.Devices
|
||||
alias HereIAm.Devices.Device
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, stream(socket, :devices, Devices.list_devices())}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Device")
|
||||
|> assign(:device, Devices.get_device!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Device")
|
||||
|> assign(:device, %Device{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Devices")
|
||||
|> assign(:device, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({HereIAmWeb.DeviceLive.FormComponent, {:saved, device}}, socket) do
|
||||
{:noreply, stream_insert(socket, :devices, device)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
device = Devices.get_device!(id)
|
||||
{:ok, _} = Devices.delete_device(device)
|
||||
|
||||
{:noreply, stream_delete(socket, :devices, device)}
|
||||
end
|
||||
end
|
47
lib/here_i_am_web/live/device_live/index.html.heex
Normal file
47
lib/here_i_am_web/live/device_live/index.html.heex
Normal file
@@ -0,0 +1,47 @@
|
||||
<.header>
|
||||
Listing Devices
|
||||
<:actions>
|
||||
<.link patch={~p"/devices/new"}>
|
||||
<.button>New Device</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="devices"
|
||||
rows={@streams.devices}
|
||||
row_click={fn {_id, device} -> JS.navigate(~p"/devices/#{device}") end}
|
||||
>
|
||||
<:col :let={{_id, device}} label="IP Address"><%= device.ip_address %></:col>
|
||||
<:col :let={{_id, device}} label="Audio"><%= device.audio %></:col>
|
||||
<:action :let={{_id, device}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/devices/#{device}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/devices/#{device}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={{id, device}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: device.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal
|
||||
:if={@live_action in [:new, :edit]}
|
||||
id="device-modal"
|
||||
show
|
||||
on_cancel={JS.patch(~p"/devices")}
|
||||
>
|
||||
<.live_component
|
||||
module={HereIAmWeb.DeviceLive.FormComponent}
|
||||
id={@device.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
device={@device}
|
||||
patch={~p"/devices"}
|
||||
/>
|
||||
</.modal>
|
21
lib/here_i_am_web/live/device_live/show.ex
Normal file
21
lib/here_i_am_web/live/device_live/show.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule HereIAmWeb.DeviceLive.Show do
|
||||
use HereIAmWeb, :live_view
|
||||
|
||||
alias HereIAm.Devices
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:device, Devices.get_device!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Device"
|
||||
defp page_title(:edit), do: "Edit Device"
|
||||
end
|
32
lib/here_i_am_web/live/device_live/show.html.heex
Normal file
32
lib/here_i_am_web/live/device_live/show.html.heex
Normal file
@@ -0,0 +1,32 @@
|
||||
<.header>
|
||||
Device <%= @device.id %>
|
||||
<:subtitle>This is a device record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/devices/#{@device}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit device</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="IP Address"><%= @device.ip_address %></:item>
|
||||
<:item title="Audio"><%= @device.audio %></:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/devices"}>Back to devices</.back>
|
||||
|
||||
<.modal
|
||||
:if={@live_action == :edit}
|
||||
id="device-modal"
|
||||
show
|
||||
on_cancel={JS.patch(~p"/devices/#{@device}")}
|
||||
>
|
||||
<.live_component
|
||||
module={HereIAmWeb.DeviceLive.FormComponent}
|
||||
id={@device.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
device={@device}
|
||||
patch={~p"/devices/#{@device}"}
|
||||
/>
|
||||
</.modal>
|
Reference in New Issue
Block a user