templates: Make the index a class

The index is a full HTML page with some boilerplate that would be needed in all
pages deviating from the truth list, specifically the head and general structure.

Make the core structure a base template, rebuild the week index page inheriting
from it.
Change which template is used by the route accordingly.
This commit is contained in:
trotFunky 2024-07-27 23:16:19 +01:00
parent afdac98dea
commit b1f37a4e4a
5 changed files with 159 additions and 145 deletions

View file

@ -53,7 +53,7 @@ pub async fn week(week_number: u8, mut db: Connection<Db>, cookies: &CookieJar<'
} }
}; };
Template::render("index", context! { Template::render("weeks/index", context! {
week_data: week_data, week_data: week_data,
truths: truths, truths: truths,
user: user, user: user,
@ -70,7 +70,7 @@ pub async fn week(week_number: u8, mut db: Connection<Db>, cookies: &CookieJar<'
} }
}; };
Template::render("index", context! { Template::render("weeks/index", context! {
week_data: week_data, week_data: week_data,
truths: truths, truths: truths,
user: user, user: user,

View file

@ -5,7 +5,7 @@
margin-right: 2em; margin-right: 2em;
} }
.page_body { .truth_page_body {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: space-between; justify-content: space-between;

View file

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
{% set title = "Vérités Fabula Ultima" %}
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="/static/style.css" rel="stylesheet"/>
<link href="/static/favicon.ico" rel="icon"/>
{% block scripts %}
{% endblock %}
</head>
<body>
{% block top_bar %}
<div class="top_bar">
<h1>{{ title }}</h1>
{% if user.logged_in == true %}
<form class="login" id="logout" action="/{{ week_data.number }}/logout" method="POST">
Connecté en tant que <b>{{ user.name }}</b>
<button form="logout">Déconnecter</button>
</form>
{% else %}
<form class="login" id="login" action="/{{ week_data.number }}/login" method="POST">
<label>Pseudo <input form="login" type="text" name="name"/></label>
<label>Mot de passe <input form="login" type="password" name="password"/></label>
<button form="login">Se connecter</button>
</form>
{% endif %}
</div>
{% endblock %}
{% block body %}
{% endblock %}
</body>
</html>

View file

@ -1,142 +0,0 @@
<!DOCTYPE html>
<html lang="en">
{% set title = "Vérités Fabula Ultima" %}
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="/static/style.css" rel="stylesheet"/>
<link href="/static/favicon.ico" rel="icon"/>
{% if user.logged_in == true and not user.is_admin %}
<script defer="defer" type="text/javascript" src="/static/vote_handler.js"></script>
{% endif %}
<script defer="defer" type="application/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js"></script>
<script defer="defer" type="text/javascript" src="/static/vote_chart.js"></script>
</head>
{#{% import "week_change_arrows" as week_macro %}#}
{# For some reason the import does not work ? Figure it out at some point... #}
{%- macro display(display_character, to, enabled) -%}
{%- set class = "week_change" -%}
{%- if enabled == true %}
{% set target = ("href=/" ~ to) %}
{%- else -%}
{% set class = class ~ " week_change_hidden" -%}
{% set target = "" %}
{%- endif -%}
<a {{ target }} class="{{ class }}">{%- if enabled == true -%}{{- display_character -}}{%- endif -%}</a>
{%- endmacro display -%}
{% set back_arrow_enabled = week_data.number > 1 %}
{% set next_arrow_enabled = (week_data.is_last_week != true or user.is_admin == true) %}
{% set next_arrow_href = (week_data.number + 1) %}
{% if user.is_admin == true %}
{% set next_arrow_href = next_arrow_href ~ "/create" %}
{% set next_arrow_chara = '⥅' %}
{% else %}
{% set next_arrow_chara = '⟹' %}
{% endif %}
{# Remove the form if all votes are locked, to reduce confusion. #}
{% set lock_truth_form = user.votes | length + 1 == truths | length and week_data.is_last_week != true %}
<body>
<div class="top_bar">
<h1>{{ title }}</h1>
{% if user.logged_in == true %}
<form class="login" id="logout" action="/{{ week_data.number }}/logout" method="POST">
Connecté en tant que <b>{{ user.name }}</b>
<button form="logout">Déconnecter</button>
</form>
{% else %}
<form class="login" id="login" action="/{{ week_data.number }}/login" method="POST">
<label>Pseudo <input form="login" type="text" name="name"/></label>
<label>Mot de passe <input form="login" type="password" name="password"/></label>
<button form="login">Se connecter</button>
</form>
{% endif %}
</div>
<h2>{{- self::display(display_character='⟸', to=(week_data.number - 1), enabled=back_arrow_enabled) }}
Semaine {{ week_data.number }}
{{- self::display(display_character=next_arrow_chara, to=next_arrow_href, enabled=next_arrow_enabled) -}}</h2>
<div class="page_body">
<div class="truth_list">
{% if user.is_admin == true and week_data.is_last_week != true %}
<form action="/{{ week_data.number }}/set_last" method="post">
<button>
Définir comme dernière semaine active
</button>
</form>
{% endif %}
<div class="week_intro">
{{ week_data.rendered_text | safe }}
{%- if user.is_admin == true -%}
<hr/>
<form action="/{{ week_data.number }}/edit" method="post">
<textarea class="editor" name="raw_intro">
{{- week_data.raw_text -}}
</textarea>
<button>
Modifier l'introduction
</button>
</form>
{% endif %}
</div>
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
<form id="truths" action="/{{ week_data.number }}/vote" method="POST">
{% endif %}
{# Truths start at 1 but the array starts at 0 #}
{% set index_delta = 1 %}
{% for truth in truths %}
{#
The truths are in an ordered array, but one of them might be the user's.
In this case, we need to stop the array index from incrementing if the current
truth is the user's, as they cannot have voted for themselves, leading to one
less votes than there are truths.
#}
{%- if truth.author_id == user.id -%}
{%- set_global index_delta = 2 -%}
{% endif %}
{% set truth_index = truth.number - index_delta %}
{% if user.is_admin == true %}
{% include "weeks/editable_truth" %}
{% else %}
{% include "weeks/truth" %}
{% endif %}
{% endfor %}
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
<br/>
<button form="truths">
{%- if user.logged_in == true and user.has_week_vote == true -%}
Changer de vote
{% else %}
À voter !
{% endif %}
</button>
</form>
{% endif %}
{# If admin, show an additional box for creating a new Truth. #}
{% if user.is_admin == true %}
<div class="individual_truth">
<h3>Nouvelle vérité</h3>
<form action="/{{ week_data.number }}/new_truth" method="POST">
{% include "weeks/truth_editor" %}
</form>
</div>
{% endif %}
</div>
<div class="graph">
<div>
<canvas id="vote_chart"></canvas>
</div>
</div>
</div>
</body>
</html>

View file

@ -0,0 +1,120 @@
{% extends "base_page" %}
{% block scripts %}
{% if user.logged_in == true and not user.is_admin %}
<script defer="defer" type="text/javascript" src="/static/vote_handler.js"></script>
{% endif %}
<script defer="defer" type="application/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js"></script>
<script defer="defer" type="text/javascript" src="/static/vote_chart.js"></script>
{% endblock %}
{#{% import "week_change_arrows" as week_macro %}#}
{# For some reason the import does not work ? Figure it out at some point... #}
{%- macro display(display_character, to, enabled) -%}
{%- set class = "week_change" -%}
{%- if enabled == true %}
{% set target = ("href=/" ~ to) %}
{%- else -%}
{% set class = class ~ " week_change_hidden" -%}
{% set target = "" %}
{%- endif -%}
<a {{ target }} class="{{ class }}">{%- if enabled == true -%}{{- display_character -}}{%- endif -%}</a>
{%- endmacro display -%}
{# Remove the form if all votes are locked, to reduce confusion. #}
{% set lock_truth_form = user.votes | length + 1 == truths | length and week_data.is_last_week != true %}
{% block body %}
{# Apparently needs to be inside the block ? #}
{% set back_arrow_enabled = week_data.number > 1 %}
{% set next_arrow_enabled = (week_data.is_last_week != true or user.is_admin == true) %}
{% set next_arrow_href = (week_data.number + 1) %}
{% if user.is_admin == true %}
{% set next_arrow_href = next_arrow_href ~ "/create" %}
{% set next_arrow_chara = '⥅' %}
{% else %}
{% set next_arrow_chara = '⟹' %}
{% endif %}
<h2>{{- self::display(display_character='⟸', to=(week_data.number - 1), enabled=back_arrow_enabled) }}
Semaine {{ week_data.number }}
{{- self::display(display_character=next_arrow_chara, to=next_arrow_href, enabled=next_arrow_enabled) -}}</h2>
<div class="truth_page_body">
<div class="truth_list">
{% if user.is_admin == true and week_data.is_last_week != true %}
<form action="/{{ week_data.number }}/set_last" method="post">
<button>
Définir comme dernière semaine active
</button>
</form>
{% endif %}
<div class="week_intro">
{{ week_data.rendered_text | safe }}
{%- if user.is_admin == true -%}
<hr/>
<form action="/{{ week_data.number }}/edit" method="post">
<textarea class="editor" name="raw_intro">
{{- week_data.raw_text -}}
</textarea>
<button>
Modifier l'introduction
</button>
</form>
{% endif %}
</div>
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
<form id="truths" action="/{{ week_data.number }}/vote" method="POST">
{% endif %}
{# Truths start at 1 but the array starts at 0 #}
{% set index_delta = 1 %}
{% for truth in truths %}
{#
The truths are in an ordered array, but one of them might be the user's.
In this case, we need to stop the array index from incrementing if the current
truth is the user's, as they cannot have voted for themselves, leading to one
less votes than there are truths.
#}
{%- if truth.author_id == user.id -%}
{%- set_global index_delta = 2 -%}
{% endif %}
{% set truth_index = truth.number - index_delta %}
{% if user.is_admin == true %}
{% include "weeks/editable_truth" %}
{% else %}
{% include "weeks/truth" %}
{% endif %}
{% endfor %}
{% if user.logged_in == true and user.is_admin == false and not lock_truth_form %}
<br/>
<button form="truths">
{%- if user.logged_in == true and user.has_week_vote == true -%}
Changer de vote
{% else %}
À voter !
{% endif %}
</button>
</form>
{% endif %}
{# If admin, show an additional box for creating a new Truth. #}
{% if user.is_admin == true %}
<div class="individual_truth">
<h3>Nouvelle vérité</h3>
<form action="/{{ week_data.number }}/new_truth" method="POST">
{% include "weeks/truth_editor" %}
</form>
</div>
{% endif %}
</div>
<div class="graph">
<div>
<canvas id="vote_chart"></canvas>
</div>
</div>
</div>
{% endblock %}