Open sebyx07 opened 4 years ago
@catmando if you could help me with error, after the example. Would be great, then I can add the progress bar
# a direct upload implementation
class DirectUploader < HyperComponent
param :upload_url # access it as @UploadUrl
param :upload_name# access it as @UploadName
render(DIV) do
input = INPUT(
id: @UploadName,
type: :file, accept: "image/*", multiple: true
).on(:change) { |event| upload_files(event) }
LABEL(htmlFor: "contained-button-file") do
Button(variant: :contained, component: :span) do
"Upload"
end.on(:click) { jQ[input].click } # select the input with jQ, then trigger a click on it, check initializer to have jquery enabled
end
end
def upload_files(event)
event.target.files.to_a.each do |file|
upload_file(file)
end
end
def upload_file(file)
Uploader.new(file, @UploadUrl, @UploadName).call!
end
class Uploader
def initialize(file, upload_url, upload_name)
@upload_url = upload_url
@upload_name = upload_name
@upload = Native(`new ActiveStorage.DirectUpload(#{file.to_n}, "/rails/active_storage/direct_uploads")`) # with Native you can native js, like ActiveStorage.DirectUpload or w/e
end
def call!
@upload.create do |err, blob| # handle callback
err = Native(err) # convert again to ruby/opal objects
blob = Native(blob) # convert again to ruby/opal objects
next p err if err
data = { @upload_name => blob.signed_id }
Hyperstack::HTTP.post(@upload_url, payload: data) do
p "DONE"
end
end
end
def directUploadWillStoreFileWithXHR(request) # TODO
p Native(request)
end
def directUploadDidProgress(event) # Todo
p Native(event)
end
end
end
The error happens on @upload.create do |err, blob|
//error
broadcast.rb:158 Uncaught TypeError: Cannot read property 'prototype' of undefined
at singleton_class_alloc.$$eval_native_react_component [as $eval_native_react_component] (react_wrapper.rb:34)
at singleton_class_alloc.TMP_ReactWrapper_native_react_component$q_4 [as $native_react_component?] (react_wrapper.rb:46)
at singleton_class_alloc.$$create_component_wrapper [as $create_component_wrapper] (native_library.rb:73)
at singleton_class_alloc.$$import_const_from_native [as $import_const_from_native] (native_library.rb:40)
at singleton_class_alloc.$$const_missing [as $const_missing] (auto-import.rb:17)
at const_missing (runtime.self-a59b72816f596f5bb11f5da568edd0b684eec56ddaff0ae5da9834528996763d.js?body=1:211)
at Object.Opal.const_get_relative (runtime.self-a59b72816f596f5bb11f5da568edd0b684eec56ddaff0ae5da9834528996763d.js?body=1:284)
at singleton_class_alloc.$$const_get [as $const_get] (module.rb:296)
at TMP_26 (module.rb:305)
at Object.Opal.yieldX (runtime.self-a59b72816f596f5bb11f5da568edd0b684eec56ddaff0ae5da9834528996763d.js?body=1:1411)
hey @excid3 if you could some tutorial for Hyperstack + Material Design. I think this way hotter than rails5-6 features. It's the missing feature....
It's like wysiwyg. Even action-text can be integrated with ease. DirectUpload took me ~10mins
@sebyx07 brilliant work!
Might be useful for others. The results are really impressive
yarn add @material-ui/core --save
I'll keep you guys posted if I have a example running DirectUpload from ActiveStorage.