Sha256: 96651a388a828e6e6139ed3244b4913cd1168c763f36e26e4c6d204596ad62f5

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["filter"];

  apply() {
    location.href = `${window.location.pathname}?${this.params}`;
  }

  post() {
    const token = document.head.querySelector(
      'meta[name="csrf-token"]'
    ).content;
    const path = `${window.location.pathname}/destroy_all?${this.params}`;
    fetch(path, {
      method: "DELETE",
      redirect: "follow",
      headers: {
        "Content-Type": "application/json",
        credentials: "same-origin",
      },
      body: JSON.stringify({ authenticity_token: token }),
    }).then((response) => {
      if (response.redirected) {
        window.location.href = response.url;
      }
    });
  }

  get params() {
    return this.activeFilterTargets()
      .map((t) => `${t.name}=${t.value}`)
      .join("&");
  }

  activeFilterTargets() {
    return this.filterTargets.filter(function (target) {
      if (target.type === "checkbox" || target.type === "radio")
        return target.checked;

      return target.value.length > 0;
    });
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_mini_profiler-0.6.0 app/javascript/js/filter_controller.js