Refactor flash

This commit is contained in:
Sam Samai 2021-09-17 20:17:17 +10:00
parent 8b46a89e2c
commit d623474d7c
2 changed files with 8 additions and 14 deletions

View File

@ -57,15 +57,6 @@ async fn list(
let paginator = Post::find().paginate(&conn, posts_per_page);
let num_pages = paginator.num_pages().await.ok().unwrap();
let mut flash_message = String::new();
let mut flash_kind = String::new();
if let Some(flash) = opt_flash {
let flash_inner = flash.into_inner();
flash_message = flash_inner.message.to_owned();
flash_kind = flash_inner.kind.to_owned();
}
let posts = paginator
.fetch_page(page)
.await
@ -75,8 +66,11 @@ async fn list(
ctx.insert("page", &page);
ctx.insert("posts_per_page", &posts_per_page);
ctx.insert("num_pages", &num_pages);
ctx.insert("flash_message", &flash_message);
ctx.insert("flash_kind", &flash_kind);
if let Some(flash) = opt_flash {
let flash_inner = flash.into_inner();
ctx.insert("flash", &flash_inner);
}
let body = template
.render("index.html.tera", &ctx)

View File

@ -2,9 +2,9 @@
<div class="container">
<p><!--Nothing to see here --></p>
<h1>Posts</h1>
{% if flash_message %}
<small class="field-{{ flash_kind }}-flash">
{{ flash_message }}
{% if flash %}
<small class="field-{{ flash.kind }}-flash">
{{ flash.message }}
</small>
{% endif %}
<table>