achubb_website/src/main.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

use actix_htmx::{Htmx, HtmxMiddleware, TriggerType};
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(HtmxMiddleware)
.service(web::resource("/contact/1/edit").to(index))
})
.bind("0.0.0.0:8080")?
.run()
.await
}
async fn index(htmx: Htmx) -> impl Responder {
let mut body: &str = "did not work";
if htmx.is_htmx {
body = r#"<form hx-put="/contact/1" hx-target="this" hx-swap="outerHTML">
<div>
<label>First Name</label>
<input type="text" name="firstName" value="Joe">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" value="Blow">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="joe@blow.com">
</div>
<button class="btn">Submit</button>
<button class="btn" hx-get="/contact/1">Cancel</button>
</form>"#
}
htmx.trigger_event(
"my_event".to_string(),
Some(r#"{"level": "info", "message": "my event message!"}"#.to_string()),
Some(TriggerType::Standard)
);
HttpResponse::Ok().content_type("text/html").body(body)
2023-12-14 20:50:31 -05:00
}