Skip to content

Commit

Permalink
Route setup syntax is cleaner, now just need it to work
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfingers23 committed Apr 8, 2024
1 parent d31777c commit c2034e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
15 changes: 9 additions & 6 deletions crates/kobold_router/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use kobold::dom::Mountable;
use kobold::internal::In;
use kobold::prelude::*;
use kobold::{dom::Mountable, internal::Out};
use matchit::Match;
use std::cell::RefCell;
use std::rc::Rc;
Expand All @@ -9,6 +9,7 @@ use web_sys::console::log_1;

mod internal;

//TODO need to change back to a fnonce of some sort so i can pass params to it
/// Routes type
type Routes = matchit::Router<JsValue>;

Expand Down Expand Up @@ -79,7 +80,6 @@ impl Router {
pub fn add_route_with_params<V, T>(
&mut self,
route: &str,
params: T,
closure: impl FnOnce(T) -> V + 'static,
) where
V: kobold::View,
Expand All @@ -89,10 +89,13 @@ impl Router {
get_js_value(view)
};

self.router
.borrow_mut()
.insert(route, view(params))
.expect_throw("failed to insert route");
// todo!();
// let JsValue = view((1, "foo"));

// self.router
// .borrow_mut()
// .insert(route, JsValue)
// .expect_throw("failed to insert route");
}

fn handle_route(&mut self) {
Expand Down
19 changes: 3 additions & 16 deletions examples/router/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use kobold_router::ParamError;
use kobold_router::Router;
use wasm_bindgen::JsValue;
use web_sys::console::error_1;
use web_sys::console::log_1;

#[component]
fn navigate<'a>() -> impl View + 'a {
Expand Down Expand Up @@ -79,26 +80,12 @@ fn get_router() -> Router {
let mut router = Router::new();
router.add_route("/one", stateful(State::default, route_one));
router.add_route("/two", stateful(State::default, route_two));
router.add_route_with_params("/inventory/{id}", (2, ""), |(id, foo)| {
router.add_route_with_params("/inventory/{id}", |(id, foo): (usize, &str)| {
log_1(&JsValue::from_str(&format!("id: {}, foo: {}", id, foo)));
view! {
<!inventory id={ Some(id) }>
}
});
// router.add_route_with_params("/inventory/{id}", ||(id, foo): (usize, &str), {
// |params| {
// // let id = params.get(0).unwrap().as_f64().unwrap() as usize;
// view! {
// <!inventory id={ Some(1) }>
// }
// }
// });
// router.add_route_with_params("/inventory/{id}", |(id, foo): (usize, &str)| {
// view! {
// <h1> "ID: "{ id } </h1>
// <p> "Foo: "{ foo } </p>
// }
// });
// router.add_route("/route-test", view!(<!navigate>));

router
}
Expand Down

0 comments on commit c2034e3

Please sign in to comment.