use leptos::{html::Form, logging}; use leptos_meta::{Meta, Title}; use leptos::{prelude::*, server_fn::codec::JsonEncoding}; use markdown_view_leptos::markdown_view; use serde::{Deserialize, Serialize}; use thiserror::Error; #[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 { let form: NodeRef
= NodeRef::new(); let submit_mail = ServerAction::::new(); let pending = submit_mail.pending(); let flash_message = submit_mail.value(); Effect::new(move || if flash_message.get().is_some_and(|m| m.is_ok()) { if let Some(form) = form.get() { form.reset(); } else { logging::warn!("Failed to reset form"); } } ); view! {