Skip to content

Commit

Permalink
Simplify error component
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Mar 12, 2024
1 parent 12e49f0 commit fc283d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
5 changes: 2 additions & 3 deletions lib/asciinema_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
<small :if={@error} class="form-text text-danger"><%= translate_error(@error) %></small>
Expand Down
8 changes: 4 additions & 4 deletions lib/asciinema_web/controllers/live_stream/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<.label for={f[:title]} class="col-sm-4 col-md-3 col-lg-3 col-form-label">Title</.label>
<div class="col-sm-8 col-md-9 col-lg-9">
<.input field={f[:title]} class="form-control" autofocus={true} />
<.error form={f} field={:title} />
<.error field={f[:title]} />
</div>
</div>

Expand All @@ -18,7 +18,7 @@
</.label>
<div class="col-sm-8 col-md-9 col-lg-9">
<.input type="textarea" field={f[:description]} class="form-control" rows={10} />
<.error form={f} field={:description} />
<.error field={f[:description]} />
<small class="form-text text-muted">Parsed as Markdown.</small>
</div>
</div>
Expand All @@ -37,7 +37,7 @@
step="any"
placeholder="1.0"
/>
<.error form={f} field={:buffer_time} />
<.error field={f[:buffer_time]} />
<small class="form-text text-muted">
Player uses buffer duration (in
seconds) to avoid animation lag from network jitter. Set to 0 to
Expand Down Expand Up @@ -75,7 +75,7 @@
step="any"
placeholder="1.33333"
/>
<.error form={f} field={:terminal_line_height} />
<.error field={f[:terminal_line_height]} />
<small class="form-text text-muted">
Relative to font size. Lowering it to ~1.1 helps with alignment of block characters like ▀ ▄ █
</small>
Expand Down
18 changes: 9 additions & 9 deletions lib/asciinema_web/controllers/recording/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<.label for={f[:title]} class="col-sm-4 col-md-3 col-lg-3 col-form-label">Title</.label>
<div class="col-sm-8 col-md-9 col-lg-9">
<.input field={f[:title]} class="form-control" autofocus={true} />
<.error form={f} field={:title} />
<.error field={f[:title]} />
</div>
</div>

Expand All @@ -18,7 +18,7 @@
</.label>
<div class="col-sm-8 col-md-9 col-lg-9">
<.input field={f[:description]} type="textarea" class="form-control" rows={10} />
<.error form={f} field={:description} />
<.error field={f[:description]} />
<small class="form-text text-muted">Parsed as Markdown.</small>
</div>
</div>
Expand All @@ -35,7 +35,7 @@
min={0.5}
step={0.5}
/>
<.error form={f} field={:idle_time_limit} />
<.error field={f[:idle_time_limit]} />
<small class="form-text text-muted">
Limit terminal inactivity to a given number of seconds. Leave blank for no limit.
</small>
Expand All @@ -53,7 +53,7 @@
step={0.1}
placeholder={1.0}
/>
<.error form={f} field={:speed} />
<.error field={f[:speed]} />
<small class="form-text text-muted">
Playback speed. 1.0 is the original speed, less is slower, more is faster.
</small>
Expand All @@ -73,7 +73,7 @@
step={1}
placeholder={@asciicast.cols}
/>
<.error form={f} field={:cols_override} />
<.error field={f[:cols_override]} />
<small class="form-text text-muted">
Leave blank to use recording's original value.
</small>
Expand All @@ -93,7 +93,7 @@
step={1}
placeholder={@asciicast.rows}
/>
<.error form={f} field={:rows_override} />
<.error field={f[:rows_override]} />
<small class="form-text text-muted">
Leave blank to use recording's original value.
</small>
Expand Down Expand Up @@ -129,7 +129,7 @@
step="any"
placeholder="1.33333"
/>
<.error form={f} field={:terminal_line_height} />
<.error field={f[:terminal_line_height]} />
<small class="form-text text-muted">
Relative to the font size. Lowering it to ~1.1 helps with alignment of block characters such as ▀ ▄ █
</small>
Expand Down Expand Up @@ -160,7 +160,7 @@
</.label>
<div class="col-sm-8 col-md-9 col-lg-9">
<.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]} />
<small class="form-text text-muted">
Time of the frame to be displayed as snapshot/poster. For example: 10.5
</small>
Expand All @@ -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]} />
<small class="form-text text-muted">
Each line is a marker defined as <code>[time] - [label]</code>.
</small>
Expand Down
6 changes: 3 additions & 3 deletions lib/asciinema_web/controllers/user/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<.label for={f[:email]} class="email required col-form-label col-3">Email</.label>
<div class="col-9">
<.input field={f[:email]} type="email" class="string email required form-control" />
<.error form={f} field={:email} />
<.error field={f[:email]} />
</div>
</div>

Expand Down Expand Up @@ -44,7 +44,7 @@
</.label>
<div class="col-9">
<.input field={f[:username]} class="string optional form-control" />
<.error form={f} field={:username} />
<.error field={f[:username]} />
</div>
</div>

Expand All @@ -58,7 +58,7 @@
class="string optional form-control"
placeholder={@changeset.data.username}
/>
<.error form={f} field={:name} />
<.error field={f[:name]} />
</div>
</div>

Expand Down

0 comments on commit fc283d4

Please sign in to comment.