Skip to content

Commit

Permalink
chore: update to gloo-net 0.5 (closes #2411) (#2416)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj authored Mar 8, 2024
1 parent 7928f61 commit 480d741
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ leptos_integration_utils = { workspace = true, optional = true }
leptos_meta = { workspace = true, optional = true }
cached = { version = "0.45.0", optional = true }
cfg-if = "1"
gloo-net = { version = "0.2", features = ["http"] }
gloo-net = { version = "0.5", features = ["http"] }
lazy_static = "1"
linear-map = { version = "1", features = ["serde_impl"] }
once_cell = "1.18"
Expand Down
47 changes: 32 additions & 15 deletions router/src/components/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ pub fn Form<A>(
where
A: ToHref + 'static,
{
async fn post_form_data(
action: &str,
form_data: FormData,
) -> Result<gloo_net::http::Response, gloo_net::Error> {
gloo_net::http::Request::post(action)
.header("Accept", "application/json")
.redirect(RequestRedirect::Follow)
.body(form_data)?
.send()
.await
}

async fn post_params(
action: &str,
enctype: &str,
params: web_sys::UrlSearchParams,
) -> Result<gloo_net::http::Response, gloo_net::Error> {
gloo_net::http::Request::post(action)
.header("Accept", "application/json")
.header("Content-Type", enctype)
.redirect(RequestRedirect::Follow)
.body(params)?
.send()
.await
}

fn inner(
has_router: bool,
method: Option<&'static str>,
Expand Down Expand Up @@ -138,12 +164,7 @@ where
let on_response = on_response.clone();
let on_error = on_error.clone();
spawn_local(async move {
let res = gloo_net::http::Request::post(&action)
.header("Accept", "application/json")
.redirect(RequestRedirect::Follow)
.body(form_data)
.send()
.await;
let res = post_form_data(&action, form_data).await;
match res {
Err(e) => {
error!("<Form/> error while POSTing: {e:#?}");
Expand All @@ -155,14 +176,15 @@ where
}
}
Ok(resp) => {
let resp = web_sys::Response::from(resp);
if let Some(version) = action_version {
version.update(|n| *n += 1);
}
if let Some(error) = error {
error.try_set(None);
}
if let Some(on_response) = on_response.clone() {
on_response(resp.as_raw());
on_response(&resp);
}
// Check all the logical 3xx responses that might
// get returned from a server function
Expand Down Expand Up @@ -216,13 +238,7 @@ where
let on_response = on_response.clone();
let on_error = on_error.clone();
spawn_local(async move {
let res = gloo_net::http::Request::post(&action)
.header("Accept", "application/json")
.header("Content-Type", &enctype)
.redirect(RequestRedirect::Follow)
.body(params)
.send()
.await;
let res = post_params(&action, &enctype, params).await;
match res {
Err(e) => {
error!("<Form/> error while POSTing: {e:#?}");
Expand All @@ -234,14 +250,15 @@ where
}
}
Ok(resp) => {
let resp = web_sys::Response::from(resp);
if let Some(version) = action_version {
version.update(|n| *n += 1);
}
if let Some(error) = error {
error.try_set(None);
}
if let Some(on_response) = on_response.clone() {
on_response(resp.as_raw());
on_response(&resp);
}
// Check all the logical 3xx responses that might
// get returned from a server function
Expand Down

0 comments on commit 480d741

Please sign in to comment.