mitgliederverwaltung/lib/mv_web/property_live/index.ex
Moritz b91df051dc
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
WIP: add liveview
2025-05-07 19:23:40 +02:00

99 lines
2.5 KiB
Elixir

defmodule MvWeb.PropertyLive.Index do
use MvWeb, :live_view
@impl true
def render(assigns) do
~H"""
<.header>
Listing Properties
<:actions>
<.link patch={~p"/properties/new"}>
<.button>New Property</.button>
</.link>
</:actions>
</.header>
<.table
id="properties"
rows={@streams.properties}
row_click={fn {_id, property} -> JS.navigate(~p"/properties/#{property}") end}
>
<:col :let={{_id, property}} label="Id">{property.id}</:col>
<:action :let={{_id, property}}>
<div class="sr-only">
<.link navigate={~p"/properties/#{property}"}>Show</.link>
</div>
<.link patch={~p"/properties/#{property}/edit"}>Edit</.link>
</:action>
<:action :let={{id, property}}>
<.link
phx-click={JS.push("delete", value: %{id: property.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>
<.modal
:if={@live_action in [:new, :edit]}
id="property-modal"
show
on_cancel={JS.patch(~p"/properties")}
>
<.live_component
module={MvWeb.PropertyLive.FormComponent}
id={(@property && @property.id) || :new}
title={@page_title}
action={@live_action}
property={@property}
patch={~p"/properties"}
/>
</.modal>
"""
end
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :properties, Ash.read!(Mv.Membership.Property))}
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 Property")
|> assign(:property, Ash.get!(Mv.Membership.Property, id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Property")
|> assign(:property, nil)
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Properties")
|> assign(:property, nil)
end
@impl true
def handle_info({MvWeb.PropertyLive.FormComponent, {:saved, property}}, socket) do
{:noreply, stream_insert(socket, :properties, property)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
property = Ash.get!(Mv.Membership.Property, id)
Ash.destroy!(property)
{:noreply, stream_delete(socket, :properties, property)}
end
end