From 28b501810ea9c988313aaea7c59b58a743964fe0 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 23 Feb 2022 12:17:48 +0530 Subject: [PATCH] feat: separate navigation bars for logged and unauthenticated users SUMMARY Authenticated and unauthenticated users have different requirements. Links to profile and settings are irrelevant for unauthenticated users. So separate navigation bars. USAGE crate::pages::auth_ctx Authenticated user context should be supplied on every authenticated route template render. Authenticated navigation bar depends on "loggedin_user" to render profile link. AUTH_NAV("auth_nav") Should only be used in authenticated routes. Depends on "loggedin_user" authenticated user context, which can't be supplied in unauthenticated routes. NOTES Currently, there's (template)code duplication, when inheritance for "include" templates are implemented in Tera crate, navigation bars implementations should be refactored to avoid duplication. --- src/pages/mod.rs | 23 ++++++++++++---- templates/components/nav/auth.html | 26 +++++++++++++++++++ templates/components/nav/base.html | 18 +++++++++++++ .../components/{pub-nav.html => nav/pub.html} | 0 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 templates/components/nav/auth.html create mode 100644 templates/components/nav/base.html rename templates/components/{pub-nav.html => nav/pub.html} (100%) diff --git a/src/pages/mod.rs b/src/pages/mod.rs index bd986b0..5f9ce05 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -57,12 +57,13 @@ pub const PAYLOAD_KEY: &str = "payload"; pub const BASE: TemplateFile = TemplateFile::new("base", "components/base.html"); pub const FOOTER: TemplateFile = TemplateFile::new("footer", "components/footer.html"); -pub const PUB_NAV: TemplateFile = TemplateFile::new("pub_nav", "components/pub-nav.html"); +pub const PUB_NAV: TemplateFile = TemplateFile::new("pub_nav", "components/nav/pub.html"); +pub const AUTH_NAV: TemplateFile = TemplateFile::new("auth_nav", "components/nav/auth.html"); lazy_static! { pub static ref TEMPLATES: Tera = { let mut tera = Tera::default(); - for t in [BASE, FOOTER, PUB_NAV].iter() { + for t in [BASE, FOOTER, PUB_NAV, AUTH_NAV].iter() { t.register(&mut tera).unwrap(); } errors::register_templates(&mut tera); @@ -94,6 +95,16 @@ pub fn context(s: &Settings) -> Context { ctx } +pub fn auth_ctx(user: &str, s: &Settings) -> Context { + let mut ctx = Context::new(); + let footer = Footer::new(s); + ctx.insert("footer", &footer); + ctx.insert("page", &PAGES); + ctx.insert("assets", &*ASSETS); + ctx.insert("loggedin_user", user); + ctx +} + #[derive(Serialize)] pub struct Footer<'a> { version: &'a str, @@ -125,17 +136,19 @@ pub async fn home() -> impl Responder { #[cfg(test)] mod tests { - use super::{auth, errors, BASE, FOOTER, PUB_NAV}; - use tera::Tera; #[test] - fn templates_work() { + fn templates_work_basic() { + use super::*; + use tera::Tera; + let mut tera = Tera::default(); let mut tera2 = Tera::default(); for t in [ BASE, FOOTER, PUB_NAV, + AUTH_NAV, auth::AUTH_BASE, auth::login::LOGIN, auth::register::REGISTER, diff --git a/templates/components/nav/auth.html b/templates/components/nav/auth.html new file mode 100644 index 0000000..d34737a --- /dev/null +++ b/templates/components/nav/auth.html @@ -0,0 +1,26 @@ + diff --git a/templates/components/nav/base.html b/templates/components/nav/base.html new file mode 100644 index 0000000..c3ad394 --- /dev/null +++ b/templates/components/nav/base.html @@ -0,0 +1,18 @@ + diff --git a/templates/components/pub-nav.html b/templates/components/nav/pub.html similarity index 100% rename from templates/components/pub-nav.html rename to templates/components/nav/pub.html