Beginning of frontend + recipe editing

(it's a mess)
This commit is contained in:
Greg Burri 2022-12-13 21:01:18 +01:00
parent 642dd8a80c
commit cbe276fc06
16 changed files with 203 additions and 63 deletions

View file

@ -14,18 +14,18 @@ default = ["console_error_panic_hook"]
common = {path = "../common"}
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ['console', 'Document', 'Element', 'HtmlElement', 'Node', 'Window'] }
web-sys = {version = "0.3", features = ['console', 'Document', 'Element', 'HtmlElement', 'Node', 'Window', 'Location']}
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1", optional = true }
console_error_panic_hook = {version = "0.1", optional = true}
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
wee_alloc = { version = "0.4", optional = true }
wee_alloc = {version = "0.4", optional = true}
# [dev-dependencies]
# wasm-bindgen-test = "0.3"

6
frontend/src/handles.rs Normal file
View file

@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;
use web_sys::Document;
pub fn edit_recipe(doc: &Document) {
}

View file

@ -1,4 +1,5 @@
mod utils;
mod handles;
use wasm_bindgen::prelude::*;
use web_sys::console;
@ -22,14 +23,41 @@ pub fn greet(name: &str) {
#[wasm_bindgen(start)]
pub fn main() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
let window = web_sys::window().expect("no global `window` exists");
let document = window.document().expect("should have a document on window");
let body = document.body().expect("document should have a body");
//let body = document.body().expect("document should have a body");
let val = document.create_element("p")?;
val.set_inner_html("Hello from Rust!");
let location = window.location().pathname()?;
let path: Vec<&str> = location.split('/').skip(1).collect();
body.append_child(&val)?;
/*
* Todo:
* [ok] get url (/recipe/edit/{id}) and extract the id
* - Add a handle (event?) to the title field (when edited/changed?):
* - Call (as AJAR) /ron-api/set-title and set the body to a serialized RON of the type common::ron_api::SetTitle
* - Display error message if needed
*/
match path[..] {
["recipe", "edit", id] => {
let id = id.parse::<i64>().unwrap(); // TODO: remove unwrap.
console_log!("recipe edit ID: {}", id);
handles::edit_recipe(&document);
let title_input = document.get_element_by_id("title_field").unwrap();
},
_ => (),
}
//alert(&path);
// TEST
// let val = document.create_element("p")?;
// val.set_inner_html("Hello from Rust!");
// body.append_child(&val)?;
Ok(())
}

View file

@ -1,3 +1,5 @@
use web_sys::console;
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
@ -8,3 +10,10 @@ pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[macro_export]
macro_rules! console_log {
// Note that this is using the `log` function imported above during
// `bare_bones`
($($t:tt)*) => (console::log_1(&format_args!($($t)*).to_string().into()))
}