Skip to content

Commit

Permalink
closure
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfingers23 committed Apr 8, 2024
1 parent 8e42811 commit d31777c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
30 changes: 19 additions & 11 deletions crates/kobold_router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ impl Router {
}
}

/// Add a route to the router
// pub fn add_route<F>(&mut self, route: &str, view: F)
// where
// F: Fn() + 'static,
// {
// self.router
// .borrow_mut()
// .insert(route, Box::new(move || view()))
// .expect_throw("failed to insert route");
// }

/// Add a route to the router
pub fn add_route(&mut self, route: &str, view: impl View) {
self.router
Expand All @@ -87,6 +76,25 @@ impl Router {
.expect_throw("failed to insert route");
}

pub fn add_route_with_params<V, T>(
&mut self,
route: &str,
params: T,
closure: impl FnOnce(T) -> V + 'static,
) where
V: kobold::View,
{
let view = move |params: T| {
let view = closure(params);
get_js_value(view)
};

self.router
.borrow_mut()
.insert(route, view(params))
.expect_throw("failed to insert route");
}

fn handle_route(&mut self) {
log_1(&JsValue::from_str("Ran route"));

Expand Down
42 changes: 24 additions & 18 deletions examples/router/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use kobold::prelude::*;
use kobold::View;
use kobold_router::ParamError;
use kobold_router::Router;
use wasm_bindgen::JsValue;
use web_sys::console::error_1;
Expand All @@ -25,24 +26,12 @@ fn navigate<'a>() -> impl View + 'a {
)
}

#[component(auto_branch)]
fn inventory() -> impl View + 'static {
#[component]
fn inventory(id: Option<usize>) -> impl View + 'static {
//TODO still need to remove need to get a new router
let router = get_router();
let id = router.get_param::<u64>("id");

match id {
Ok(id) => {
view! {
<h1> "ID: "{ id } </h1>
}
}
Err(param_err) => {
error_1(&JsValue::from_str(&format!("{:?}", param_err)));
view! {
<h1> "Could not read the value of id" </h1>
}
}
view! {
<h1> "ID: "{ id } </h1>
}
}

Expand Down Expand Up @@ -88,10 +77,27 @@ fn route_two(_state: &Hook<State>) -> impl View + '_ {

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("/inventory/{id}", view!(<!inventory>));
router.add_route_with_params("/inventory/{id}", (2, ""), |(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 d31777c

Please sign in to comment.