X-Git-Url: http://git.euphorik.ch/?p=recipes.git;a=blobdiff_plain;f=frontend%2Fsrc%2Flib.rs;fp=frontend%2Fsrc%2Flib.rs;h=aea6567d9fffc356cde94118d7b89a917ca6ddc8;hp=1607fc0c5a2084147d4235d220e3d34a41e698d0;hb=cbe276fc0601041b13087a6ffd80c5b126dfbe59;hpb=642dd8a80ce2e1212b8e30c1edabb32bdb416cfc diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 1607fc0..aea6567 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -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 location = window.location().pathname()?; + let path: Vec<&str> = location.split('/').skip(1).collect(); + + /* + * 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::().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(); + }, + _ => (), + } - let val = document.create_element("p")?; - val.set_inner_html("Hello from Rust!"); + //alert(&path); - body.append_child(&val)?; + // TEST + // let val = document.create_element("p")?; + // val.set_inner_html("Hello from Rust!"); + // body.append_child(&val)?; Ok(()) } \ No newline at end of file