1import { Controller } from '@hotwired/stimulus' 2import { get } from '@rails/request.js' 3 4// Reacts to a catalog entry picked in the generic autocomplete: reloads the 5// line-item form through `reform`, which fills title, description, price, unit 6// and tax rate server-side. 7// 8// The title field itself is untouched by this â it stays a plain text input, so 9// a position that exists in no catalog works exactly as before. 10export default class extends Controller { 11 static values = { reformUrl: String } 12 13 async apply (event) { 14 const serviceId = event.detail?.value 15 if (!serviceId) return 16 17 const url = new URL(this.reformUrlValue, document.baseURI) 18 19 // Carry the current form state along: `reform` rebuilds the line item from 20 // these params and needs `line_item[type]` to pick its STI class. 21 const form = this.element.closest('form') 22 if (form) { 23 for (const [key, value] of new FormData(form).entries()) { 24 if (value instanceof window.File) continue 25 if (key === 'line_item[title]') continue // replaced by the catalog entry 26 url.searchParams.append(key, value) 27 } 28 } 29 url.searchParams.set('line_item[service_id]', serviceId) 30 31 try { 32 const response = await get(url.toString(), { responseKind: 'turbo-stream' }) 33 if (!response.ok) console.error('catalog lookup failed', response) 34 } catch (error) { 35 console.error('catalog lookup failed', error) 36 } 37 } 38}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.