Beginning of frontend + recipe editing
[recipes.git] / frontend / src / lib.rs
index 1607fc0..aea6567 100644 (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 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::<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();
+        },
+        _ => (),
+    }
 
-    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