Initial commit

This commit is contained in:
2022-05-23 15:57:15 +02:00
commit 5608d2e5ec
99 changed files with 5636 additions and 0 deletions

View File

@ -0,0 +1,55 @@
defmodule SomethingErlangWeb.ThreadLive.FormComponent do
use SomethingErlangWeb, :live_component
alias SomethingErlang.Forums
@impl true
def update(%{thread: thread} = assigns, socket) do
changeset = Forums.change_thread(thread)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"thread" => thread_params}, socket) do
changeset =
socket.assigns.thread
|> Forums.change_thread(thread_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"thread" => thread_params}, socket) do
save_thread(socket, socket.assigns.action, thread_params)
end
defp save_thread(socket, :edit, thread_params) do
case Forums.update_thread(socket.assigns.thread, thread_params) do
{:ok, _thread} ->
{:noreply,
socket
|> put_flash(:info, "Thread updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_thread(socket, :new, thread_params) do
case Forums.create_thread(thread_params) do
{:ok, _thread} ->
{:noreply,
socket
|> put_flash(:info, "Thread created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,24 @@
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="thread-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :title %>
<%= text_input f, :title %>
<%= error_tag f, :title %>
<%= label f, :thread_id %>
<%= number_input f, :thread_id %>
<%= error_tag f, :thread_id %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

@ -0,0 +1,46 @@
defmodule SomethingErlangWeb.ThreadLive.Index do
use SomethingErlangWeb, :live_view
alias SomethingErlang.Forums
alias SomethingErlang.Forums.Thread
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :threads, list_threads())}
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 Thread")
|> assign(:thread, Forums.get_thread!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Thread")
|> assign(:thread, %Thread{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Threads")
|> assign(:thread, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
thread = Forums.get_thread!(id)
{:ok, _} = Forums.delete_thread(thread)
{:noreply, assign(socket, :threads, list_threads())}
end
defp list_threads do
Forums.list_threads()
end
end

View File

@ -0,0 +1,41 @@
<h1>Listing Threads</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.thread_index_path(@socket, :index)}>
<.live_component
module={SomethingErlangWeb.ThreadLive.FormComponent}
id={@thread.id || :new}
title={@page_title}
action={@live_action}
thread={@thread}
return_to={Routes.thread_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Thread</th>
<th></th>
</tr>
</thead>
<tbody id="threads">
<%= for thread <- @threads do %>
<tr id={"thread-#{thread.id}"}>
<td><%= thread.title %></td>
<td><%= thread.thread_id %></td>
<td>
<span><%= live_redirect "Show", to: Routes.thread_show_path(@socket, :show, thread) %></span>
<span><%= live_patch "Edit", to: Routes.thread_index_path(@socket, :edit, thread) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: thread.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Thread", to: Routes.thread_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule SomethingErlangWeb.ThreadLive.Show do
use SomethingErlangWeb, :live_view
alias SomethingErlang.Forums
@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(:thread, Forums.get_thread!(id))}
end
defp page_title(:show), do: "Show Thread"
defp page_title(:edit), do: "Edit Thread"
end

View File

@ -0,0 +1,31 @@
<h1>Show Thread</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.thread_show_path(@socket, :show, @thread)}>
<.live_component
module={SomethingErlangWeb.ThreadLive.FormComponent}
id={@thread.id}
title={@page_title}
action={@live_action}
thread={@thread}
return_to={Routes.thread_show_path(@socket, :show, @thread)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @thread.title %>
</li>
<li>
<strong>Thread:</strong>
<%= @thread.thread_id %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.thread_show_path(@socket, :edit, @thread), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.thread_index_path(@socket, :index) %></span>