hyperloop icon indicating copy to clipboard operation
hyperloop copied to clipboard

Hyperloop Operation mangles Hash class

Open illogikal opened this issue 8 years ago • 0 comments

class TestPage < Hyperloop::Component
  def render
    div do
      form do
        TestField(store: TestStore, 
                  update_op: UpdateTestForm, 
                  form_key: :test_form, 
                  field_name: :test)
        button(type: :submit)
      end
    end
  end
end

class TestField < Hyperloop::Component
  param :field_name, required: false
  param :form_key,   type: String, required: false
  param :store,      required: true
  param :update_op,  required: true

  def update_value val
    params.update_op.run(test_field: val.gsub(/\s/, ''))
  end

  def valid?
     params.store.send(params.form_key)[field_error_name].present?
  end

  def field_error_name
    "#{params.field_name}_error"
  end

  def render
    div {
      p { valid? ? "Valid" : "Invalid" }
      input(type: :text).
        on(:key_down, :key_up) { |e| update_value e.target.value }
    }
  end
end

class UpdateTestForm < Hyperloop::Operation
  param :test_field, type: String, default: ''
  param :test_field_error, type: String, default: ''
  step { update_form }

  def update_form
    params.test_field_error = params.test_field.present? ? '' : "Can't be blank."
  end
end

class TestStore < Hyperloop::Store
  state test_form: {}, scope: :shared

  receives UpdateTestForm do |params|
    mutate.test_form(params)
  end

  def self.test_form
    state.test_form
  end
end

hyperloop-loader-system.self-ebeb435….js?bo…:20 Exception raised while rendering #<TestField:0xade>: undefined method `[]' for {"test_field"=>"", "test_field_error"=>"Test is invalid"}

Not sure what I'm doing incorrectly, as inspecting the TestStore.test_form outputs the correct hash, but it seems like it's a different class to Hash, and using hash methods on it does not work.

illogikal avatar Jul 20 '17 15:07 illogikal