77 lines
1.8 KiB
Elixir
77 lines
1.8 KiB
Elixir
defmodule FriendsWeb.LiveHelpers do
|
|
use FriendsWeb, :live_component
|
|
alias Friends.Friend
|
|
|
|
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}"
|
|
IO.inspect "#{country}(#{area}) #{first}-#{sec1}#{sec2}"
|
|
11 when has_plus ->
|
|
phone
|
|
end
|
|
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(: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
|
|
end
|