get started on route for creating links, dividing steps into components

This commit is contained in:
2021-08-19 22:35:12 -04:00
parent 1542213875
commit dfaa328af4
24 changed files with 618 additions and 36 deletions

View File

@@ -6,7 +6,29 @@ defmodule Entendu.Application do
use Application
def start(_type, _args) do
children = [
# topologies = [
# chat: [
# strategy: Cluster.Strategy.Gossip
# ]
# ]
topologies = [
k8s_entendu: [
strategy: Elixir.Cluster.Strategy.Kubernetes.DNS,
config: [
service: "entendu-nodes",
application_name: "entendu"
]
]
]
children = case Application.get_env(:frayt_elixir, :enable_k8s) do
true -> [
{Cluster.Supervisor, [topologies, [name: Entendu.ClusterSupervisor]]}
]
_ -> []
end
|> Kernel.++([
# Start the Ecto repository
Entendu.Repo,
# Start the Telemetry supervisor
@@ -17,7 +39,7 @@ defmodule Entendu.Application do
EntenduWeb.Endpoint
# Start a worker by calling: Entendu.Worker.start_link(arg)
# {Entendu.Worker, arg}
]
])
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options

104
lib/entendu/links.ex Normal file
View File

@@ -0,0 +1,104 @@
defmodule Entendu.Links do
@moduledoc """
The Links context.
"""
import Ecto.Query, warn: false
alias Entendu.Repo
alias Entendu.Links.Link
@doc """
Returns the list of links.
## Examples
iex> list_links()
[%Link{}, ...]
"""
def list_links do
Repo.all(Link)
end
@doc """
Gets a single link.
Raises `Ecto.NoResultsError` if the Link does not exist.
## Examples
iex> get_link!(123)
%Link{}
iex> get_link!(456)
** (Ecto.NoResultsError)
"""
def get_link!(id), do: Repo.get!(Link, id)
@doc """
Creates a link.
## Examples
iex> create_link(%{field: value})
{:ok, %Link{}}
iex> create_link(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_link(attrs \\ %{}) do
%Link{}
|> Link.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a link.
## Examples
iex> update_link(link, %{field: new_value})
{:ok, %Link{}}
iex> update_link(link, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_link(%Link{} = link, attrs) do
link
|> Link.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a link.
## Examples
iex> delete_link(link)
{:ok, %Link{}}
iex> delete_link(link)
{:error, %Ecto.Changeset{}}
"""
def delete_link(%Link{} = link) do
Repo.delete(link)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking link changes.
## Examples
iex> change_link(link)
%Ecto.Changeset{data: %Link{}}
"""
def change_link(%Link{} = link, attrs \\ %{}) do
Link.changeset(link, attrs)
end
end

18
lib/entendu/links/link.ex Normal file
View File

@@ -0,0 +1,18 @@
defmodule Entendu.Links.Link do
use Ecto.Schema
import Ecto.Changeset
schema "links" do
field :burn_after_reading, :boolean, default: false
field :expires, :utc_datetime
timestamps()
end
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:expires, :burn_after_reading])
|> validate_required([:expires, :burn_after_reading])
end
end

View File

View File

@@ -83,6 +83,7 @@ defmodule EntenduWeb do
# Import LiveView helpers (live_render, live_component, live_patch, etc)
import Phoenix.LiveView.Helpers
import EntenduWeb.LiveHelpers
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View

View File

@@ -0,0 +1,13 @@
defmodule EntenduWeb.LinkLive.For do
use EntenduWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket |> assign(:step, 1)}
end
def handle_event("next_step", _, %{ assigns: %{ step: step } } = socket) do
socket = socket |> assign(:step, step + 1)
{:noreply, socket}
end
end

View File

@@ -0,0 +1,4 @@
<h1>Create Link</h1>
<%= live_component @socket, EntenduWeb.LinkLive.For.Step1Component, id: "step1", test: "test" %>
On Step: <%= @step %>

View File

@@ -0,0 +1,46 @@
defmodule EntenduWeb.LinkLive.Index do
use EntenduWeb, :live_view
alias Entendu.Links
alias Entendu.Links.Link
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :links, list_links())}
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 Link")
|> assign(:link, Links.get_link!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Link")
|> assign(:link, %Link{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Links")
|> assign(:link, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
link = Links.get_link!(id)
{:ok, _} = Links.delete_link(link)
{:noreply, assign(socket, :links, list_links())}
end
defp list_links do
Links.list_links()
end
end

View File

@@ -0,0 +1,37 @@
<h1>Listing Links</h1>
<%= if @live_action in [:new, :edit] do %>
<%= live_modal @socket, EntenduWeb.LinkLive.FormComponent,
id: @link.id || :new,
title: @page_title,
action: @live_action,
link: @link,
return_to: Routes.link_index_path(@socket, :index) %>
<% end %>
<table>
<thead>
<tr>
<th>Expires</th>
<th>Burn after reading</th>
<th></th>
</tr>
</thead>
<tbody id="links">
<%= for link <- @links do %>
<tr id="link-<%= link.id %>">
<td><%= link.expires %></td>
<td><%= link.burn_after_reading %></td>
<td>
<span><%= live_redirect "Show", to: Routes.link_show_path(@socket, :show, link) %></span>
<span><%= live_patch "Edit", to: Routes.link_index_path(@socket, :edit, link) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: link.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Link", to: Routes.link_index_path(@socket, :new) %></span>

View File

@@ -0,0 +1,4 @@
defmodule EntenduWeb.LinkLive.For.Step1Component do
use EntenduWeb, :live_component
end

View File

@@ -0,0 +1,3 @@
This is step 1. <%= @test %>
<button phx-click="next_step">Next Step</button>

View File

@@ -0,0 +1,23 @@
defmodule EntenduWeb.LiveHelpers do
import Phoenix.LiveView.Helpers
@doc """
Renders a component inside the `EntenduWeb.ModalComponent` component.
The rendered modal receives a `:return_to` option to properly update
the URL when the modal is closed.
## Examples
<%= live_modal @socket, EntenduWeb.LinkLive.FormComponent,
id: @link.id || :new,
action: @live_action,
link: @link,
return_to: Routes.link_index_path(@socket, :index) %>
"""
def live_modal(socket, component, opts) do
path = Keyword.fetch!(opts, :return_to)
modal_opts = [id: :modal, return_to: path, component: component, opts: opts]
live_component(socket, EntenduWeb.ModalComponent, modal_opts)
end
end

View File

@@ -0,0 +1,26 @@
defmodule EntenduWeb.ModalComponent do
use EntenduWeb, :live_component
@impl true
def render(assigns) do
~L"""
<div id="<%= @id %>" class="phx-modal"
phx-capture-click="close"
phx-window-keydown="close"
phx-key="escape"
phx-target="#<%= @id %>"
phx-page-loading>
<div class="phx-modal-content">
<%= live_patch raw("&times;"), to: @return_to, class: "phx-modal-close" %>
<%= live_component @socket, @component, @opts %>
</div>
</div>
"""
end
@impl true
def handle_event("close", _, socket) do
{:noreply, push_patch(socket, to: socket.assigns.return_to)}
end
end

View File

@@ -14,35 +14,5 @@
</section>
<section class="row">
<article class="column">
<h2>Resources</h2>
<ul>
<li>
<a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix">Source</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix/blob/v1.5/CHANGELOG.md">v1.5 Changelog</a>
</li>
</ul>
</article>
<article class="column">
<h2>Help</h2>
<ul>
<li>
<a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
</li>
<li>
<a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
</li>
<li>
<a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
</li>
<li>
<a href="https://elixir-slackin.herokuapp.com/">Elixir on Slack</a>
</li>
</ul>
</article>
<a href="/links">Links</a>
</section>

View File

@@ -18,6 +18,7 @@ defmodule EntenduWeb.Router do
pipe_through :browser
live "/", PageLive, :index
live "/for", LinkLive.For
end
# Other scopes may use custom stacks.