Minimal htmx form

main
Araozu 2024-02-09 20:37:11 -05:00
parent 625fa9325b
commit 2e25c50532
4 changed files with 41 additions and 13 deletions

View File

@ -1,5 +1,5 @@
use maud::{Markup, html}; use maud::{Markup, html};
use rocket::form::Form; use rocket::{form::Form, http::Status};
#[derive(FromForm, Debug)] #[derive(FromForm, Debug)]
pub struct UserCreate { pub struct UserCreate {
@ -10,10 +10,23 @@ pub struct UserCreate {
} }
#[post("/user", data = "<user>")] #[post("/user", data = "<user>")]
pub async fn create_user(user: Form<UserCreate>) -> Markup { pub async fn create_user(user: Form<UserCreate>) -> (Status, Markup) {
println!("Got a user: {:?}", user); // Email domain must be eegsac.com
let email_domain = user.user_email.split('@').collect::<Vec<&str>>()[1];
html! { if email_domain != "eegsac.com" {
":D" return (Status::BadRequest, html! {
div id="user_create_response" class="bg-red-500 text-white p-2 rounded" {
"El dominio del correo electrónico debe ser eegsac.com"
}
});
} }
(Status::Ok, html! {
div id="user_create_response" class="bg-green-700 text-white p-2 rounded" {
"Registrado con éxito"
}
})
} }

View File

@ -8,24 +8,34 @@ pub fn index() -> Markup {
head { head {
title { "EEGSAC" } title { "EEGSAC" }
link rel="stylesheet" type="text/css" href="/static/css/output.css"; link rel="stylesheet" type="text/css" href="/static/css/output.css";
link rel="icon" type="image/png" href="/static/img/favicon.png";
script defer src="https://unpkg.com/htmx.org@1.9.10" crossorigin="anonymous" {} script defer src="https://unpkg.com/htmx.org@1.9.10" crossorigin="anonymous" {}
script defer src="https://unpkg.com/htmx.org/dist/ext/response-targets.js" {}
} }
body { body hx-ext="response-targets" {
h1 { "Registrar nuevo usuario" } h1 { "Registrar nuevo usuario" }
form form
action="/f/user" hx-post="/f/user"
method="post" hx-target="#user_create_response"
hx-target-400="#user_create_response"
hx-swap="outerHTML"
{ {
input type="text" name="user_names" placeholder="Nombres"; input class="bg-c-bg text-c-on-bg border border-c-on-bg"
required type="text" name="user_names" placeholder="Nombres";
br; br;
input type="text" name="user_surnames" placeholder="Apellidos"; input class="bg-c-bg text-c-on-bg border border-c-on-bg"
required type="text" name="user_surnames" placeholder="Apellidos";
br; br;
input type="email" name="user_email" placeholder="Correo electrónico"; input class="bg-c-bg text-c-on-bg border border-c-on-bg"
required type="email" name="user_email" placeholder="Correo electrónico";
br; br;
input type="password" name="user_password" placeholder="Contraseña"; input class="bg-c-bg text-c-on-bg border border-c-on-bg"
required type="password" name="user_password" placeholder="Contraseña";
br; br;
input type="submit" value="Registrar"; input type="submit" value="Registrar";
} }
br;
div id="user_create_response" {}
} }
} }
} }

BIN
static/img/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -4,7 +4,12 @@ module.exports = {
"./src/**/*.{html,rs}", "./src/**/*.{html,rs}",
], ],
theme: { theme: {
extend: {}, extend: {
colors: {
"c-bg": "var(--c-bg)",
"c-on-bg": "var(--c-on-bg)",
}
},
}, },
plugins: [], plugins: [],
} }