put header in separate file

This commit is contained in:
Khaïs COLIN 2025-10-24 17:49:03 +02:00
parent c5b21d1cef
commit 4aa1fa5731
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
6 changed files with 48 additions and 239 deletions

View file

@ -46,6 +46,7 @@ fn range(bound: i32) -> Vec<i32> {
struct FoodUpdateTemplate {
food: Food,
sum: Sum,
date: String,
}
#[derive(Debug, Clone, PartialEq)]
@ -211,11 +212,16 @@ fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<Sum> {
Ok(sum)
}
fn get_date() -> String {
let now = chrono::Local::now();
let date = format!("{}", now.format("%Y-%M-%d"));
date
}
async fn root(State(conn): State<ConnState>) -> Result<Html<String>, StatusCode> {
let foods = get_foods(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let now = chrono::Local::now();
let date = format!("{}", now.format("%Y-%M-%d"));
let date = get_date();
let index = IndexTemplate { foods, sum, date };
Ok(Html(
index
@ -247,7 +253,8 @@ async fn increase(
do_increase(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let update = FoodUpdateTemplate { food, sum };
let date = get_date();
let update = FoodUpdateTemplate { food, sum, date };
Ok(Html(
update
.render()
@ -270,7 +277,8 @@ async fn decrease(
do_decrease(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let update = FoodUpdateTemplate { food, sum };
let date = get_date();
let update = FoodUpdateTemplate { food, sum, date };
Ok(Html(
update
.render()

View file

@ -1,2 +1,2 @@
{% include "food.html" %}
{% include "sum.html" %}
{% include "header.html" %}

16
templates/header.html Normal file
View file

@ -0,0 +1,16 @@
<header>
<span>
Datum: {{ date }}
</span>
<div>
<span>
kc: {{ sum.kc }}
</span>
<span>
Protein: {{ sum.protein }}
</span>
<span>
Balaststoffe: {{ sum.bs }}
</span>
</div>
</header>

View file

@ -1,193 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.7/dist/htmx.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Food Tracker</title>
</head>
<body>
<header>
<span>
Datum: {{ date }}
</span>
<div>
<span>
kc: {{ sum.kc }}
</span>
<span>
Protein: {{ sum.protein }}
</span>
<span>
Balaststoffe: {{ sum.bs }}
</span>
</div>
</header>
<main>
<hr>
<div class="card">
<p class="food-name">
{{ food.name }}
</p>
<div class="card-inner">
<div class="amounts">
<p class="unit">
<sup>1</sup>&frasl;<sub>4</sub> Cup
</p>
<p class="kc">
{{ food.kc_per_serving }} kc
</p>
</div>
<div class="buttons">
<button class="increase">+</button>
<button class="decrease">-</button>
</div>
<div class="checkboxes">
<div class="ok">
<input type="checkbox" checked>
</div>
<div class="ok">
<input type="checkbox">
</div>
<div class="ok">
<input type="checkbox" checked>
</div>
<div class="bad">
<input type="checkbox" checked>
</div>
<div class="bad">
<input type="checkbox" checked>
</div>
<div class="bad">
<input type="checkbox">
</div>
<div class="bad">
<input type="checkbox">
</div>
<div class="bad">
<input type="checkbox">
</div>
<div class="bad">
<input type="checkbox">
</div>
<div class="bad">
<input type="checkbox">
</div>
</div>
</div>
</div>
<hr>
</main>
</body>
<style>
html {
font-family: sans-serif;
}
body {
margin: 0;
}
header {
display: inline-block;
text-align: center;
width: 100%;
background-color: hsl(from #a8c8a6 h 40% l);
padding: 2px;
#a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6
}
header div {
width: 100%;
}
header div span {
width: 30%;
display: inline-block;
text-align: left;
}
.card {
background-color: hsl(from #a8c8a6 h 30% l);
#a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6 #a8c8a6
}
hr {
margin: 0;
border: 1px solid #655057;
}
.card p {
margin: 0;
padding: 2px;
}
.food-name {
font-size: 1.2em;
}
.buttons {
display: flex;
flex-direction: column;
height: 100%;
}
.card-inner {
display: flex;
align-items: center;
gap: 5px;
}
.card-inner div button {
display: block;
}
button {
border: 0;
border-radius: 5px;
width: 2em;
height: 2em;
}
.increase {
background-color: #cb8175;
}
.decrease {
background-color: #6d8d8a;
}
.increase:hover {
background-color: hsl(from #cb8175 h 80% l);
}
.decrease:hover {
background-color: hsl(from #6d8d8a h 40% l);
}
input[type="checkbox"] {
width: 2em;
height: 2em;
}
.checkboxes {
display: flex;
}
.ok {
accent-color: hsl(from #a8c8a6 h 90% l);
background-color: hsl(from #a8c8a6 h 60% l);
}
.bad {
accent-color: hsl(from #cb8175 h 90% l);
background-color: hsl(from #cb8175 h 60% l);
}
</style>
</html>

View file

@ -1,44 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.7/dist/htmx.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Food Tracker</title>
</head>
<body>
<header>
<span>
Datum: {{ date }}
</span>
<div>
<span>
kc: {{ sum.kc }}
</span>
<span>
Protein: {{ sum.protein }}
</span>
<span>
Balaststoffe: {{ sum.bs }}
</span>
</div>
</header>
<main>
<hr>
{% for food in foods %}
<head>
<script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.7/dist/htmx.min.js"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Food Tracker</title>
</head>
<body>
{% include "header.html" %}
<main>
<hr>
{% for food in foods %}
{% include "food.html" %}
{% if !loop.last %}
<hr>
{% endif %}
<hr>
{% endif %}
{% endfor %}
</main>
</body>
<style>
html {
</main>
</body>
<style>
html {
font-family: sans-serif;
border: 2px solid hsl(from #a8c8a6 h 40% l);
}
@ -146,6 +128,5 @@
accent-color: hsl(from #cb8175 h 90% l);
background-color: hsl(from #cb8175 h 60% l);
}
</style>
</style>
</html>

View file

@ -1,3 +0,0 @@
<div style="background-color: #a8c8a6; margin: 1em;" id="sum" hx-swap-oob="true">
<b style="font-size: 24px; padding: 1em;">Total: {{ sum }} kc.</b>
</div>