TrestleAdmin/trestle

View on GitHub
frontend/js/controllers/application_controller.js

Summary

Maintainability
A
0 mins
Test Coverage
import { Controller } from '@hotwired/stimulus'

import { fetchWithErrorHandling, fetchTurboStream } from '../core/fetch'

export default class extends Controller {
  appendAction (event, action, element = this.element) {
    const actions = element.dataset.action ? element.dataset.action.split(' ') : []
    actions.push(`${event}->${this.identifier}#${action}`)
    element.dataset.action = actions.join(' ')
  }

  fetch (url, options = {}) {
    return fetchWithErrorHandling(url, options)
  }

  fetchTurboStream (url, options = {}) {
    return fetchTurboStream(url, options)
  }

  get csrfToken () {
    return document.querySelector("[name='csrf-token']").content
  }
}