use leptos::logging; use leptos_meta::{Meta, Title}; use leptos::{ev::SubmitEvent, prelude::*, server_fn::codec::JsonEncoding}; use markdown_view_leptos::markdown_view; use serde::{Deserialize, Serialize}; use thiserror::Error; use web_sys::HtmlFormElement; #[server] async fn submit_mail(mail: String, subject: String, message: String) -> Result { use crate::AppState; const NTFY_TOPIC_CREDENTIAL_NAME: &str = "ntfy-topic"; let mail = format!("From: {}\nSubject: {}\n{}", mail, subject, message); logging::log!("{}", mail); let state = use_context::().unwrap(); let ntfy_topic = std::fs::read_to_string( state .config .credentials_directory .join(NTFY_TOPIC_CREDENTIAL_NAME), )?; let ntfy_url = state.config.ntfy_instance.join(ntfy_topic.trim())?; state .http_client .post(ntfy_url) .body(mail) .send() .await .map_err(|err| err.without_url())? .error_for_status() .map_err(|err| err.without_url())?; leptos_axum::redirect("/mail"); Ok("Mail successfully sent!".to_owned()) } #[component] fn MailForm() -> impl IntoView { use web_sys::wasm_bindgen::JsCast as _; let submit_mail = ServerAction::::new(); let flash_message = submit_mail.value(); view! {
()); if let Some(target) = form { target.unwrap().reset(); } else { logging::error!("Failed to reset form"); } } >