From fc283d47327d2a6568a803c696dc1d44d26ac494 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Tue, 12 Mar 2024 11:00:40 +0100 Subject: [PATCH] Simplify error component --- .../components/core_components.ex | 5 ++--- .../controllers/live_stream/edit.html.heex | 8 ++++---- .../controllers/recording/edit.html.heex | 18 +++++++++--------- .../controllers/user/edit.html.heex | 6 +++--- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/asciinema_web/components/core_components.ex b/lib/asciinema_web/components/core_components.ex index 2e7bb693c..4a492792f 100644 --- a/lib/asciinema_web/components/core_components.ex +++ b/lib/asciinema_web/components/core_components.ex @@ -104,11 +104,10 @@ defmodule AsciinemaWeb.CoreComponents do """ end - attr :form, :any, required: true - attr :field, :atom, required: true + attr :field, Phoenix.HTML.FormField, required: true def error(assigns) do - assigns = assign(assigns, :error, assigns.form.errors[assigns.field]) + assigns = assign(assigns, :error, List.first(assigns.field.errors)) ~H""" <%= translate_error(@error) %> diff --git a/lib/asciinema_web/controllers/live_stream/edit.html.heex b/lib/asciinema_web/controllers/live_stream/edit.html.heex index ca003df0b..e68571def 100644 --- a/lib/asciinema_web/controllers/live_stream/edit.html.heex +++ b/lib/asciinema_web/controllers/live_stream/edit.html.heex @@ -8,7 +8,7 @@ <.label for={f[:title]} class="col-sm-4 col-md-3 col-lg-3 col-form-label">Title
<.input field={f[:title]} class="form-control" autofocus={true} /> - <.error form={f} field={:title} /> + <.error field={f[:title]} />
@@ -18,7 +18,7 @@
<.input type="textarea" field={f[:description]} class="form-control" rows={10} /> - <.error form={f} field={:description} /> + <.error field={f[:description]} /> Parsed as Markdown.
@@ -37,7 +37,7 @@ step="any" placeholder="1.0" /> - <.error form={f} field={:buffer_time} /> + <.error field={f[:buffer_time]} /> Player uses buffer duration (in seconds) to avoid animation lag from network jitter. Set to 0 to @@ -75,7 +75,7 @@ step="any" placeholder="1.33333" /> - <.error form={f} field={:terminal_line_height} /> + <.error field={f[:terminal_line_height]} /> Relative to font size. Lowering it to ~1.1 helps with alignment of block characters like ▀ ▄ █ diff --git a/lib/asciinema_web/controllers/recording/edit.html.heex b/lib/asciinema_web/controllers/recording/edit.html.heex index 99f517f09..1cdb0084f 100644 --- a/lib/asciinema_web/controllers/recording/edit.html.heex +++ b/lib/asciinema_web/controllers/recording/edit.html.heex @@ -8,7 +8,7 @@ <.label for={f[:title]} class="col-sm-4 col-md-3 col-lg-3 col-form-label">Title
<.input field={f[:title]} class="form-control" autofocus={true} /> - <.error form={f} field={:title} /> + <.error field={f[:title]} />
@@ -18,7 +18,7 @@
<.input field={f[:description]} type="textarea" class="form-control" rows={10} /> - <.error form={f} field={:description} /> + <.error field={f[:description]} /> Parsed as Markdown.
@@ -35,7 +35,7 @@ min={0.5} step={0.5} /> - <.error form={f} field={:idle_time_limit} /> + <.error field={f[:idle_time_limit]} /> Limit terminal inactivity to a given number of seconds. Leave blank for no limit. @@ -53,7 +53,7 @@ step={0.1} placeholder={1.0} /> - <.error form={f} field={:speed} /> + <.error field={f[:speed]} /> Playback speed. 1.0 is the original speed, less is slower, more is faster. @@ -73,7 +73,7 @@ step={1} placeholder={@asciicast.cols} /> - <.error form={f} field={:cols_override} /> + <.error field={f[:cols_override]} /> Leave blank to use recording's original value. @@ -93,7 +93,7 @@ step={1} placeholder={@asciicast.rows} /> - <.error form={f} field={:rows_override} /> + <.error field={f[:rows_override]} /> Leave blank to use recording's original value. @@ -129,7 +129,7 @@ step="any" placeholder="1.33333" /> - <.error form={f} field={:terminal_line_height} /> + <.error field={f[:terminal_line_height]} /> Relative to the font size. Lowering it to ~1.1 helps with alignment of block characters such as ▀ ▄ █ @@ -160,7 +160,7 @@
<.input field={f[:snapshot_at]} type="number" class="form-control" min={0} step={0.1} /> - <.error form={f} field={:snapshot_at} /> + <.error field={f[:snapshot_at]} /> Time of the frame to be displayed as snapshot/poster. For example: 10.5 @@ -179,7 +179,7 @@ rows={6} placeholder={"Example:\n\n" <> "5.0 - Intro\n11.3 - Installation\n32.0 - Configuration\n66.5 - Tips & Tricks"} /> - <.error form={f} field={:markers} /> + <.error field={f[:markers]} /> Each line is a marker defined as [time] - [label]. diff --git a/lib/asciinema_web/controllers/user/edit.html.heex b/lib/asciinema_web/controllers/user/edit.html.heex index f8542fe3e..e658ec447 100644 --- a/lib/asciinema_web/controllers/user/edit.html.heex +++ b/lib/asciinema_web/controllers/user/edit.html.heex @@ -15,7 +15,7 @@ <.label for={f[:email]} class="email required col-form-label col-3">Email
<.input field={f[:email]} type="email" class="string email required form-control" /> - <.error form={f} field={:email} /> + <.error field={f[:email]} />
@@ -44,7 +44,7 @@
<.input field={f[:username]} class="string optional form-control" /> - <.error form={f} field={:username} /> + <.error field={f[:username]} />
@@ -58,7 +58,7 @@ class="string optional form-control" placeholder={@changeset.data.username} /> - <.error form={f} field={:name} /> + <.error field={f[:name]} />