Sha256: d56f13212fed822cd01161d4244bc632b50771b9772a878b4655c27e003126bd

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

import { Controller } from '@hotwired/stimulus'
import { get, patch } from '@rails/request.js'

export default class extends Controller {
  static values = {
    key: String
  }

  async connect () {
    this.mutationObserver = new MutationObserver(this.mutateState.bind(this))

    this.mutationObserver.observe(this.element, { attributes: true })
  }

  disconnect () {
    this.mutationObserver?.disconnect()
  }

  mutateState (mutationList, observer) {
    mutationList
      .filter(mutation => mutation.attributeName !== 'data-solder-touch')
      .forEach(async mutation => {
        const body = new FormData()

        const attributes = {}
        this.element
          .getAttributeNames()
          .filter(name => name !== 'data-controller')
          .map(name => {
            attributes[name] = this.element.getAttribute(name)
          })

        body.append('key', this.keyValue)
        body.append('attributes', JSON.stringify(attributes))

        await patch('/solder/ui_state/update', {
          body
        })
      })
  }
}

Version data entries

2 entries across 1 versions & 1 rubygems

Version Path
solder-0.3.0 lib/generators/solder/stimulus/templates/solder_controller.js.tt
solder-0.3.0 lib/install/templates/solder_controller.js.tt