42 lines
1.0 KiB
Elixir
42 lines
1.0 KiB
Elixir
defmodule SendItWeb.ContactLive.Index do
|
|
use SendItWeb, :live_view
|
|
|
|
alias SendIt.Marketing
|
|
alias SendIt.Marketing.Contact
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
{:ok, stream(socket, :contacts, Marketing.list_contacts())}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(params, _url, socket) do
|
|
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
|
end
|
|
|
|
defp apply_action(socket, :new, _params) do
|
|
socket
|
|
|> assign(:page_title, "New Contact")
|
|
|> assign(:contact, %Contact{})
|
|
end
|
|
|
|
defp apply_action(socket, :index, _params) do
|
|
socket
|
|
|> assign(:page_title, "Listing Contacts")
|
|
|> assign(:contact, nil)
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({SendItWeb.ContactLive.FormComponent, {:saved, contact}}, socket) do
|
|
{:noreply, stream_insert(socket, :contacts, contact)}
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("delete", %{"id" => id}, socket) do
|
|
contact = Marketing.get_contact!(id)
|
|
{:ok, _} = Marketing.delete_contact(contact)
|
|
|
|
{:noreply, stream_delete(socket, :contacts, contact)}
|
|
end
|
|
end
|