aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/layout.rs
blob: c25e6445183e56b2bf06852ded1f5da906116550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use super::{account::session::Session, Defer, HtmlTemplate};
use crate::{uri, CONF};
use async_std::task::block_on;
use markup::Render;
use rocket::{
    http::ContentType,
    request::{FromRequest, Outcome},
    response::{self, Responder},
    Request, Response,
};
use std::{convert::Infallible, io::Cursor};
use tokio::runtime::Handle;

markup::define! {
    Layout<Main: Render>(title: String, main: Main, session: Option<Session>) {
        @markup::doctype()
        html {
            head {
                title { @title " - " @CONF.brand }
                link[rel="stylesheet", href="/assets/style.css"];
                script[src="/assets/bundle.js"] {}
            }
            body {
                nav {
                    h1 { a[href="/"] { @CONF.brand } }
                    a[href="/library"] { "My Library" }

                    div.account {
                        @if let Some(session) = session {

                        } else {
                            // a[href=uri!(r_account_register())] { "Register" }
                            // a[href=uri!(r_account_login())] { "Log in" }
                        }
                    }
                }
                #main { @main }
            }
        }
    }
}

pub type DynLayoutPage<'a> = LayoutPage<markup::DynRender<'a>>;

pub struct LayoutPage<T> {
    pub title: String,
    pub content: T,
}

impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> {
    fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
        let session = block_on(req.guard::<Option<Session>>()).unwrap();
        let mut out = String::new();
        Layout {
            main: self.content,
            title: self.title,
            session,
        }
        .render(&mut out)
        .unwrap();

        Response::build()
            .header(ContentType::HTML)
            .streamed_body(Cursor::new(out))
            .ok()
    }
}