Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Refactor template field names
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Apr 13, 2024
1 parent 2810cc9 commit ca3ad2f
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/routes/admin/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(super) async fn admin_dashboard(
.map_err(e500)?;

Ok(Dashboard {
title: "Admin Dashboard",
page_title: "Admin Dashboard",
welcome: "Welcome",
available_actions: "Available actions",
send_newsletter: "Send newsletter",
Expand Down Expand Up @@ -50,7 +50,7 @@ pub async fn get_username(db_pool: &PgPool, user_id: Uuid) -> Result<String, Err
#[derive(Template)]
#[template(path = "web/dashboard.html")]
pub struct Dashboard<'a> {
title: &'a str,
page_title: &'a str,
welcome: &'a str,
available_actions: &'a str,
send_newsletter: &'a str,
Expand Down
28 changes: 14 additions & 14 deletions src/routes/admin/newsletters/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ pub(in crate::routes::admin) async fn newsletter_form(
let flashes = messages.map(|m| m.message).collect();

NewsletterForm {
title: "Send Newsletter",
newsletter_title_label: "Newsletter title",
newsletter_title_placeholder: "Enter newsletter title",
newsletter_html_label: "Newsletter HTML content",
newsletter_html_placeholder: "Enter newsletter HTML content",
newsletter_text_label: "Newsletter text",
newsletter_text_placeholder: "Enter newsletter text",
page_title: "Send Newsletter",
title_label: "Newsletter title",
title_placeholder: "Enter newsletter title",
html_content_label: "Newsletter HTML content",
html_content_placeholder: "Enter newsletter HTML content",
text_content_label: "Newsletter text",
text_content_placeholder: "Enter newsletter text",
send_newsletter_button: "Send newsletter",
back_link: "Back",
flashes,
Expand All @@ -23,13 +23,13 @@ pub(in crate::routes::admin) async fn newsletter_form(
#[derive(Template)]
#[template(path = "web/newsletter_form.html")]
pub(in crate::routes::admin) struct NewsletterForm<'a> {
title: &'a str,
newsletter_title_label: &'a str,
newsletter_title_placeholder: &'a str,
newsletter_html_label: &'a str,
newsletter_html_placeholder: &'a str,
newsletter_text_label: &'a str,
newsletter_text_placeholder: &'a str,
page_title: &'a str,
title_label: &'a str,
title_placeholder: &'a str,
html_content_label: &'a str,
html_content_placeholder: &'a str,
text_content_label: &'a str,
text_content_placeholder: &'a str,
send_newsletter_button: &'a str,
back_link: &'a str,
flashes: Vec<String>,
Expand Down
12 changes: 6 additions & 6 deletions src/routes/admin/newsletters/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub(in crate::routes::admin) async fn publish_newsletter(
.email_client
.send_email(
&subscriber.email,
&form.newsletter_title,
&form.newsletter_html,
&form.newsletter_text,
&form.title,
&form.html_content,
&form.text_content,
)
.await
.with_context(|| {
Expand Down Expand Up @@ -73,9 +73,9 @@ async fn get_confirmed_subscribers(

#[derive(Deserialize)]
pub(in crate::routes::admin) struct FormData {
newsletter_title: String,
newsletter_html: String,
newsletter_text: String,
title: String,
html_content: String,
text_content: String,
}

struct ConfirmedSubscriber {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/admin/password/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(in crate::routes::admin) async fn change_password_form(
let flashes = messages.map(|m| m.message).collect();

ChangePasswordForm {
title: "Change Password",
page_title: "Change Password",
current_password_label: "Current Password",
current_password_placeholder: "Enter current password",
new_password_label: "New Password",
Expand All @@ -23,7 +23,7 @@ pub(in crate::routes::admin) async fn change_password_form(
#[derive(Template)]
#[template(path = "web/change_password_form.html")]
pub(in crate::routes::admin) struct ChangePasswordForm<'a> {
title: &'a str,
page_title: &'a str,
current_password_label: &'a str,
current_password_placeholder: &'a str,
new_password_label: &'a str,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub fn router() -> Router<AppState> {
#[tracing::instrument(name = "Render home page")]
async fn home() -> HomeTemplate<'static> {
HomeTemplate {
title: "zero2prod",
page_title: "zero2prod",
username: None,
}
}

#[derive(Template)]
#[template(path = "web/home.html")]
struct HomeTemplate<'a> {
title: &'a str,
page_title: &'a str,
username: Option<String>,
}
4 changes: 2 additions & 2 deletions src/routes/login/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(super) async fn login_form(messages: Messages) -> LoginForm<'static> {
let flashes = messages.map(|m| m.message).collect();

LoginForm {
title: "Login",
page_title: "Login",
username_label: "Username",
username_placeholder: "Enter username",
password_label: "Password",
Expand All @@ -20,7 +20,7 @@ pub(super) async fn login_form(messages: Messages) -> LoginForm<'static> {
#[derive(Template)]
#[template(path = "web/login_form.html")]
pub(super) struct LoginForm<'a> {
title: &'a str,
page_title: &'a str,
username_label: &'a str,
username_placeholder: &'a str,
password_label: &'a str,
Expand Down
4 changes: 2 additions & 2 deletions templates/web/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>{% block title %}{{ title }}{% endblock %}</title>
<title>{% block page_title %}{{ page_title }}{% endblock %}</title>
</head>

<body>
{% block content %}Something went wrong with rendering{% endblock %}
{% block page_content %}Something went wrong with rendering{% endblock %}
</body>

</html>
2 changes: 1 addition & 1 deletion templates/web/change_password_form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}

{% block content %}
{% block page_content %}
{%- for flash in flashes %}
<p><i>{{ flash }}</i></p>
{%- endfor %}
Expand Down
2 changes: 1 addition & 1 deletion templates/web/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}

{% block content %}
{% block page_content %}
<p>{{ welcome }}, {{ username }}!</p>
<p>{{ available_actions }}:</p>
<ol>
Expand Down
2 changes: 1 addition & 1 deletion templates/web/home.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}

{% block content %}
{% block page_content %}
{%- if let Some(username) = username %}
<p>Welcome to our newsletter, {{ username }}!</p>
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion templates/web/login_form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "base.html" %}

{% block content %}
{% block page_content %}
{%- for flash in flashes %}
<p><i>{{ flash }}</i></p>
{%- endfor %}
Expand Down
16 changes: 8 additions & 8 deletions templates/web/newsletter_form.html
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{% extends "base.html" %}

{% block content %}
{% block page_content %}
{%- for flash in flashes %}
<p><i>{{ flash }}</i></p>
{%- endfor %}

<form action="/admin/newsletters" method="post">
<label>
{{ newsletter_title_label }}<br>
<input type="text" placeholder="{{ newsletter_title_placeholder }}" name="newsletter_title" required>
{{ title_label }}<br>
<input type="text" placeholder="{{ title_placeholder }}" name="title" required>
</label>
<br>
<br>
<label>
{{ newsletter_html_label }}<br>
<textarea placeholder="{{ newsletter_html_placeholder }}" rows="20" cols="100" name="newsletter_html"
{{ html_content_label }}<br>
<textarea placeholder="{{ html_content_placeholder }}" rows="20" cols="100" name="html_content"
required></textarea>
</label>
<br>
<br>
<label>
{{ newsletter_text_label }}<br>
<textarea type="text" placeholder="{{ newsletter_text_placeholder }}" rows="20" cols="100"
name="newsletter_text" required></textarea>
{{ text_content_label }}<br>
<textarea type="text" placeholder="{{ text_content_placeholder }}" rows="20" cols="100"
name="text_content" required></textarea>
</label>
<br>
<br>
Expand Down
30 changes: 15 additions & 15 deletions tests/api/admin_newsletters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() {
// given
let app = TestApp::spawn().await;
let newsletter_request_body = json!({
"newsletter_title": "Newsletter Title",
"newsletter_html": "<p>Newsletter body as html.</p>",
"newsletter_text": "Newsletter body as text.",
"title": "Newsletter Title",
"html_content": "<p>Newsletter body as html.</p>",
"text_content": "Newsletter body as text.",
});

create_confirmed_subscriber(&app).await;
Expand Down Expand Up @@ -43,9 +43,9 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
// given
let app = TestApp::spawn().await;
let newsletter_request_body = json!({
"newsletter_title": "Newsletter Title",
"newsletter_html": "<p>Newsletter body as html.</p>",
"newsletter_text": "Newsletter body as text.",
"title": "Newsletter Title",
"html_content": "<p>Newsletter body as html.</p>",
"text_content": "Newsletter body as text.",
});

create_unfonfirmed_subscriber(&app).await;
Expand Down Expand Up @@ -77,22 +77,22 @@ async fn newsletters_returns_400_for_invalid_data() {
let test_cases = vec![
(
json!({
"newsletter_html": "<p>Newsletter body as html.</p>",
"newsletter_text": "Newsletter body as text.",
"html_content": "<p>Newsletter body as html.</p>",
"text_content": "Newsletter body as text.",
}),
"missing title",
),
(
json!({
"newsletter_title": "Newsletter Title",
"newsletter_text": "Newsletter body as text.",
"title": "Newsletter Title",
"text_content": "Newsletter body as text.",
}),
"missing html content",
),
(
json!({
"newsletter_title": "Newsletter Title",
"newsletter_html": "<p>Newsletter body as html.</p>",
"title": "Newsletter Title",
"html_content": "<p>Newsletter body as html.</p>",
}),
"missing text content",
),
Expand Down Expand Up @@ -125,9 +125,9 @@ async fn requests_from_anonymous_users_are_redirected_to_login() {
// given
let app = TestApp::spawn().await;
let newsletter_request_body = json!({
"newsletter_title": "Newsletter Title",
"newsletter_html": "<p>Newsletter body as html.</p>",
"newsletter_text": "Newsletter body as text.",
"title": "Newsletter Title",
"html_content": "<p>Newsletter body as html.</p>",
"text_content": "Newsletter body as text.",
});

// when
Expand Down

0 comments on commit ca3ad2f

Please sign in to comment.