Render flash messages

This commit is contained in:
Sam Samai 2021-09-02 22:30:23 +10:00
parent 5f47421b38
commit 39b5cb7f33
3 changed files with 18 additions and 9 deletions

View File

@ -4,6 +4,7 @@ extern crate rocket;
use rocket::fairing::{self, AdHoc}; use rocket::fairing::{self, AdHoc};
use rocket::form::{Context, Form}; use rocket::form::{Context, Form};
use rocket::fs::{relative, FileServer}; use rocket::fs::{relative, FileServer};
use rocket::request::FlashMessage;
use rocket::response::{Flash, Redirect}; use rocket::response::{Flash, Redirect};
use rocket::serde::json::Json; use rocket::serde::json::Json;
use rocket::{Build, Request, Rocket}; use rocket::{Build, Request, Rocket};
@ -46,18 +47,20 @@ async fn create(conn: Connection<Db>, post_form: Form<post::Model>) -> Flash<Red
} }
#[get("/")] #[get("/")]
async fn list(conn: Connection<Db>) -> Template { async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template {
let posts = Post::find() let posts = Post::find()
.all(&conn) .all(&conn)
.await .await
.expect("could not retrieve posts") .expect("could not retrieve posts")
.into_iter() .into_iter()
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let flash = flash.map(FlashMessage::into_inner);
Template::render( Template::render(
"index", "index",
context! { context! {
posts: posts, posts: posts,
flash: flash,
}, },
) )
} }

View File

@ -2,24 +2,24 @@
border: 1px solid #ff0000 !important; border: 1px solid #ff0000 !important;
} }
.field-error-msg { .field-error-flash {
color: #ff0000; color: #ff0000;
display: block; display: block;
margin: -10px 0 10px 0; margin: -10px 0 10px 0;
} }
.field-success { .field-success {
border: 1px solid #5AB953 !important; border: 1px solid #5ab953 !important;
} }
.field-success-msg { .field-success-flash {
color: #5AB953; color: #5ab953;
display: block; display: block;
margin: -10px 0 10px 0; margin: -10px 0 10px 0;
} }
span.completed { span.completed {
text-decoration: line-through; text-decoration: line-through;
} }
form.inline { form.inline {
@ -29,7 +29,7 @@ form.inline {
form.link, form.link,
button.link { button.link {
display: inline; display: inline;
color: #1EAEDB; color: #1eaedb;
border: none; border: none;
outline: none; outline: none;
background: none; background: none;
@ -45,8 +45,9 @@ button.link {
letter-spacing: inherit; letter-spacing: inherit;
} }
form.link:hover, button.link:hover { form.link:hover,
color: #0FA0CE; button.link:hover {
color: #0fa0ce;
} }
button.small { button.small {

View File

@ -1,5 +1,10 @@
{% extends "base" %} {% block content %} {% extends "base" %} {% block content %}
<h1>Posts</h1> <h1>Posts</h1>
{% if flash %}
<small class="field-{{ flash.0 }}-flash">
{{ flash.1 }}
</small>
{% endif %}
<table> <table>
<tbody> <tbody>
<thead> <thead>