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

View File

@@ -1,28 +1,12 @@
defmodule LabelmakerWeb.LabelController do
use LabelmakerWeb, :controller
alias LabelmakerWeb.Constants
alias LabelmakerWeb.Tools
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
File.mkdir_p!(@label_dir)
def show(conn, params) do
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)
options = Tools.process_parameters(params)
filename =
options
@@ -47,15 +31,15 @@ defmodule LabelmakerWeb.LabelController do
"-background",
"none",
"-fill",
options["color"],
options.color,
"-pointsize",
options["size"],
options.size,
"-font",
options["font"],
"label:#{options["label"]}",
options.font,
"label:#{options.label}",
"-set",
"comment",
inspect(options),
inspect(Jason.encode!(options)),
filepath
]

View File

@@ -1,29 +1,24 @@
defmodule LabelmakerWeb.Home do
use LabelmakerWeb, :live_view
alias LabelmakerWeb.Constants
alias LabelmakerWeb.Tools
def mount(_params, _session, socket) do
{:ok,
assign(socket,
label: "",
font: "Helvetica",
color: "black",
size: "24"
)}
{
:ok,
assign(
socket,
Enum.to_list(Constants.defaults())
)
}
end
def handle_event("update_label", params, socket) do
{:noreply,
assign(socket,
label: params["label"] || "",
font: params["font"] || "Helvetica",
color: params["color"] || "black",
size: params["size"] || "24"
)}
{:noreply, assign(socket, Tools.process_parameters(params))}
end
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
def render(assigns) do

View File

@@ -14,18 +14,11 @@ defmodule LabelmakerWeb.Router do
plug :accepts, ["json"]
end
# scope "/", LabelmakerWeb do
# pipe_through :browser
#
# get "/", PageController, :home
# end
scope "/", LabelmakerWeb do
pipe_through :browser
live "/", Home
get "/:label", LabelController, :show
# live "/:label", Label
end
# 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