memEx/lib/memex_web/views/view_helpers.ex

53 lines
1.4 KiB
Elixir
Raw Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.ViewHelpers do
2022-02-13 21:56:54 -05:00
@moduledoc """
Contains common helpers that can be used in liveviews and regular views. These
2022-07-25 19:31:54 -04:00
are automatically imported into any Phoenix View using `use MemexWeb,
2022-02-25 21:53:04 -05:00
:view`
2022-02-13 21:56:54 -05:00
"""
import Phoenix.LiveView.Helpers
@doc """
Returns a <time> element that renders the naivedatetime in the user's local
timezone with Alpine.js
"""
2022-02-25 21:53:04 -05:00
@spec display_datetime(NaiveDateTime.t() | nil) :: Phoenix.LiveView.Rendered.t()
def display_datetime(nil), do: ""
2022-02-13 21:56:54 -05:00
def display_datetime(datetime) do
assigns = %{
datetime: datetime |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601(:extended)
}
~H"""
2022-02-25 21:53:04 -05:00
<time datetime={@datetime} x-data={"{
2022-02-13 21:56:54 -05:00
date:
Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'})
.format(new Date(\"#{@datetime}\"))
2022-02-25 21:53:04 -05:00
}"} x-text="date">
2022-02-13 21:56:54 -05:00
<%= @datetime %>
</time>
"""
end
2022-02-25 21:53:04 -05:00
@doc """
Returns a <date> element that renders the Date in the user's local
timezone with Alpine.js
"""
@spec display_date(Date.t() | nil) :: Phoenix.LiveView.Rendered.t()
def display_date(nil), do: ""
def display_date(date) do
assigns = %{date: date |> Date.to_iso8601(:extended)}
~H"""
<time datetime={@date} x-data={"{
date:
Intl.DateTimeFormat([], {timeZone: 'Etc/UTC', dateStyle: 'short'}).format(new Date(\"#{@date}\"))
}"} x-text="date">
<%= @date %>
</time>
"""
end
2022-02-13 21:56:54 -05:00
end