Add a toggle between dark and light theme

This commit is contained in:
Greg Burri 2025-03-31 15:31:06 +02:00
parent d22617538e
commit 559ed139aa
34 changed files with 640 additions and 469 deletions

View file

@ -7,44 +7,48 @@ use axum::{
// use tracing::{event, Level};
use crate::{
Result,
Context, Result,
data::{db, model},
html_templates::*,
translation::{self, Sentence},
translation::Sentence,
};
#[debug_handler]
pub async fn create(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
Extension(tr): Extension<translation::Tr>,
Extension(context): Extension<Context>,
) -> Result<Response> {
if let Some(user) = user {
if let Some(user) = context.user {
let recipe_id = connection.create_recipe(user.id).await?;
Ok(Redirect::to(&format!(
"/{}/recipe/edit/{}",
tr.current_lang_code(),
context.tr.current_lang_code(),
recipe_id
))
.into_response())
} else {
Ok(Html(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).render()?).into_response())
Ok(
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
.into_response(),
)
}
}
#[debug_handler]
pub async fn edit(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
Extension(tr): Extension<translation::Tr>,
Extension(context): Extension<Context>,
Path(recipe_id): Path<i64>,
) -> Result<Response> {
if let Some(user) = user {
if let Some(ref user) = context.user {
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
if model::can_user_edit_recipe(&user, &recipe) {
let recipes = Recipes {
published: connection
.get_all_published_recipe_titles(tr.current_lang_code(), Some(user.id))
.get_all_published_recipe_titles(
context.tr.current_lang_code(),
Some(user.id),
)
.await?,
unpublished: connection
.get_all_unpublished_recipe_titles(user.id)
@ -54,8 +58,7 @@ pub async fn edit(
Ok(Html(
RecipeEditTemplate {
user: Some(user),
tr,
context,
recipes,
recipe,
}
@ -63,42 +66,48 @@ pub async fn edit(
)
.into_response())
} else {
Ok(
Html(
MessageTemplate::new(tr.t(Sentence::RecipeNotAllowedToEdit), tr)
.render()?,
Ok(Html(
MessageTemplate::new(
context.tr.t(Sentence::RecipeNotAllowedToEdit),
context.tr,
)
.into_response(),
.render()?,
)
.into_response())
}
} else {
Ok(
Html(MessageTemplate::new(tr.t(Sentence::RecipeNotFound), tr).render()?)
.into_response(),
Ok(Html(
MessageTemplate::new(context.tr.t(Sentence::RecipeNotFound), context.tr)
.render()?,
)
.into_response())
}
} else {
Ok(Html(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).render()?).into_response())
Ok(
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
.into_response(),
)
}
}
#[debug_handler]
pub async fn view(
State(connection): State<db::Connection>,
Extension(user): Extension<Option<model::User>>,
Extension(tr): Extension<translation::Tr>,
Extension(context): Extension<Context>,
Path(recipe_id): Path<i64>,
) -> Result<Response> {
match connection.get_recipe(recipe_id, true).await? {
Some(recipe) => {
if !recipe.is_published
&& (user.is_none() || recipe.user_id != user.as_ref().unwrap().id)
&& (context.user.is_none() || recipe.user_id != context.user.as_ref().unwrap().id)
{
return Ok(Html(
MessageTemplate::new_with_user(
&tr.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
tr,
user,
&context
.tr
.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
context.tr,
context.user,
)
.render()?,
)
@ -108,11 +117,11 @@ pub async fn view(
let recipes = Recipes {
published: connection
.get_all_published_recipe_titles(
tr.current_lang_code(),
user.as_ref().map(|u| u.id),
context.tr.current_lang_code(),
context.user.as_ref().map(|u| u.id),
)
.await?,
unpublished: if let Some(user) = user.as_ref() {
unpublished: if let Some(user) = context.user.as_ref() {
connection
.get_all_unpublished_recipe_titles(user.id)
.await?
@ -124,8 +133,7 @@ pub async fn view(
Ok(Html(
RecipeViewTemplate {
user,
tr,
context,
recipes,
recipe,
}
@ -134,7 +142,12 @@ pub async fn view(
.into_response())
}
None => Ok(Html(
MessageTemplate::new_with_user(tr.t(Sentence::RecipeNotFound), tr, user).render()?,
MessageTemplate::new_with_user(
context.tr.t(Sentence::RecipeNotFound),
context.tr,
context.user,
)
.render()?,
)
.into_response()),
}