Sha256: cac76c0864c6e33fad710033627a5b54e5f25ace67169f73ea66f4ca52752032

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

import { Controller } from "@hotwired/stimulus"
import { useDebounce } from "stimulus-use"

export default class extends Controller {
  static values = { productsUrl: String }
  static debounces = [{ name: "submitLineItems", wait: 500 }]

  connect() {
    useDebounce(this)
    this.lineItemsToBeSubmitted = []
  }

  async search({ detail: { query, controller } }) {
    controller.resultsValue = await (
      await fetch(`${this.productsUrlValue}?q[name_or_variants_including_master_sku_cont]=${query}`)
    ).text()
  }

  updateLineItem(event) {
    if (!this.lineItemsToBeSubmitted.includes(event.currentTarget)) {
      this.lineItemsToBeSubmitted.push(event.currentTarget)
    }

    this.submitLineItems()
  }

  // This is a workaround to permit using debounce when needing to pass a parameter
  submitLineItems() {
    this.lineItemsToBeSubmitted.forEach((lineItem) => lineItem.form.requestSubmit())
    this.lineItemsToBeSubmitted = []
  }

  selectResult(event) {
    const form = event.detail.resultTarget.querySelector("form")
    form.submit()
  }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_admin-0.3.2 app/components/solidus_admin/orders/cart/component.js
solidus_admin-0.3.1 app/components/solidus_admin/orders/cart/component.js
solidus_admin-0.3.0 app/components/solidus_admin/orders/cart/component.js
solidus_admin-0.2.0 app/components/solidus_admin/orders/cart/component.js
solidus_admin-0.1.0 app/components/solidus_admin/orders/cart/component.js