hero page, ingress form

This commit is contained in:
Per Stark
2024-12-30 23:57:13 +01:00
parent d5cf81edbe
commit 796bbc0225
17 changed files with 199 additions and 73 deletions

View File

@@ -26,6 +26,7 @@ use zettle_db::{
html::{
account::{delete_account, set_api_key, show_account_page},
index::index_handler,
ingress::{process_ingress_form, show_ingress_form},
search_result::search_result_handler,
signin::{authenticate_user, show_signin_form},
signout::sign_out_user,
@@ -152,6 +153,10 @@ fn html_routes(
.route("/search", get(search_result_handler))
.route("/signout", get(sign_out_user))
.route("/signin", get(show_signin_form).post(authenticate_user))
.route(
"/ingress",
get(show_ingress_form).post(process_ingress_form),
)
.route("/account", get(show_account_page))
.route("/set-api-key", post(set_api_key))
.route("/delete-account", delete(delete_account))

View File

@@ -11,7 +11,7 @@ use crate::{
storage::types::user::User,
};
page_data!(IndexData, "index.html", {
page_data!(IndexData, "index/index.html", {
queue_length: u32,
user: Option<User>
});

View File

@@ -0,0 +1,71 @@
use axum::{
extract::State,
http::{StatusCode, Uri},
response::{Html, IntoResponse, Redirect},
Form,
};
use axum_htmx::{HxBoosted, HxRedirect};
use axum_session_auth::AuthSession;
use axum_session_surreal::SessionSurrealPool;
use axum_typed_multipart::{FieldData, TryFromMultipart, TypedMultipart};
use serde::{Deserialize, Serialize};
use surrealdb::{engine::any::Any, Surreal};
use tempfile::NamedTempFile;
use tracing::info;
use crate::{
error::ApiError,
server::AppState,
storage::types::{file_info::FileInfo, user::User},
};
use super::{render_block, render_template};
#[derive(Serialize)]
struct PageData {
// name: String,
}
pub async fn show_ingress_form(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
) -> Result<impl IntoResponse, ApiError> {
if !auth.is_authenticated() {
return Ok(Redirect::to("/").into_response());
}
Ok(render_template("ingress_form.html", PageData {}, state.templates)?.into_response())
}
#[derive(Debug, TryFromMultipart)]
pub struct IngressParams {
pub content: Option<String>,
pub instructions: String,
pub category: String,
#[form_data(limit = "10000000")] // Adjust limit as needed
pub files: Vec<FieldData<NamedTempFile>>,
}
pub async fn process_ingress_form(
State(state): State<AppState>,
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
TypedMultipart(input): TypedMultipart<IngressParams>,
) -> Result<impl IntoResponse, ApiError> {
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
};
info!("{:?}", input);
// Process files and create FileInfo objects
let mut file_infos = Vec::new();
for file in input.files {
let file_info = FileInfo::new(file, &state.surreal_db_client).await?;
file_infos.push(file_info);
}
// Process the ingress (implement your logic here)
Ok(Html("SuccessBRO!").into_response())
// Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
}

View File

@@ -5,6 +5,7 @@ use minijinja_autoreload::AutoReloader;
pub mod account;
pub mod index;
pub mod ingress;
pub mod search_result;
pub mod signin;
pub mod signout;

View File

@@ -34,7 +34,7 @@ pub async fn search_result_handler(
)
.await?;
Ok(Html("Hello".to_string()))
Ok(Html(answer.content))
// let output = state
// .tera
// .render(