Skip to content

Commit

Permalink
refactor: add inner_back method to InertiaResponder trait and mov…
Browse files Browse the repository at this point in the history
…e redirecting back logic to it in actix web implementation
  • Loading branch information
KaioFelps committed Jan 7, 2025
1 parent 06684b6 commit 59dea9e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/inertia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub trait InertiaResponder<TResponder, THttpRequest, TRedirect> {
props: InertiaProps<'b>,
) -> Result<TResponder, InertiaError>;

fn inner_back(&self, req: &THttpRequest) -> TRedirect;

fn inner_back_with_errors(&self, req: &THttpRequest, errors: HashMap<&str, Value>)
-> TRedirect;

Expand Down
4 changes: 2 additions & 2 deletions src/providers/actix_provider/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::facade::InertiaFacade;
use crate::inertia::InertiaResponder;
use crate::utils::inertia_err_msg;
use crate::{hashmap, Component, Inertia, InertiaError, InertiaProps};
use crate::{Component, Inertia, InertiaError, InertiaProps};
use actix_web::web::{Data, Redirect};
use actix_web::{HttpRequest, HttpResponse};
use async_trait::async_trait;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl InertiaFacade<HttpRequest, HttpResponse, Redirect> for Inertia {
#[inline]
fn back(req: &HttpRequest) -> Redirect {
let inertia = extract_inertia(req);
inertia.inner_back_with_errors(req, hashmap![])
inertia.inner_back(req)
}

#[inline]
Expand Down
9 changes: 7 additions & 2 deletions src/providers/actix_provider/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl InertiaResponder<HttpResponse, HttpRequest, Redirect> for Inertia {
}

#[inline]
fn inner_back_with_errors(&self, req: &HttpRequest, errors: HashMap<&str, Value>) -> Redirect {
fn inner_back(&self, req: &HttpRequest) -> Redirect {
let session = req.extensions().get::<InertiaTemporarySession>().cloned();

let previous_uri = if let Some(session) = session {
Expand All @@ -172,6 +172,11 @@ impl InertiaResponder<HttpResponse, HttpRequest, Redirect> for Inertia {
})
};

Redirect::new(req.uri().to_string(), previous_uri).using_status_code(StatusCode::FOUND)
}

#[inline]
fn inner_back_with_errors(&self, req: &HttpRequest, errors: HashMap<&str, Value>) -> Redirect {
if !errors.is_empty() {
let mut errors_map = Map::new();

Expand All @@ -192,7 +197,7 @@ impl InertiaResponder<HttpResponse, HttpRequest, Redirect> for Inertia {
.insert(SessionErrors(resolve_session_errors(errors_map, req)));
}

Redirect::new(req.uri().to_string(), previous_uri).using_status_code(StatusCode::FOUND)
self.inner_back(req)
}
}

Expand Down

0 comments on commit 59dea9e

Please sign in to comment.