add datetime helper

This commit is contained in:
shibao 2022-02-13 21:56:54 -05:00 committed by oliviasculley
parent 7283932d85
commit 23b60e032d
2 changed files with 34 additions and 3 deletions

View File

@ -91,13 +91,11 @@ defmodule LokalWeb do
# Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc) # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
import Phoenix.LiveView.Helpers import Phoenix.LiveView.Helpers
import LokalWeb.LiveHelpers
# Import basic rendering functionality (render, render_layout, etc) # Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View import Phoenix.View
import LokalWeb.ErrorHelpers import LokalWeb.{ErrorHelpers, Gettext, LiveHelpers, ViewHelpers}
import LokalWeb.Gettext
alias LokalWeb.Router.Helpers, as: Routes alias LokalWeb.Router.Helpers, as: Routes
end end
end end

View File

@ -0,0 +1,33 @@
defmodule LokalWeb.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 LokalWeb, :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