98 lines
2.1 KiB
Elixir
98 lines
2.1 KiB
Elixir
defmodule FriendsWeb.LiveHelpers do
|
|
alias Friends.Friend
|
|
use Phoenix.LiveComponent
|
|
|
|
def titlecase(atom) do
|
|
atom |> to_string |> :string.titlecase()
|
|
end
|
|
|
|
def parse_id("new"), do: "new"
|
|
def parse_id(int), do: int |> String.to_integer()
|
|
|
|
def display_phone(phone, changeset) do
|
|
if changeset.errors[:phone] do
|
|
phone
|
|
else
|
|
display_phone(phone)
|
|
end
|
|
end
|
|
|
|
def display_phone(phone) do
|
|
"""
|
|
TODO: Actually implement this
|
|
"""
|
|
|
|
has_plus = phone |> String.starts_with?("+")
|
|
|
|
case phone |> String.length() do
|
|
10 ->
|
|
country = "+1 "
|
|
|
|
[area, first, sec1, sec2] =
|
|
phone |> to_charlist |> Enum.chunk_every(3) |> Enum.map(&(&1 |> to_string))
|
|
|
|
"#{country}(#{area}) #{first}-#{sec1}#{sec2}"
|
|
|
|
11 when has_plus ->
|
|
phone
|
|
|
|
12 when has_plus ->
|
|
phone
|
|
end
|
|
end
|
|
|
|
def get_edit_text(live_action) do
|
|
case live_action do
|
|
:timeline -> "add a moment"
|
|
:relationships -> "add a relationship"
|
|
_ -> "edit"
|
|
end
|
|
end
|
|
|
|
def format_date(date) do
|
|
date
|
|
|> Calendar.strftime("%b %d, %Y")
|
|
end
|
|
|
|
def assign_current_user(socket, user_token) do
|
|
user =
|
|
case user_token do
|
|
nil ->
|
|
nil
|
|
|
|
_moot ->
|
|
user_token
|
|
|> Friends.Accounts.get_user_by_session_token()
|
|
|> Friends.Repo.preload(:profile)
|
|
end
|
|
|
|
socket
|
|
|> assign(
|
|
:current_user,
|
|
user
|
|
)
|
|
end
|
|
|
|
# Set page title variable
|
|
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(:editable, friend |> Friend.can_be_edited_by(socket.assigns[:current_user]))
|
|
|> assign(:changeset, friend |> Friend.changeset())
|
|
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(:editable, friend |> Friend.can_be_edited_by(socket.assigns[:current_user]))
|
|
|> assign(:changeset, changeset)
|
|
end
|
|
end
|