65fbac1f74648e59af4ac5402b6854123e215183
[recipes.git] / frontend / src / lib.rs
1 mod utils;
2 mod handles;
3
4 use wasm_bindgen::prelude::*;
5 use web_sys::console;
6
7 // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
8 // allocator.
9 #[cfg(feature = "wee_alloc")]
10 #[global_allocator]
11 static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
12
13 #[wasm_bindgen]
14 extern {
15 fn alert(s: &str);
16 }
17
18 #[wasm_bindgen]
19 pub fn greet(name: &str) {
20 alert(&format!("Hello, {}!", name));
21 console::log_1(&"Hello bg".into());
22 }
23
24 #[wasm_bindgen(start)]
25 pub fn main() -> Result<(), JsValue> {
26 console_error_panic_hook::set_once();
27
28 let window = web_sys::window().expect("no global `window` exists");
29 let document = window.document().expect("should have a document on window");
30 //let body = document.body().expect("document should have a body");
31
32 let location = window.location().pathname()?;
33 let path: Vec<&str> = location.split('/').skip(1).collect();
34
35 /*
36 * TODO:
37 * [ok] get url (/recipe/edit/{id}) and extract the id
38 * - Add a handle (event?) to the title field (when edited/changed?):
39 * - Call (as AJAR) /ron-api/set-title and set the body to a serialized RON of the type common::ron_api::SetTitle
40 * - Display error message if needed
41 */
42
43 match path[..] {
44 ["recipe", "edit", id] => {
45 let id = id.parse::<i64>().unwrap(); // TODO: remove unwrap.
46 console_log!("recipe edit ID: {}", id);
47
48 handles::edit_recipe(&document);
49
50 let title_input = document.get_element_by_id("title_field").unwrap();
51 },
52 _ => (),
53 }
54
55 //alert(&path);
56
57 // TEST
58 // let val = document.create_element("p")?;
59 // val.set_inner_html("Hello from Rust!");
60 // body.append_child(&val)?;
61
62 Ok(())
63 }