From ba71c73836c084c95ca89021f9c9a455d8c94134 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Wed, 14 Feb 2018 14:11:54 -0500 Subject: Move hard coded URLs to computed data attributes --- app/javascript/activetext/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'app/javascript') diff --git a/app/javascript/activetext/index.js b/app/javascript/activetext/index.js index cdcd1d311f..e1c59dd50d 100644 --- a/app/javascript/activetext/index.js +++ b/app/javascript/activetext/index.js @@ -1,14 +1,12 @@ import * as Trix from "trix" import { DirectUpload } from "activestorage" -// FIXME: Hard coded routes -const directUploadsURL = "/rails/active_storage/direct_uploads" -const blobsURL = "/rails/active_storage/blobs" - addEventListener("trix-attachment-add", event => { const { attachment } = event if (!attachment.file) return + const { directUploadUrl, blobUrlTemplate } = event.target.dataset + const delegate = { directUploadWillStoreFileWithXHR: (xhr) => { xhr.upload.addEventListener("progress", event => { @@ -18,17 +16,19 @@ addEventListener("trix-attachment-add", event => { } } - const directUpload = new DirectUpload(attachment.file, directUploadsURL, delegate) + const directUpload = new DirectUpload(attachment.file, directUploadUrl, delegate) directUpload.create((error, attributes) => { if (error) { console.warn("Failed to store file for attachment", attachment, error) } else { - console.log("Created blob for attachment", attributes, attachment) - attachment.setAttributes({ - url: `${blobsURL}/${attributes.signed_id}/${encodeURIComponent(attachment.file.name)}`, - sgid: attributes.attachable_sgid - }) + const sgid = attributes.attachable_sgid + + const url = blobUrlTemplate + .replace(":signed_id", attributes.signed_id) + .replace(":filename", encodeURIComponent(attributes.filename)) + + attachment.setAttributes({ sgid, url }) } }) }) -- cgit v1.2.3