View form paylod

This commit is contained in:
Chris Jean-Marie 2024-11-13 20:25:06 +00:00
parent 0326c0c27a
commit 4b7d6ec52a
3 changed files with 64 additions and 20 deletions

View File

@ -1,11 +1,16 @@
use std::collections::HashMap;
use askama::Template; use askama::Template;
use askama_axum::{IntoResponse, Response}; use askama_axum::{IntoResponse, Response};
use axum::{ use axum::{
extract::{Path, State}, response::Redirect, routing::post, Extension, Form body::{self, Body},
extract::{FromRequest, Path, Request, State},
response::Redirect,
Extension, Form, Json, RequestExt,
}; };
use axum_extra::response::Html; use axum_extra::response::Html;
use chrono::Utc; use chrono::Utc;
use http::StatusCode; use http::{header::CONTENT_TYPE, StatusCode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool}; use sqlx::{FromRow, SqlitePool};
@ -213,16 +218,44 @@ pub struct ExchangeForm {
} }
pub async fn giftexchange_save( pub async fn giftexchange_save(
Path(exchange_id): Path<i64>,
Extension(user_data): Extension<Option<UserData>>,
State(db_pool): State<SqlitePool>, State(db_pool): State<SqlitePool>,
Form(item_form): Form<ExchangeForm>, request: Request<Body>,
) -> impl IntoResponse { ) -> impl IntoResponse {
println!("Saving gift exchange: {:?}", item_form); let content_type_header = request.headers().get(CONTENT_TYPE);
if is_authorized("/giftexchange", user_data.clone(), db_pool.clone()).await { let content_type = content_type_header.and_then(|value| value.to_str().ok());
// Insert new item to database
let now = Utc::now().timestamp();
} /* if let Some(content_type) = content_type {
Redirect::to("/").into_response() if content_type.starts_with("application/json") {
let payload = request
.extract()
.await
.map_err(IntoResponse::into_response);
}
if content_type.starts_with("application/x-www-form-urlencoded") {
let payload = request
.extract()
.await
.map_err(IntoResponse::into_response);
}
} */
let (req_parts, map_request_body) = request.into_parts();
let bytes = match body::to_bytes(map_request_body,usize::MAX).await {
Ok(bytes) => bytes,
Err(err) => {
return Err((
StatusCode::BAD_REQUEST,
format!("failed to read request body: {}", err),
));
}
};
println!("Saving gift exchange: {:?}", req_parts);
println!("Saving gift exchange: {:?} ", bytes);
Ok(Redirect::to("/").into_response())
}
#[derive(Debug, Serialize, Deserialize)]
struct Payload {
foo: String,
} }

View File

@ -60,6 +60,7 @@
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.23.5/dist/bootstrap-table.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.23.5/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
{% block script %}{% endblock script %} {% block script %}{% endblock script %}
</body> </body>

View File

@ -5,7 +5,7 @@
<div class="row"> <div class="row">
<div class="btn-toolbar" role="toolbar"> <div class="btn-toolbar" role="toolbar">
<a role="button" class="btn btn-primary" href="/giftexchange/{{ giftexchange.id }}">Save</a> <a role="button" class="btn btn-primary" href="/giftexchange/{{ giftexchange.id }}">Save</a>
<button type="submit" class="btn btn-danger">Update</button> <button id="update" type="submit" class="btn btn-danger">Update</button>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
@ -20,12 +20,12 @@
</div> </div>
<div class="row align-items-center text-center"> <div class="row align-items-center text-center">
<div class="col"> <div class="col">
<table id="non_participants" data-toggle="table" data-click-to-select="true" class="table table-bordered" <table id="non_participants" data-toggle="table" data-click-to-select="true"
height="400"> data-height="400">
<thead> <thead>
<tr> <tr>
<th data-field="state" data-checkbox="true"></th> <th data-field="state" data-checkbox="true"></th>
<th data-sortable="true" data-field="name" scope="col">Name</th> <th data-sortable="true" data-field="name" scope="col">Available</th>
<th data-hidden="true" data-field="id" scope="col">ID</th> <th data-hidden="true" data-field="id" scope="col">ID</th>
</tr> </tr>
</thead> </thead>
@ -47,12 +47,11 @@
</div> </div>
</div> </div>
<div class="col"> <div class="col">
<table id="participants" data-toggle="table" data-click-to-select="true" class="table table-bordered" <table id="participants" data-toggle="table" data-click-to-select="true" data-height="400">
height="400">
<thead> <thead>
<tr> <tr>
<th data-field="state" data-checkbox="true"></th> <th data-field="state" data-checkbox="true"></th>
<th data-sortable="true" data-field="name" scope="col">Name</th> <th data-sortable="true" data-field="name" scope="col">Participating</th>
<th data-hidden="true" data-field="id" scope="col">ID</th> <th data-hidden="true" data-field="id" scope="col">ID</th>
</tr> </tr>
</thead> </thead>
@ -109,7 +108,6 @@
}); });
$("#add").click(function () { $("#add").click(function () {
$("#output").empty();
$.each(npRows, function (index, value) { $.each(npRows, function (index, value) {
$('#non_participants').bootstrapTable('remove', { $('#non_participants').bootstrapTable('remove', {
field: 'id', field: 'id',
@ -127,7 +125,6 @@
}); });
$("#remove").click(function () { $("#remove").click(function () {
$("#p_output").empty();
$.each(pRows, function (index, value) { $.each(pRows, function (index, value) {
$('#participants').bootstrapTable('remove', { $('#participants').bootstrapTable('remove', {
field: 'id', field: 'id',
@ -143,6 +140,19 @@
}); });
pRows = []; pRows = [];
}); });
$("#update").click(function () {
$.each(pRows, function (index, value) {
$('#participants').bootstrapTable('insertRow', {
index: 0,
row: {
name: value.name,
id: value.id
}
});
});
pRows = [];
});
}) })
</script> </script>
{% endblock script %} {% endblock script %}