Skip to content

Commit

Permalink
Merge pull request #28 from leouofa/turbo-upgrade
Browse files Browse the repository at this point in the history
Updating the form submission state
  • Loading branch information
leouofa authored Sep 21, 2024
2 parents 2ee1555 + a244070 commit 8274ddf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
18 changes: 15 additions & 3 deletions app/frontend/controllers/form_controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ["button"]
static targets = ["button", "textarea"]

connect() {
// Only call toggleButtonState if textareaTarget is present
if (this.hasTextareaTarget) {
this.toggleButtonState()
}
}

toggleButtonState() {
const message = this.textareaTarget.value.trim()
this.buttonTarget.disabled = message.length === 0
}

onInput() {
this.toggleButtonState()
}

submit(e) {
this.buttonTarget.classList.add('loading')
this.buttonTarget.disabled = true
}
}
}
2 changes: 1 addition & 1 deletion app/frontend/stylesheets/admin.sass
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

@layer
.btn-primary
@apply py-2 px-4 bg-blue-500 text-sm text-white rounded-sm hover:bg-blue-600 hover:text-white border-solid border-2 border-gray-700 no-underline cursor-pointer
@apply py-2 px-4 bg-blue-500 disabled:bg-blue-300 text-sm text-white disabled:text-gray-50 rounded-sm hover:bg-blue-600 hover:text-white border-solid border-2 border-gray-700 disabled:border-gray-200 no-underline cursor-pointer

.btn-secondary
@apply py-2 px-4 bg-emerald-500 text-sm text-white rounded-sm hover:bg-emerald-600 hover:text-white border-solid border-2 border-gray-700 no-underline cursor-pointer
Expand Down
6 changes: 3 additions & 3 deletions app/views/texts/_iterate_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= form_with url: iterate_writing_style_path(parent), class: 'ui reply form !mb-20', method: :post, local: true do |form|
= form_with url: iterate_writing_style_path(parent), data: {'controller': 'form'}, class: 'ui reply form !mb-20', method: :post, local: true do |form|
.field
= form.text_area :message, rows: 3, placeholder: 'Instructions. I.e. "Remove Lines 10 and 11"', id: 'form_text_area'
= form.text_area :message, rows: 3, placeholder: 'Instructions. I.e. "Remove Lines 10 and 11"', id: 'form_text_area', data: {'form-target': 'textarea', 'action': 'input->form#onInput'}
.actions
= form.submit 'Iterate On Prompt', class: 'btn-primary'
= form.submit 'Iterate On Prompt', class: 'btn-primary', data: {'form-target': 'button'}
4 changes: 2 additions & 2 deletions app/views/writing_styles/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
=r ux.component_field
= form.label :name
= form.text_field :name, value: @component.name
=r ux.component_submit_field
= form.submit class: 'btn-primary'
=r ux.component_submit_field controller: 'form'
= form.submit class: 'btn-primary', data: {'form-target': 'button'}
2 changes: 1 addition & 1 deletion config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Rails.application.config.content_security_policy do |policy|
policy.connect_src :self, :https

policy.connect_src(*policy.connect_src, "ws://#{ViteRuby.config.host_with_port}") if Rails.env.development?
policy.connect_src(*policy.connect_src, "ws://#{ViteRuby.config.host_with_port}", "http://#{ViteRuby.config.host_with_port}") if Rails.env.development?

policy.default_src :self, :https
policy.font_src :self, :https, :data
Expand Down

0 comments on commit 8274ddf

Please sign in to comment.