Highlight errors

This commit is contained in:
Jorge Manrubia
2025-05-06 09:56:12 +02:00
parent a441d144e9
commit 0bdbcbf156
4 changed files with 31 additions and 20 deletions
+4
View File
@@ -54,4 +54,8 @@
display: flex;
}
}
.has-error .terminal__input {
border: 2px solid red;
}
}
+6 -3
View File
@@ -3,10 +3,14 @@ class CommandsController < ApplicationController
@commands = Current.user.commands.order(created_at: :desc).limit(20)
end
def new
end
def create
command = parse_command(params[:command])
if command
if command&.valid?
result = command.execute
case result
@@ -16,7 +20,7 @@ class CommandsController < ApplicationController
redirect_back_or_to root_path
end
else
raise "Pending to handle invalid commands"
render turbo_stream: turbo_stream.replace("commands_form", partial: "commands/form", locals: { error: true })
end
end
@@ -24,7 +28,6 @@ class CommandsController < ApplicationController
def parse_command(string)
Command::Parser.new(parsing_context).parse(string).tap do |command|
Current.user.commands << command
command.validate!
end
end
+20
View File
@@ -0,0 +1,20 @@
<% has_error = local_assigns.fetch(:error, false) %>
<%= form_tag commands_path,
id: "commands_form",
class: [ "flex align-center gap-half", { "has-error" => has_error } ],
data: { controller: "form", action: "turbo:submit-end->form#reset" } do %>
<label class="terminal__label txt-nowrap" for="fizzy_do">Fizzy do &gt;</label>
<%= text_field_tag "command", has_error ? params[:command] : nil,
autocomplete: "off",
autocorrect: "off",
autocapitalize: "off",
class: "terminal__input input fill-transparent unpad",
data: {
commands_target: "input",
action: "keydown.up->toggle-class#add:prevent keydown.down->toggle-class#remove:prevent"
},
placeholder: "Search or type commands…" %>
<% end %>
+1 -17
View File
@@ -3,22 +3,6 @@
toggle_class_toggle_class: "terminal--open" } do %>
<%= turbo_frame_tag :recent_commands, src: commands_path, refresh: "morph" %>
<%= form_tag commands_path,
class: "flex align-center gap-half",
data: { controller: "form", action: "turbo:submit-end->form#reset" } do %>
<label class="terminal__label txt-nowrap" for="fizzy_do">Fizzy do &gt;</label>
<%= text_field_tag "command", nil,
autocomplete: "off",
autocorrect: "off",
autocapitalize: "off",
class: "terminal__input input fill-transparent unpad",
data: {
turbo_permanent: true,
commands_target: "input",
action: "keydown.up->toggle-class#add:prevent keydown.down->toggle-class#remove:prevent"
},
placeholder: "Search or type commands…" %>
<% end %>
<%= render "commands/form" %>
<% end %>