From 8b46a89e2c2275630999eb817518c89b016b1540 Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Thu, 16 Sep 2021 22:47:11 +1000 Subject: [PATCH] Add flash messages --- examples/actix_example/Cargo.toml | 1 + examples/actix_example/src/main.rs | 57 ++++++++++++++++--- .../actix_example/templates/index.html.tera | 8 +-- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/examples/actix_example/Cargo.toml b/examples/actix_example/Cargo.toml index 454d20fa..72ded81d 100644 --- a/examples/actix_example/Cargo.toml +++ b/examples/actix_example/Cargo.toml @@ -14,6 +14,7 @@ futures = { version = "^0.3" } futures-util = { version = "^0.3" } actix-http = "2" actix-web = "3" +actix-flash = "0.2" actix-files = "0.5" tera = "1.8.0" dotenv = "0.15" diff --git a/examples/actix_example/src/main.rs b/examples/actix_example/src/main.rs index 2812bbaf..84249ece 100644 --- a/examples/actix_example/src/main.rs +++ b/examples/actix_example/src/main.rs @@ -13,7 +13,7 @@ use listenfd::ListenFd; use sea_orm::entity::*; use sea_orm::query::*; use sea_orm::EntityTrait; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::env; use tera::Tera; @@ -34,8 +34,18 @@ pub struct Params { posts_per_page: Option, } +#[derive(Deserialize, Serialize, Debug, Clone)] +struct FlashData { + kind: String, + message: String, +} + #[get("/")] -async fn list(req: HttpRequest, data: web::Data) -> Result { +async fn list( + req: HttpRequest, + data: web::Data, + opt_flash: Option>, +) -> Result { let template = &data.templates; let conn = sea_orm::Database::connect(&data.db_url).await.unwrap(); @@ -47,6 +57,15 @@ async fn list(req: HttpRequest, data: web::Data) -> Result) -> Result) -> Result { async fn create( data: web::Data, post_form: web::Form, -) -> Result { +) -> actix_flash::Response { let conn = sea_orm::Database::connect(&data.db_url).await.unwrap(); let form = post_form.into_inner(); @@ -91,7 +112,12 @@ async fn create( .await .expect("could not insert post"); - Ok(HttpResponse::Found().header("location", "/").finish()) + let flash = FlashData { + kind: "success".to_owned(), + message: "Post successfully added.".to_owned(), + }; + + actix_flash::Response::with_redirect(flash.clone(), "/") } #[get("/{id}")] @@ -119,7 +145,7 @@ async fn update( data: web::Data, id: web::Path, post_form: web::Form, -) -> Result { +) -> actix_flash::Response { let conn = sea_orm::Database::connect(&data.db_url).await.unwrap(); let form = post_form.into_inner(); @@ -132,11 +158,19 @@ async fn update( .await .expect("could not edit post"); - Ok(HttpResponse::Found().header("location", "/").finish()) + let flash = FlashData { + kind: "success".to_owned(), + message: "Post successfully updated.".to_owned(), + }; + + actix_flash::Response::with_redirect(flash.clone(), "/") } #[post("/delete/{id}")] -async fn delete(data: web::Data, id: web::Path) -> Result { +async fn delete( + data: web::Data, + id: web::Path, +) -> actix_flash::Response { let conn = sea_orm::Database::connect(&data.db_url).await.unwrap(); let post: post::ActiveModel = Post::find_by_id(id.into_inner()) @@ -148,8 +182,12 @@ async fn delete(data: web::Data, id: web::Path) -> Result std::io::Result<()> { templates: templates, }) .wrap(middleware::Logger::default()) // enable logger + .wrap(actix_flash::Flash::default()) .configure(init) .service(fs::Files::new("/static", "./static").show_files_listing()) }); diff --git a/examples/actix_example/templates/index.html.tera b/examples/actix_example/templates/index.html.tera index 5b6131b8..4627b14d 100644 --- a/examples/actix_example/templates/index.html.tera +++ b/examples/actix_example/templates/index.html.tera @@ -2,11 +2,11 @@

Posts

- {# {% if flash %} - - {{ flash.1 }} + {% if flash_message %} + + {{ flash_message }} - {% endif %} #} + {% endif %}