add datetime view helper

This commit is contained in:
shibao 2022-02-12 02:28:04 -05:00
parent ba58f92f02
commit fa55c13c29
2 changed files with 35 additions and 0 deletions

View File

@ -98,6 +98,7 @@ defmodule CanneryWeb do
import CanneryWeb.ErrorHelpers import CanneryWeb.ErrorHelpers
import CanneryWeb.Gettext import CanneryWeb.Gettext
import CanneryWeb.ViewHelpers
alias CanneryWeb.Router.Helpers, as: Routes alias CanneryWeb.Router.Helpers, as: Routes
end end
end end

View File

@ -0,0 +1,34 @@
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
datetime={datetime}
x-data={"{
date:
Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'})
.format(new Date(\"#{datetime}\"))
}"}
x-text="date"
>
<%= datetime %>
</time>
"""
end
end