fix view helpers display bug

This commit is contained in:
shibao 2022-03-04 22:05:08 -05:00
parent f120e54c3e
commit ec6946068e

View File

@ -7,6 +7,8 @@ defmodule CanneryWeb.ViewHelpers do
import Phoenix.LiveView.Helpers import Phoenix.LiveView.Helpers
@id_length 16
@doc """ @doc """
Returns a <time> element that renders the naivedatetime in the user's local Returns a <time> element that renders the naivedatetime in the user's local
timezone with Alpine.js timezone with Alpine.js
@ -16,11 +18,12 @@ defmodule CanneryWeb.ViewHelpers do
def display_datetime(datetime) do def display_datetime(datetime) do
assigns = %{ assigns = %{
id: :crypto.strong_rand_bytes(@id_length) |> Base.url_encode64(),
datetime: datetime |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601(:extended) datetime: datetime |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601(:extended)
} }
~H""" ~H"""
<time datetime={@datetime} x-data={"{ <time id={@id} datetime={@datetime} x-data={"{
date: date:
Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'}) Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'})
.format(new Date(\"#{@datetime}\")) .format(new Date(\"#{@datetime}\"))
@ -38,10 +41,13 @@ defmodule CanneryWeb.ViewHelpers do
def display_date(nil), do: "" def display_date(nil), do: ""
def display_date(date) do def display_date(date) do
assigns = %{date: date |> Date.to_iso8601(:extended)} assigns = %{
id: :crypto.strong_rand_bytes(@id_length) |> Base.url_encode64(),
date: date |> Date.to_iso8601(:extended)
}
~H""" ~H"""
<time datetime={@date} x-data={"{ <time id={@id} datetime={@date} x-data={"{
date: date:
Intl.DateTimeFormat([], {timeZone: 'Etc/UTC', dateStyle: 'short'}).format(new Date(\"#{@date}\")) Intl.DateTimeFormat([], {timeZone: 'Etc/UTC', dateStyle: 'short'}).format(new Date(\"#{@date}\"))
}"} x-text="date"> }"} x-text="date">