Skip to content

Commit

Permalink
feat: provide leptos_router::Method via context (#1808) (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoomiti authored Feb 27, 2024
1 parent aa97700 commit 4809cf4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
15 changes: 10 additions & 5 deletions integrations/actix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,13 +1215,18 @@ where
let mode = listing.mode();

for method in listing.methods() {
let additional_context = additional_context.clone();
let additional_context_and_method = move || {
provide_context(method);
additional_context();
};
router = if let Some(static_mode) = listing.static_mode() {
router.route(
path,
static_route(
options.clone(),
app_fn.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
method,
static_mode,
),
Expand All @@ -1233,15 +1238,15 @@ where
SsrMode::OutOfOrder => {
render_app_to_stream_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
)
}
SsrMode::PartiallyBlocked => {
render_app_to_stream_with_context_and_replace_blocks(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
true,
Expand All @@ -1250,14 +1255,14 @@ where
SsrMode::InOrder => {
render_app_to_stream_in_order_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
)
}
SsrMode::Async => render_app_async_with_context(
options.clone(),
additional_context.clone(),
additional_context_and_method.clone(),
app_fn.clone(),
method,
),
Expand Down
15 changes: 10 additions & 5 deletions integrations/axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,11 @@ where
let path = listing.path();

for method in listing.methods() {
let cx_with_state = cx_with_state.clone();
let cx_with_state_and_method = move || {
provide_context(method);
cx_with_state();
};
router = if let Some(static_mode) = listing.static_mode() {
#[cfg(feature = "default")]
{
Expand All @@ -1623,7 +1628,7 @@ where
path,
LeptosOptions::from_ref(options),
app_fn.clone(),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
method,
static_mode,
)
Expand All @@ -1643,7 +1648,7 @@ where
SsrMode::OutOfOrder => {
let s = render_app_to_stream_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {
Expand All @@ -1657,7 +1662,7 @@ where
SsrMode::PartiallyBlocked => {
let s = render_app_to_stream_with_context_and_replace_blocks(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
true
);
Expand All @@ -1672,7 +1677,7 @@ where
SsrMode::InOrder => {
let s = render_app_to_stream_in_order_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {
Expand All @@ -1686,7 +1691,7 @@ where
SsrMode::Async => {
let s = render_app_async_with_context(
LeptosOptions::from_ref(options),
cx_with_state.clone(),
cx_with_state_and_method.clone(),
app_fn.clone(),
);
match method {
Expand Down
5 changes: 5 additions & 0 deletions router/src/components/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ impl RouteContext {
pub fn outlet(&self) -> impl IntoView {
(self.inner.outlet)()
}

/// The http method used to navigate to this route. Defaults to [`Method::Get`] when unavailable like in client side routing
pub fn method(&self) -> Method {
use_context().unwrap_or_default()
}
}

pub(crate) struct RouteContextInner {
Expand Down

0 comments on commit 4809cf4

Please sign in to comment.