import { Application } from "stimulus" import { definitionsFromContext } from "stimulus/webpack-helpers" const application = Application.start() const context = require.context("controllers", true, /_controller\.js$/) application.load(definitionsFromContext(context))
import { Controller } from "stimulus"; import Dropzone from "dropzone"; import "dropzone/dist/min/dropzone.min.css"; import "dropzone/dist/min/basic.min.css"; import { DirectUpload } from "@rails/activestorage"; export default class extends Controller { static targets = ["input"]; connect() { Dropzone.autoDisc = false; this.inputTarget.disable = true; this.inputTarget.style.display = "none"; const dropzone = new Dropzone(this.element, { url: "/", maxFiles: "10", maxFilesize: "10", autoQueue: false, }); dropzone.on("addedfile", (file) => { setTimeout(() => { if (file.accepted) { const upload = new DirectUpload(file, this.url); upload.create((error, blob) => { this.hiddenInput = document.createElement("input"); this.hiddenInput.type = "hidden"; this.hiddenInput.name = this.inputTarget.name; this.hiddenInput.value = blob.signed_id; this.inputTarget.parentNode.insertBefore( this.hiddenInput, this.inputTarget.nextSibling ); dropzone.emit("success", file); dropzone.emit("complete", file); }); } }, 500); }); } get url() { return this.inputTarget.getAttribute("data-direct-upload-url"); }}
= form_with(model: guest, multipart: true, local: true, id:"new_gues") do |form|
.dropzone.dropzone-default.dz-clickable{"data-controller" => "dropzone", "data-dropzone-max-file-size" => "2", "data-dropzone-max-files" => "1"} = form.file_field :avatar, direct_upload: true, class: 'form-input py-10', accept: 'image/png, image/jpeg, image/gif, image/tiff', size_limit: 2_000_000, data: { target: 'dropzone.input' } .text-gray-600.dropzone-msg.dz-message.needsclick %h3.text-base.font-bold.dropzone-msg-title Drag or click to upload your Company logo %span.text-xs.dropzone-msg-desc Choose a square image with a solid background colour of at least 100x100px. - if @guest.avatar.attached? = image_tag(@guest.avatar, class: "rounded-full h-20 w-20 mx-auto mt-2")