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
|
|
|
"""
|
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
use Phoenix.Component
|
2022-02-13 21:56:54 -05:00
|
|
|
|
|
|
|
@doc """
|
2023-01-26 00:36:15 -05:00
|
|
|
Phoenix.Component for a <time> element that renders the naivedatetime in the
|
|
|
|
user's local timezone with Alpine.js
|
2022-02-13 21:56:54 -05:00
|
|
|
"""
|
2022-02-25 21:53:04 -05:00
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
attr :datetime, :any, required: true, doc: "A `DateTime` struct or nil"
|
2022-02-13 21:56:54 -05:00
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
def datetime(assigns) do
|
2022-02-13 21:56:54 -05:00
|
|
|
~H"""
|
2023-02-04 11:29:06 -05:00
|
|
|
<time
|
|
|
|
:if={@datetime}
|
|
|
|
datetime={cast_datetime(@datetime)}
|
|
|
|
x-data={"{
|
|
|
|
datetime:
|
|
|
|
Intl.DateTimeFormat([], {dateStyle: 'short', timeStyle: 'long'})
|
|
|
|
.format(new Date(\"#{cast_datetime(@datetime)}\"))
|
|
|
|
}"}
|
|
|
|
x-text="datetime"
|
|
|
|
>
|
|
|
|
<%= cast_datetime(@datetime) %>
|
|
|
|
</time>
|
2022-02-13 21:56:54 -05:00
|
|
|
"""
|
|
|
|
end
|
2022-02-25 21:53:04 -05:00
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
@spec cast_datetime(NaiveDateTime.t() | nil) :: String.t()
|
|
|
|
defp cast_datetime(%NaiveDateTime{} = datetime) do
|
|
|
|
datetime |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601(:extended)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp cast_datetime(_datetime), do: ""
|
|
|
|
|
2022-02-25 21:53:04 -05:00
|
|
|
@doc """
|
2023-01-26 00:36:15 -05:00
|
|
|
Phoenix.Component for a <date> element that renders the Date in the user's
|
|
|
|
local timezone with Alpine.js
|
2022-02-25 21:53:04 -05:00
|
|
|
"""
|
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
attr :date, :any, required: true, doc: "A `Date` struct or nil"
|
2022-02-25 21:53:04 -05:00
|
|
|
|
2023-01-26 00:36:15 -05:00
|
|
|
def date(assigns) do
|
2022-02-25 21:53:04 -05:00
|
|
|
~H"""
|
2023-02-04 11:29:06 -05:00
|
|
|
<time
|
|
|
|
:if={@date}
|
|
|
|
datetime={@date |> Date.to_iso8601(:extended)}
|
|
|
|
x-data={"{
|
|
|
|
date:
|
|
|
|
Intl.DateTimeFormat([], {timeZone: 'Etc/UTC', dateStyle: 'short'})
|
|
|
|
.format(new Date(\"#{@date |> Date.to_iso8601(:extended)}\"))
|
|
|
|
}"}
|
|
|
|
x-text="date"
|
|
|
|
>
|
|
|
|
<%= @date |> Date.to_iso8601(:extended) %>
|
|
|
|
</time>
|
2022-02-25 21:53:04 -05:00
|
|
|
"""
|
|
|
|
end
|
2023-01-26 00:46:27 -05:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Displays content in a QR code as a base64 encoded PNG
|
|
|
|
"""
|
|
|
|
@spec qr_code_image(String.t()) :: String.t()
|
|
|
|
@spec qr_code_image(String.t(), width :: non_neg_integer()) :: String.t()
|
|
|
|
def qr_code_image(content, width \\ 384) do
|
|
|
|
img_data =
|
|
|
|
content
|
|
|
|
|> EQRCode.encode()
|
|
|
|
|> EQRCode.png(width: width, background_color: <<39, 39, 42>>, color: <<255, 255, 255>>)
|
|
|
|
|> Base.encode64()
|
|
|
|
|
|
|
|
"data:image/png;base64," <> img_data
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a downloadable QR Code element
|
|
|
|
"""
|
|
|
|
|
|
|
|
attr :content, :string, required: true
|
|
|
|
attr :filename, :string, default: "qrcode", doc: "filename without .png extension"
|
|
|
|
attr :image_class, :string, default: "w-64 h-max"
|
|
|
|
attr :width, :integer, default: 384, doc: "width of png to generate"
|
|
|
|
|
|
|
|
def qr_code(assigns) do
|
|
|
|
~H"""
|
|
|
|
<a href={qr_code_image(@content)} download={@filename <> ".png"}>
|
|
|
|
<img class={@image_class} alt={@filename} src={qr_code_image(@content)} />
|
|
|
|
</a>
|
|
|
|
"""
|
|
|
|
end
|
2022-02-13 21:56:54 -05:00
|
|
|
end
|