54 lines
1.4 KiB
Elixir
54 lines
1.4 KiB
Elixir
defmodule LabelmakerWeb.Home do
|
|
use LabelmakerWeb, :live_view
|
|
import LabelmakerWeb.RadioComponent
|
|
alias LabelmakerWeb.Constants
|
|
alias LabelmakerWeb.Tools
|
|
|
|
def mount(_params, _session, socket) do
|
|
assigns =
|
|
Constants.defaults()
|
|
|> Map.merge(Constants.preview())
|
|
|> Enum.to_list()
|
|
|
|
{
|
|
:ok,
|
|
assign(
|
|
socket,
|
|
assigns
|
|
)
|
|
}
|
|
end
|
|
|
|
def handle_event("update_label", params, socket) do
|
|
assigns =
|
|
socket.assigns
|
|
|> Enum.map(fn {k, v} -> {Atom.to_string(k), v} end)
|
|
|> Map.new()
|
|
|> Map.merge(params)
|
|
|> Tools.process_parameters()
|
|
|> Enum.to_list()
|
|
|
|
{:noreply, assign(socket, assigns)}
|
|
end
|
|
|
|
def handle_event("update_preview", %{"bg" => bg}, socket) do
|
|
preview_background =
|
|
case bg do
|
|
"r" -> "bg-[linear-gradient(to_right,_black_33%,_white_67%)]"
|
|
"b" -> "bg-[linear-gradient(to_bottom,_black_33%,_white_67%)]"
|
|
"c" -> ""
|
|
_ -> "bg-[linear-gradient(to_right,_black_33%,_white_67%)]"
|
|
end
|
|
|
|
{:noreply, assign(socket, :preview_background, preview_background)}
|
|
end
|
|
|
|
def handle_event("update_alignment", %{"option" => option}, socket) do
|
|
{:noreply, assign(socket, :align, option)}
|
|
end
|
|
|
|
def handle_event("make_label", params, socket) do
|
|
{:noreply, redirect(socket, to: ~p"/#{params["label"]}?#{Map.drop(params, ["label"])}")}
|
|
end
|
|
end
|