cannery/lib/cannery_web/views/view_helpers.ex

57 lines
1.4 KiB
Elixir
Raw Normal View History

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