cleaned up data structure, cleaned up some code

This commit is contained in:
Gavin McDonald
2025-05-06 14:12:04 -04:00
parent 1ee183557c
commit 2d5dbd4140
5 changed files with 44 additions and 50 deletions

View File

@@ -1,12 +1,12 @@
defmodule LabelmakerWeb.Constants do defmodule LabelmakerWeb.Constants do
@defaults %{ @defaults %{
"label" => "64 character maximum", label: "64 character maximum",
"font" => "Helvetica", font: "Helvetica",
"color" => "black", color: "black",
"size" => "24" size: "24"
} }
@permitted_keys Map.keys(@defaults) @permitted_keys @defaults |> Map.keys() |> Enum.map(&Atom.to_string/1)
@colors System.cmd("magick", ["-list", "color"]) @colors System.cmd("magick", ["-list", "color"])
|> elem(0) |> elem(0)

View File

@@ -1,28 +1,12 @@
defmodule LabelmakerWeb.LabelController do defmodule LabelmakerWeb.LabelController do
use LabelmakerWeb, :controller use LabelmakerWeb, :controller
alias LabelmakerWeb.Constants alias LabelmakerWeb.Tools
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels") @label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
File.mkdir_p!(@label_dir) File.mkdir_p!(@label_dir)
def show(conn, params) do def show(conn, params) do
params = options = Tools.process_parameters(params)
params
|> Map.take(Constants.permitted_keys())
|> Enum.filter(fn {k, v} ->
case k do
"color" -> v in Constants.colors()
"font" -> v in Constants.fonts()
"label" -> String.length(v) <= Constants.max_label_length()
"size" -> v in Constants.sizes()
_ -> true
end
end)
|> Map.new()
options =
Constants.defaults()
|> Map.merge(params)
filename = filename =
options options
@@ -47,15 +31,15 @@ defmodule LabelmakerWeb.LabelController do
"-background", "-background",
"none", "none",
"-fill", "-fill",
options["color"], options.color,
"-pointsize", "-pointsize",
options["size"], options.size,
"-font", "-font",
options["font"], options.font,
"label:#{options["label"]}", "label:#{options.label}",
"-set", "-set",
"comment", "comment",
inspect(options), inspect(Jason.encode!(options)),
filepath filepath
] ]

View File

@@ -1,29 +1,24 @@
defmodule LabelmakerWeb.Home do defmodule LabelmakerWeb.Home do
use LabelmakerWeb, :live_view use LabelmakerWeb, :live_view
alias LabelmakerWeb.Constants alias LabelmakerWeb.Constants
alias LabelmakerWeb.Tools
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, {
assign(socket, :ok,
label: "", assign(
font: "Helvetica", socket,
color: "black", Enum.to_list(Constants.defaults())
size: "24" )
)} }
end end
def handle_event("update_label", params, socket) do def handle_event("update_label", params, socket) do
{:noreply, {:noreply, assign(socket, Tools.process_parameters(params))}
assign(socket,
label: params["label"] || "",
font: params["font"] || "Helvetica",
color: params["color"] || "black",
size: params["size"] || "24"
)}
end end
def handle_event("make_label", params, socket) do def handle_event("make_label", params, socket) do
{:noreply, push_navigate(socket, to: ~p"/#{params["label"]}?#{Map.drop(params, ["label"])}")} {:noreply, redirect(socket, to: ~p"/#{params["label"]}?#{Map.drop(params, ["label"])}")}
end end
def render(assigns) do def render(assigns) do

View File

@@ -14,18 +14,11 @@ defmodule LabelmakerWeb.Router do
plug :accepts, ["json"] plug :accepts, ["json"]
end end
# scope "/", LabelmakerWeb do
# pipe_through :browser
#
# get "/", PageController, :home
# end
scope "/", LabelmakerWeb do scope "/", LabelmakerWeb do
pipe_through :browser pipe_through :browser
live "/", Home live "/", Home
get "/:label", LabelController, :show get "/:label", LabelController, :show
# live "/:label", Label
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.

View File

@@ -0,0 +1,22 @@
defmodule LabelmakerWeb.Tools do
alias LabelmakerWeb.Constants
def process_parameters(parameters) do
parameters =
parameters
|> Map.take(Constants.permitted_keys())
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
|> Enum.filter(fn {k, v} ->
case k do
:color -> v in Constants.colors()
:font -> v in Constants.fonts()
:label -> String.length(v) <= Constants.max_label_length()
:size -> v in Constants.sizes()
_ -> true
end
end)
|> Map.new()
Map.merge(Constants.defaults(), parameters)
end
end