Sha256: b49b8e519ec51fad5dfc94c532f06cab3de54228be43e3e0885914be7facb38b

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

import { Controller } from "@hotwired/stimulus"
import AttachmentUploadController from "./attachment_upload_controller";
import { get } from '@rails/request.js'

/***
 * Delete attachments controller
 *
 * Deletes an attachments
 */
export default class AttachmentDeleteController extends Controller {
  connect() {}

  delete(event) {
    const self = this

    event.stopPropagation()
    event.preventDefault()

    const formData = new FormData()
    formData.append("_method", "DELETE")

    fetch(self.element.getAttribute("href"), {
      method: "POST",
      headers: {
        "X-CSRF-Token": document.querySelector("meta[name=csrf-token]").content,
      },
      body: formData,
    })
      .then(self.handleErrors)
      .then((response) => {
        window.location.reload(true);
        response.json().then(function (data) {
          let node = document.querySelector(data.selector)
          if (node) {
            node.innerHTML = data.html
          }
        })
      })
      .catch((error) => {
        console.log(error)
      })
    return false
  }

  handleErrors(response) {
    if (!response.ok) throw new Error(response.status)
    return response
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
satis-2.1.47 app/javascript/satis/controllers/attachment_delete_controller.js