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 states;
mod routes; mod routes;
mod responses; mod responses;
mod views;
mod schema; mod schema;
mod models; mod models;
mod forms; mod forms;

View File

@ -1,3 +1,5 @@
use crate::views;
use rocket::response::Redirect; use rocket::response::Redirect;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
@ -15,12 +17,6 @@ pub enum CommonResponse {
UnknownError(Template), UnknownError(Template),
} }
#[derive(Serialize)]
struct ErrorTemplateContext {
layout: &'static str,
error_code: u16,
}
impl From<rocket_csrf::VerificationFailure> for CommonResponse { impl From<rocket_csrf::VerificationFailure> for CommonResponse {
fn from(_: rocket_csrf::VerificationFailure) -> Self { fn from(_: rocket_csrf::VerificationFailure) -> Self {
Self::InvalidAuthenticityToken(Redirect::to("/")) Self::InvalidAuthenticityToken(Redirect::to("/"))
@ -29,11 +25,17 @@ impl From<rocket_csrf::VerificationFailure> for CommonResponse {
impl From<diesel::result::Error> for CommonResponse { impl From<diesel::result::Error> for CommonResponse {
fn from(_: diesel::result::Error) -> Self { fn from(_: diesel::result::Error) -> Self {
let template_context = ErrorTemplateContext { let page_context = views::Error {
layout: "site",
error_code: 500, 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::database;
use crate::states; use crate::states;
use crate::views;
use crate::models; use crate::models;
use crate::responses::CommonResponse; use crate::responses::CommonResponse;
@ -15,18 +16,16 @@ pub fn index(
) -> Result<Template, CommonResponse> { ) -> Result<Template, CommonResponse> {
let all_users = models::User::all(db_conn)?; let all_users = models::User::all(db_conn)?;
Ok(Template::render("home/index", &IndexTemplateContext { let page_context = views::home::Index {
authenticity_token: csrf_token.0,
layout: "site",
current_user: current_user.0,
users: all_users, users: all_users,
})) };
}
#[derive(Serialize)] let context = views::Site {
struct IndexTemplateContext { page: "home/index".to_string(),
authenticity_token: String, page_context,
layout: &'static str, authenticity_token: csrf_token.0,
current_user: Option<models::User>, current_user: current_user.0,
users: Vec<models::User>, };
Ok(Template::render("site", &context))
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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