Sha256: 649d14e8a48da40f05d96734da8e1791a279ff5c9c8b8341febbdb054698afec

Contents?: true

Size: 1.91 KB

Versions: 13

Compression:

Stored size: 1.91 KB

Contents

import { DirectUpload } from "@rails/activestorage";

export class Uploader {
  constructor(modal, options) {
    this.modal = modal;
    this.options = options;
    this.validationSent = false;
    this.errors = []

    if (modal.options.maxFileSize && options.file.size > modal.options.maxFileSize) {
      this.errors = [modal.locales.file_size_too_large]
    } else {
      this.upload = new DirectUpload(options.file, options.url, this);
    }
  }

  validate(blobId) {
    const callback = (data) => {
      let errors = []
      for (const [, value] of Object.entries(data)) {
        errors = errors.concat(value);
      }

      if (errors.length) {
        this.errors = errors;
      }
    }

    if (!this.validationSent) {
      let property = this.modal.options.addAttribute;
      if (this.modal.options.titled) {
        property = "file"
      }

      let url = this.modal.input.dataset.uploadValidationsUrl;

      const params = new URLSearchParams({
        resourceClass: this.modal.options.resourceClass,
        property: property,
        blob: blobId,
        formClass: this.modal.options.formObjectClass
      });

      return fetch(`${url}?${params}`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "X-CSRF-Token": $("meta[name=csrf-token]").attr("content")
        }
      }).
        then((response) => response.json()).
        then((data) => {
          this.validationSent = true;
          callback(data);
        });
    }

    return Promise.resolve()
  }

  // The following method come from @rails/activestorage
  // {@link https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-upload-javascript-events Active Storage Rails guide}
  directUploadWillStoreFileWithXHR(request) {
    request.upload.addEventListener("progress", ({ loaded, total }) => this.modal.setProgressBar(this.options.attachmentName, Math.floor(loaded / total * 100)));
  }
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.4 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.29.0 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.3 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.29.0.rc4 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.29.0.rc3 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.29.0.rc2 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.29.0.rc1 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.2 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.1 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.0 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.0.rc5 app/packs/src/decidim/direct_uploads/uploader.js
decidim-core-0.28.0.rc4 app/packs/src/decidim/direct_uploads/uploader.js