nulla/lib/nulla_web/components/templates.ex

100 lines
2.1 KiB
Elixir

defmodule NullaWeb.UserHTML do
use NullaWeb, :html
embed_templates "templates/user/*"
@doc """
Renders a user form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
def user_form(assigns)
def format_birthdate(date) do
formatted = Date.to_string(date) |> String.replace("-", "/")
age = Timex.diff(Timex.today(), date, :years)
"#{formatted} (#{age} years old)"
end
def format_registration_date(date) do
now = Timex.now()
formatted = Date.to_string(date) |> String.replace("-", "/")
diff = Timex.diff(now, date, :days)
relative =
cond do
diff == 0 ->
"today"
diff == 1 ->
"1 day ago"
diff < 30 ->
"#{diff} days ago"
diff < 365 ->
months = Timex.diff(now, date, :months)
if months == 1, do: "1 month ago", else: "#{months} months ago"
true ->
years = Timex.diff(now, date, :years)
if years == 1, do: "1 year ago", else: "#{years} years ago"
end
"#{formatted} (#{relative})"
end
def format_note_datetime(datetime) do
Timex.format!(datetime, "{0D} {Mfull} {YYYY}, {h24}:{m}", :strftime)
end
def format_note_datetime_diff(datetime) do
now = Timex.now()
diff = Timex.diff(now, datetime, :seconds)
cond do
diff < 60 ->
"now"
diff < 3600 ->
minutes = div(diff, 60)
"#{minutes}m ago"
diff < 86400 ->
hours = div(diff, 3600)
"#{hours}h ago"
diff < 518_400 ->
days = div(diff, 86400)
"#{days}d ago"
diff < 2_419_200 ->
weeks = div(diff, 604_800)
"#{weeks}w ago"
diff < 28_512_000 ->
months = Timex.diff(now, datetime, :months)
"#{months}mo ago"
true ->
years = Timex.diff(now, datetime, :years)
"#{years}y ago"
end
end
end
defmodule NullaWeb.NoteHTML do
use NullaWeb, :html
embed_templates "templates/note/*"
@doc """
Renders a note form.
"""
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
def note_form(assigns)
end