diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:55:12 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-04-06 12:55:12 -0600 |
commit | 4067339e8cf9dbd624a8fa0183d7a29c73b2e762 (patch) | |
tree | 4f995e706c10bf5b9a58c078cfb843c76e31c8a0 /lib/aggiedit_web/live/post_live | |
parent | 66d871e5461814dad58872eb832a58f2c3c5111b (diff) | |
download | aggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.tar.gz aggiedit-4067339e8cf9dbd624a8fa0183d7a29c73b2e762.zip |
Models for upload and post; generated liveview controller for posts
Diffstat (limited to 'lib/aggiedit_web/live/post_live')
-rw-r--r-- | lib/aggiedit_web/live/post_live/form_component.ex | 55 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/form_component.html.heex | 24 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/index.ex | 46 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/index.html.heex | 41 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/show.ex | 21 | ||||
-rw-r--r-- | lib/aggiedit_web/live/post_live/show.html.heex | 31 |
6 files changed, 218 insertions, 0 deletions
diff --git a/lib/aggiedit_web/live/post_live/form_component.ex b/lib/aggiedit_web/live/post_live/form_component.ex new file mode 100644 index 0000000..11ad4a1 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/form_component.ex @@ -0,0 +1,55 @@ +defmodule AggieditWeb.PostLive.FormComponent do + use AggieditWeb, :live_component + + alias Aggiedit.Rooms + + @impl true + def update(%{post: post} = assigns, socket) do + changeset = Rooms.change_post(post) + + {:ok, + socket + |> assign(assigns) + |> assign(:changeset, changeset)} + end + + @impl true + def handle_event("validate", %{"post" => post_params}, socket) do + changeset = + socket.assigns.post + |> Rooms.change_post(post_params) + |> Map.put(:action, :validate) + + {:noreply, assign(socket, :changeset, changeset)} + end + + def handle_event("save", %{"post" => post_params}, socket) do + save_post(socket, socket.assigns.action, post_params) + end + + defp save_post(socket, :edit, post_params) do + case Rooms.update_post(socket.assigns.post, post_params) do + {:ok, _post} -> + {:noreply, + socket + |> put_flash(:info, "Post updated successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, :changeset, changeset)} + end + end + + defp save_post(socket, :new, post_params) do + case Rooms.create_post(post_params) do + {:ok, _post} -> + {:noreply, + socket + |> put_flash(:info, "Post created successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, changeset: changeset)} + end + end +end diff --git a/lib/aggiedit_web/live/post_live/form_component.html.heex b/lib/aggiedit_web/live/post_live/form_component.html.heex new file mode 100644 index 0000000..682ec78 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/form_component.html.heex @@ -0,0 +1,24 @@ +<div> + <h2><%= @title %></h2> + + <.form + let={f} + for={@changeset} + id="post-form" + phx-target={@myself} + phx-change="validate" + phx-submit="save"> + + <%= label f, :title %> + <%= textarea f, :title %> + <%= error_tag f, :title %> + + <%= label f, :body %> + <%= textarea f, :body %> + <%= error_tag f, :body %> + + <div> + <%= submit "Save", phx_disable_with: "Saving..." %> + </div> + </.form> +</div> diff --git a/lib/aggiedit_web/live/post_live/index.ex b/lib/aggiedit_web/live/post_live/index.ex new file mode 100644 index 0000000..0b12ed0 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/index.ex @@ -0,0 +1,46 @@ +defmodule AggieditWeb.PostLive.Index do + use AggieditWeb, :live_view + + alias Aggiedit.Rooms + alias Aggiedit.Rooms.Post + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, :posts, list_posts())} + 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 Post") + |> assign(:post, Rooms.get_post!(id)) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Post") + |> assign(:post, %Post{}) + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Listing Posts") + |> assign(:post, nil) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + post = Rooms.get_post!(id) + {:ok, _} = Rooms.delete_post(post) + + {:noreply, assign(socket, :posts, list_posts())} + end + + defp list_posts do + Rooms.list_posts() + end +end diff --git a/lib/aggiedit_web/live/post_live/index.html.heex b/lib/aggiedit_web/live/post_live/index.html.heex new file mode 100644 index 0000000..4297d5a --- /dev/null +++ b/lib/aggiedit_web/live/post_live/index.html.heex @@ -0,0 +1,41 @@ +<h1>Listing Posts</h1> + +<%= if @live_action in [:new, :edit] do %> + <.modal return_to={Routes.post_index_path(@socket, :index)}> + <.live_component + module={AggieditWeb.PostLive.FormComponent} + id={@post.id || :new} + title={@page_title} + action={@live_action} + post={@post} + return_to={Routes.post_index_path(@socket, :index)} + /> + </.modal> +<% end %> + +<table> + <thead> + <tr> + <th>Title</th> + <th>Body</th> + + <th></th> + </tr> + </thead> + <tbody id="posts"> + <%= for post <- @posts do %> + <tr id={"post-#{post.id}"}> + <td><%= post.title %></td> + <td><%= post.body %></td> + + <td> + <span><%= live_redirect "Show", to: Routes.post_show_path(@socket, :show, post) %></span> + <span><%= live_patch "Edit", to: Routes.post_index_path(@socket, :edit, post) %></span> + <span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: post.id, data: [confirm: "Are you sure?"] %></span> + </td> + </tr> + <% end %> + </tbody> +</table> + +<span><%= live_patch "New Post", to: Routes.post_index_path(@socket, :new) %></span> diff --git a/lib/aggiedit_web/live/post_live/show.ex b/lib/aggiedit_web/live/post_live/show.ex new file mode 100644 index 0000000..2416156 --- /dev/null +++ b/lib/aggiedit_web/live/post_live/show.ex @@ -0,0 +1,21 @@ +defmodule AggieditWeb.PostLive.Show do + use AggieditWeb, :live_view + + alias Aggiedit.Rooms + + @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(:post, Rooms.get_post!(id))} + end + + defp page_title(:show), do: "Show Post" + defp page_title(:edit), do: "Edit Post" +end diff --git a/lib/aggiedit_web/live/post_live/show.html.heex b/lib/aggiedit_web/live/post_live/show.html.heex new file mode 100644 index 0000000..e6eaebe --- /dev/null +++ b/lib/aggiedit_web/live/post_live/show.html.heex @@ -0,0 +1,31 @@ +<h1>Show Post</h1> + +<%= if @live_action in [:edit] do %> + <.modal return_to={Routes.post_show_path(@socket, :show, @post)}> + <.live_component + module={AggieditWeb.PostLive.FormComponent} + id={@post.id} + title={@page_title} + action={@live_action} + post={@post} + return_to={Routes.post_show_path(@socket, :show, @post)} + /> + </.modal> +<% end %> + +<ul> + + <li> + <strong>Title:</strong> + <%= @post.title %> + </li> + + <li> + <strong>Body:</strong> + <%= @post.body %> + </li> + +</ul> + +<span><%= live_patch "Edit", to: Routes.post_show_path(@socket, :edit, @post), class: "button" %></span> | +<span><%= live_redirect "Back", to: Routes.post_index_path(@socket, :index) %></span> |