Skip to content

Commit

Permalink
call me maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Jul 19, 2024
1 parent 9690862 commit 38c42f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sdl2 = { optional = true, version = "0.37.0", features = ["gfx"], default-featur
#wasm
base64 = { optional = true, version = "0.21.5"}
gloo-console = { optional = true, version = "0.3.0"}
gloo = { optional = true, version = "0.10", features = ["futures"] }
gloo = { optional = true, version = "0.10" }
js-sys = { optional = true, version = "0.3"}
web-sys = { optional = true, version = "0.3.69", features = ["FormData","HtmlFormElement", "File", "DragEvent", "DataTransfer"] }
web-sys = { optional = true, version = "0.3.69", features = ["Event", "FormData","HtmlFormElement", "File"] }
yew = { optional = true, git = "https://github.com/yewstack/yew/", features = ["csr"] }
67 changes: 43 additions & 24 deletions src/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::collections::HashMap;
use base64::{engine::general_purpose::STANDARD, Engine};
use gloo::file::{callbacks::FileReader, File};
use gloo_console::debug;
use web_sys::{FormData, HtmlFormElement};
use web_sys::File as RawFile;
use web_sys::{Event, FormData, HtmlFormElement};
use yew::prelude::*;

pub struct FileDetails {
Expand All @@ -16,7 +17,7 @@ pub struct FileDetails {

pub enum Msg {
Loaded(String, String, Vec<u8>),
Submit(File),
Submit(FormData),
}

pub struct App {
Expand Down Expand Up @@ -44,36 +45,54 @@ impl Component for App {
name: file_name.clone(),
});
self.readers.remove(&file_name);
true
}
Msg::Submit(file) => {
let link = ctx.link().clone();
let name = file.name().clone();
let file_type = file.raw_mime_type();
let task = {
gloo::file::callbacks::read_as_bytes(&file, move |res| {
link.send_message(Msg::Loaded(
name,
file_type,
res.expect("failed to read file"),
));
})
};
self.readers.insert(file.name(), task);
true
Msg::Submit(form) => {
// let link = ctx.link().clone();
// let name = file.name().clone();
// let file_type = file.raw_mime_type();
// let task = {
// gloo::file::callbacks::read_as_bytes(&file, move |res| {
// link.send_message(Msg::Loaded(
// name,
// file_type,
// res.expect("failed to read file"),
// ));
// })
// };
// self.readers.insert(file.name(), task);

// let form: HtmlFormElement = e.target_unchecked_into();
//
// let alt_text = form_data.get("alt-text");
// let image_file = File::from(RawFile::from(form_data.get("file-upload")));
// let link = ctx.link().clone();
// let name = image_file.name().clone();
// let file_type = image_file.raw_mime_type();
// let task = {
// gloo::file::callbacks::read_as_bytes(&image_file, move |res| {
// link.send_message(Msg::Loaded(
// name,
// file_type,
// res.expect("failed to read file"),
// ));
// })
// };
// self.readers.insert(name.clone(), task);
}
}
true
}

fn view(&self, _ctx: &Context<Self>) -> Html {
let onsubmit = |e: SubmitEvent| {
fn view(&self, ctx: &Context<Self>) -> Html {
let link = ctx.link().clone();

let onsubmit = move |e: SubmitEvent| {
let form: HtmlFormElement = e.target_unchecked_into();
let form_data = FormData::new_with_form(&form).expect("form data");
let alt_text = form_data.get("alt-text");
let image_file = form_data.get("file-upload");
debug!(alt_text, image_file);
//let image_file = File::from(RawFile::from(form_data.get("file-upload")));
debug!(form_data.clone()); //seems to be an empty formdata
link.send_message(Msg::Submit(form_data));
e.prevent_default();

};

html! {
Expand Down

0 comments on commit 38c42f9

Please sign in to comment.