1
0
Fork 0

Use views as template contexts and improve templates

This commit is contained in:
Alex Kotov 2020-10-17 11:43:37 +05:00
parent 64f8b7f172
commit 5d38099631
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
11 changed files with 113 additions and 66 deletions

View file

@ -8,6 +8,7 @@ mod database;
mod states;
mod routes;
mod responses;
mod views;
mod schema;
mod models;
mod forms;

View file

@ -1,3 +1,5 @@
use crate::views;
use rocket::response::Redirect;
use rocket_contrib::templates::Template;
@ -15,12 +17,6 @@ pub enum CommonResponse {
UnknownError(Template),
}
#[derive(Serialize)]
struct ErrorTemplateContext {
layout: &'static str,
error_code: u16,
}
impl From<rocket_csrf::VerificationFailure> for CommonResponse {
fn from(_: rocket_csrf::VerificationFailure) -> Self {
Self::InvalidAuthenticityToken(Redirect::to("/"))
@ -29,11 +25,17 @@ impl From<rocket_csrf::VerificationFailure> for CommonResponse {
impl From<diesel::result::Error> for CommonResponse {
fn from(_: diesel::result::Error) -> Self {
let template_context = ErrorTemplateContext {
layout: "site",
let page_context = views::Error {
error_code: 500,
};
Self::UnknownError(Template::render("error", &template_context))
let context = views::Site {
page: "error".to_string(),
page_context,
authenticity_token: "".to_string(), // TODO
current_user: None, // TODO
};
Self::UnknownError(Template::render("site", &context))
}
}

View file

@ -1,5 +1,6 @@
use crate::database;
use crate::states;
use crate::views;
use crate::models;
use crate::responses::CommonResponse;
@ -15,18 +16,16 @@ pub fn index(
) -> Result<Template, CommonResponse> {
let all_users = models::User::all(db_conn)?;
Ok(Template::render("home/index", &IndexTemplateContext {
authenticity_token: csrf_token.0,
layout: "site",
current_user: current_user.0,
let page_context = views::home::Index {
users: all_users,
}))
}
};
#[derive(Serialize)]
struct IndexTemplateContext {
authenticity_token: String,
layout: &'static str,
current_user: Option<models::User>,
users: Vec<models::User>,
let context = views::Site {
page: "home/index".to_string(),
page_context,
authenticity_token: csrf_token.0,
current_user: current_user.0,
};
Ok(Template::render("site", &context))
}

View file

@ -1,5 +1,6 @@
use crate::database;
use crate::states;
use crate::views;
use crate::models;
use crate::forms;
@ -22,10 +23,18 @@ pub fn new(
));
}
Ok(Template::render("sessions/new", &BasicTemplateContext {
authenticity_token: csrf_token.0,
layout: "site",
}))
let page_context = views::sessions::New {
authenticity_token: csrf_token.0.to_string(),
};
let context = views::Site {
page: "sessions/new".to_string(),
page_context,
authenticity_token: csrf_token.0.to_string(),
current_user: None,
};
Ok(Template::render("site", &context))
}
#[post("/sign_in", data = "<form>")]
@ -76,17 +85,19 @@ pub fn delete(
Ok(Redirect::to(uri!(super::home::index)))
}
#[derive(Serialize)]
struct BasicTemplateContext {
authenticity_token: String,
layout: &'static str,
}
fn invalid_sign_in_credentials(authenticity_token: &String) -> CommonResponse {
CommonResponse::InvalidCredentials(
Template::render("sessions/new", &BasicTemplateContext {
let page_context = views::sessions::New {
authenticity_token: authenticity_token.to_string(),
layout: "site",
})
};
let context = views::Site {
page: "sessions/new".to_string(),
page_context,
authenticity_token: authenticity_token.to_string(),
current_user: None,
};
CommonResponse::InvalidCredentials(
Template::render("site", &context)
)
}

View file

@ -1,5 +1,6 @@
use crate::database;
use crate::states;
use crate::views;
use crate::models;
use crate::forms;
@ -22,10 +23,18 @@ pub fn new(
));
}
Ok(Template::render("users/new", &BasicTemplateContext {
authenticity_token: csrf_token.0,
layout: "site",
}))
let page_context = views::users::New {
authenticity_token: csrf_token.0.to_string(),
};
let context = views::Site {
page: "users/new".to_string(),
page_context,
authenticity_token: csrf_token.0.to_string(),
current_user: None,
};
Ok(Template::render("site", &context))
}
#[post("/sign_up", data = "<form>")]
@ -53,12 +62,6 @@ pub fn create(
Ok(Redirect::to(uri!(super::home::index)))
}
#[derive(Serialize)]
struct BasicTemplateContext {
authenticity_token: String,
layout: &'static str,
}
struct XXXXX {
form: forms::UserSignUp,
authenticity_token: String,
@ -81,9 +84,17 @@ impl XXXXX {
impl From<YYYYY> for CommonResponse {
fn from(yyyyy: YYYYY) -> Self {
Self::InvalidForm(Template::render("users/new", &BasicTemplateContext {
authenticity_token: yyyyy.authenticity_token,
layout: "site",
}))
let page_context = views::users::New {
authenticity_token: yyyyy.authenticity_token.to_string(),
};
let context = views::Site {
page: "users/new".to_string(),
page_context,
authenticity_token: yyyyy.authenticity_token.to_string(),
current_user: None,
};
Self::InvalidForm(Template::render("site", &context))
}
}

39
src/views.rs Normal file
View file

@ -0,0 +1,39 @@
use crate::models;
use serde::ser::Serialize;
#[derive(Serialize)]
pub struct Site<T: Serialize> {
pub page: String,
pub page_context: T,
pub authenticity_token: String,
pub current_user: Option<models::User>,
}
#[derive(Serialize)]
pub struct Error {
pub error_code: u16,
}
pub mod home {
use crate::models;
#[derive(Serialize)]
pub struct Index {
pub users: Vec<models::User>,
}
}
pub mod sessions {
#[derive(Serialize)]
pub struct New {
pub authenticity_token: String,
}
}
pub mod users {
#[derive(Serialize)]
pub struct New {
pub authenticity_token: String,
}
}

View file

@ -1,5 +1 @@
{{#*inline "page"}}
<h1>{{ error_code }}</h1>
{{/inline}}
{{> (layout)}}

View file

@ -1,4 +1,3 @@
{{#*inline "page"}}
<div class="container mt-4">
<h1>Users</h1>
@ -8,6 +7,3 @@
{{/each}}
</ul>
</div>
{{/inline}}
{{> (layout)}}

View file

@ -1,4 +1,3 @@
{{#*inline "page"}}
<div class="container mt-4">
<h1>Sign In</h1>
@ -20,6 +19,3 @@
</button>
</form>
</div>
{{/inline}}
{{> (layout)}}

View file

@ -17,7 +17,7 @@
<body>
{{> navbar}}
{{> page}}
{{> (page) page_context}}
<script type="text/javascript" src="/assets/bundle.js"></script>
</body>

View file

@ -1,4 +1,3 @@
{{#*inline "page"}}
<div class="container mt-4">
<h1>Sign Up</h1>
@ -25,6 +24,3 @@
</button>
</form>
</div>
{{/inline}}
{{> (layout)}}