From f8c0f42370d1cd6ad19e61ba8f273a42cb0bde18 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Thu, 27 Oct 2022 18:44:40 +0200 Subject: [PATCH] Redirection back without clearing form --- src/main.rs | 5 ++++- src/routes.rs | 46 ++++++++++++++++++++++++++++++++-------- src/states.rs | 12 ++++++----- templates/form.html.tera | 29 +++++++++++++++++-------- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index d52e004..955b8bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ use rocket_dyn_templates::Template; #[rocket::launch] fn rocket() -> _ { rocket::build() - .mount("/", rocket::routes![routes::index, routes::submit]) + .mount( + "/contact-form", + rocket::routes![routes::index, routes::submit, routes::success], + ) .manage(states::SharedCaptchaSolutions::new()) .attach(Template::fairing()) } diff --git a/src/routes.rs b/src/routes.rs index 47253c9..90d4dcc 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,19 +1,29 @@ use rocket::form::{Form, FromForm, Strict}; use rocket::response::status::BadRequest; -use rocket::{get, post, State}; +use rocket::response::Redirect; +use rocket::{get, post, uri, State}; use rocket_dyn_templates::Template; use serde::Serialize; +use std::mem; use crate::states; #[derive(Serialize)] struct FormContext { id: u16, + name: Option, + email: Option, + telefon: Option, + message: Option, captcha: String, } -#[get("/")] +#[get("/?&&&")] pub fn index( + name: Option, + email: Option, + telefon: Option, + message: Option, captcha_solutions: &State, ) -> Result> { let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy); @@ -26,6 +36,10 @@ pub fn index( let form_context = FormContext { id, + name, + email, + telefon, + message, captcha: captcha_base64, }; @@ -44,13 +58,27 @@ pub struct ContactForm { #[post("/submit", data = "
")] pub fn submit( - form: Form>, + mut form: Form>, captcha_solutions: &State, -) -> Result> { - println!( - "{}", - captcha_solutions.check_answer(form.id, &form.captcha_answer) - ); +) -> Redirect { + let path_prefix = uri!("/contact-form"); - Ok("Ihre Anfrage wurde übermittelt. Wir melden uns bald bei Ihnen zurück.".to_string()) + if !captcha_solutions.check_answer(form.id, &form.captcha_answer) { + return Redirect::to(uri!( + path_prefix, + index( + Some(mem::take(&mut form.name)), + Some(mem::take(&mut form.email)), + Some(mem::take(&mut form.telefon)), + Some(mem::take(&mut form.message)), + ) + )); + } + + Redirect::to(uri!(path_prefix, success())) +} + +#[get("/success")] +pub fn success() -> &'static str { + "Ihre Anfrage wurde übermittelt! Wir melden uns bald bei Ihnen zurück." } diff --git a/src/states.rs b/src/states.rs index d2f66e7..e7ed3fe 100644 --- a/src/states.rs +++ b/src/states.rs @@ -35,13 +35,15 @@ impl SharedCaptchaSolutions { } pub fn check_answer(&self, id: u16, answer: &str) -> bool { - let mut captcha_solutions = self.arc.lock().unwrap(); + { + let mut captcha_solutions = self.arc.lock().unwrap(); - let id = id as usize; + let id = id as usize; - if captcha_solutions.solutions[id] == Some(answer.trim().to_string()) { - captcha_solutions.solutions[id] = None; - return true; + if captcha_solutions.solutions[id] == Some(answer.trim().to_string()) { + captcha_solutions.solutions[id] = None; + return true; + } } false diff --git a/templates/form.html.tera b/templates/form.html.tera index 224853b..cbdadd9 100644 --- a/templates/form.html.tera +++ b/templates/form.html.tera @@ -4,37 +4,48 @@ Contact form + +

+ Alternativ können Sie im unteren Formular Ihre E-Mail-Adresse (und optional Telefon-Nummer) mit einer Nachricht mit Ihrem Anliegen hinterlassen. Wir versuchen dann, Sie so früh wie möglich zu kontaktieren. +

+

Kontakt-Formular

- +
- +
- +
- +
-
+
- + +
+ +
+
- +
- +
+ +
- +