Starting to work
This commit is contained in:
parent
a06b7a9367
commit
6306fbe175
189
friends/lib/friends_web/live/friend.ex
Normal file
189
friends/lib/friends_web/live/friend.ex
Normal file
@ -0,0 +1,189 @@
|
||||
defmodule FriendsWeb.FriendLive.Friend do
|
||||
use FriendsWeb, :live_view
|
||||
use Phoenix.HTML
|
||||
import Helpers
|
||||
import Helpers.Names
|
||||
alias Friends.{Friend,Relationship}
|
||||
alias FriendsWeb.FriendLive.Components
|
||||
alias FriendsWeb.Router.Helpers, as: Routes
|
||||
|
||||
# Initialize variables on first load
|
||||
def mount(%{}, _token, socket) do
|
||||
{:ok, socket
|
||||
|> title("New Friend")
|
||||
|> assign(:changeset, %Friend{} |> Friend.changeset)
|
||||
}
|
||||
end
|
||||
|
||||
# Show Friend
|
||||
def handle_params(%{"slug" => slug} = attrs, _token, socket) do
|
||||
friend = Friend.get_by_slug(slug)
|
||||
|
||||
page = if (attrs |> Map.get("page")) in ["overview", "calendar", "relationships"] do
|
||||
attrs["page"]
|
||||
else
|
||||
"overview"
|
||||
end
|
||||
|
||||
{:noreply, socket
|
||||
|> title(friend.name <> " - " <> (page |> :string.titlecase()))
|
||||
|> assign_friend(friend)
|
||||
|> page_view(page)
|
||||
|> assign(:action, Routes.friend_path(socket, :update, friend.id))
|
||||
}
|
||||
end
|
||||
|
||||
# New Friend
|
||||
def handle_params(attrs, _token, socket) do
|
||||
friend = Friend.new
|
||||
{:noreply, socket
|
||||
|> title("New Friend")
|
||||
|> assign_friend(friend)
|
||||
|> page_view("overview")
|
||||
|> assign(:action,Routes.friend_path(socket, :create))
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
# Handle form validation
|
||||
def handle_event("validate", %{"friend" => form_params}, %{assigns: %{friend: friend}} = socket) do
|
||||
|
||||
id = form_params["id"]
|
||||
name = form_params["name"]
|
||||
nickname = form_params["nickname"]
|
||||
born = form_params["born"]
|
||||
email = form_params["email"]
|
||||
phone = form_params["phone"] |> format_phone
|
||||
|
||||
new_params = %{
|
||||
id: id,
|
||||
name: name,
|
||||
nickname: nickname,
|
||||
slug: friend.slug,
|
||||
born: born,
|
||||
phone: phone,
|
||||
email: email
|
||||
}
|
||||
|
||||
changeset = %Friend{}
|
||||
|> Friend.changeset(new_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{
|
||||
:noreply, socket
|
||||
|> assign(:changeset, changeset)
|
||||
|> assign_friend(friend |> struct(new_params), changeset)
|
||||
}
|
||||
end
|
||||
|
||||
# Handle form saving
|
||||
def handle_event("save", %{"friend" => form_params},%{assigns: %{changeset: changeset}} = socket) do
|
||||
|
||||
name = form_params["name"]
|
||||
nickname = form_params["nickname"]
|
||||
born = form_params["born"]
|
||||
email = form_params["email"]
|
||||
phone = form_params["phone"] |> format_phone
|
||||
id = form_params["id"]
|
||||
|
||||
new_params = %{
|
||||
id: id,
|
||||
name: name,
|
||||
nickname: nickname,
|
||||
slug: name |> to_slug,
|
||||
born: born,
|
||||
phone: phone,
|
||||
email: email
|
||||
}
|
||||
|
||||
updated_friend = Friend.create_or_update(new_params)
|
||||
new_changeset = updated_friend |> Friend.changeset
|
||||
|
||||
{
|
||||
:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Saved #{updated_friend |> first_name}!")
|
||||
|> assign(:new_friend, new_changeset)
|
||||
|> assign(:friend, updated_friend)
|
||||
|> push_patch(to: "/friend/#{updated_friend.slug}")
|
||||
}
|
||||
end
|
||||
|
||||
# Handle deleting a friend
|
||||
def handle_event("delete", %{"friend_id" => friend_id}, socket) do
|
||||
|
||||
friend = Friend.get_by_id(friend_id)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "Deleted '#{friend.name}'.")
|
||||
|> push_navigate(to: "/")
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
# Set page title
|
||||
def title(socket, title) do
|
||||
socket |> assign(:page_title, title)
|
||||
end
|
||||
|
||||
# Set variables on page: friend, changeset, relationships
|
||||
def assign_friend(socket, friend) do
|
||||
socket
|
||||
|> assign(:friend, friend)
|
||||
|> assign(:changeset, friend |> Friend.changeset)
|
||||
|> assign(:relationships, friend.relationships)
|
||||
end
|
||||
|
||||
# Same thing, but this time we have a changeset we want to keep
|
||||
def assign_friend(socket, friend, changeset) do
|
||||
socket
|
||||
|> assign(:friend, friend)
|
||||
|> assign(:changeset, changeset)
|
||||
|> assign(:relationships, friend.relationships)
|
||||
end
|
||||
|
||||
# Set page_view variable
|
||||
def page_view(socket, page) do
|
||||
socket |> assign(:page_view, page)
|
||||
end
|
||||
|
||||
# Route to the right sub-template in Components/Components.ex
|
||||
def content(assigns) do
|
||||
~H"""
|
||||
<%= if @live_action != :new do %>
|
||||
<%= FriendsWeb.FriendLive.Friend.menu(assigns) %>
|
||||
<% end %>
|
||||
<%= cond do %>
|
||||
<% @live_action == :show -> %>
|
||||
<%= header(assigns) %>
|
||||
<%= @page_view |> Components.show_page(assigns) %>
|
||||
<% @live_action in [:edit, :new] -> %>
|
||||
<%= @page_view |> Components.form_page(assigns) %>
|
||||
<% end %>
|
||||
"""
|
||||
end
|
||||
|
||||
def header(assigns) do
|
||||
~H"""
|
||||
<div class="border-b-4 flex flex-row justify-between">
|
||||
<h1 class="mb-0 pl-2" style="height:48px; text-indent:5px"><%= @friend.name %></h1>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def menu(assigns) do
|
||||
~H"""
|
||||
<div class="hidden sm:tabs sm:mb-8">
|
||||
<%= for page <- ["overview", "calendar", "relationships"] do %>
|
||||
<% is_active = if(page == @page_view) do "tab-active" end %>
|
||||
<.link patch={"/friend/#{@friend.slug}?page=#{page}"} class={"font-bold sm:tab-lg flex-grow no-underline tab tab-lifted #{is_active}"}>
|
||||
<%= page |> :string.titlecase() %>
|
||||
</.link>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
"""
|
||||
end
|
||||
|
||||
end
|
||||
5
friends/lib/friends_web/live/friend.html.heex
Normal file
5
friends/lib/friends_web/live/friend.html.heex
Normal file
@ -0,0 +1,5 @@
|
||||
<section class="row">
|
||||
<article class="column prose">
|
||||
<FriendsWeb.FriendLive.Friend.content friend={@friend} page_view={@page_view} changeset={@changeset} action={@action} live_action={@live_action}/>
|
||||
</article>
|
||||
</section>
|
||||
@ -14,10 +14,10 @@ defmodule FriendsWeb.Router do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
scope "/", FriendsWeb do
|
||||
scope "/", FriendsWeb.FriendsLive do
|
||||
pipe_through :browser
|
||||
|
||||
live "/", FriendsLive.Index
|
||||
live "/", Index
|
||||
live "/friend/:slug", FriendsLive.Show
|
||||
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user