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_axum::{IntoResponse, Response};
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 chrono::Utc;
use http::StatusCode;
use http::{header::CONTENT_TYPE, StatusCode};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
@ -213,16 +218,44 @@ pub struct ExchangeForm {
}
pub async fn giftexchange_save(
Path(exchange_id): Path<i64>,
Extension(user_data): Extension<Option<UserData>>,
State(db_pool): State<SqlitePool>,
Form(item_form): Form<ExchangeForm>,
request: Request<Body>,
) -> impl IntoResponse {
println!("Saving gift exchange: {:?}", item_form);
if is_authorized("/giftexchange", user_data.clone(), db_pool.clone()).await {
// Insert new item to database
let now = Utc::now().timestamp();
let content_type_header = request.headers().get(CONTENT_TYPE);
let content_type = content_type_header.and_then(|value| value.to_str().ok());
}
Redirect::to("/").into_response()
/* if let Some(content_type) = content_type {
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"
crossorigin="anonymous"></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 %}
</body>

View File

@ -5,7 +5,7 @@
<div class="row">
<div class="btn-toolbar" role="toolbar">
<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 class="row">
@ -20,12 +20,12 @@
</div>
<div class="row align-items-center text-center">
<div class="col">
<table id="non_participants" data-toggle="table" data-click-to-select="true" class="table table-bordered"
height="400">
<table id="non_participants" data-toggle="table" data-click-to-select="true"
data-height="400">
<thead>
<tr>
<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>
</tr>
</thead>
@ -47,12 +47,11 @@
</div>
</div>
<div class="col">
<table id="participants" data-toggle="table" data-click-to-select="true" class="table table-bordered"
height="400">
<table id="participants" data-toggle="table" data-click-to-select="true" data-height="400">
<thead>
<tr>
<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>
</tr>
</thead>
@ -109,7 +108,6 @@
});
$("#add").click(function () {
$("#output").empty();
$.each(npRows, function (index, value) {
$('#non_participants').bootstrapTable('remove', {
field: 'id',
@ -127,7 +125,6 @@
});
$("#remove").click(function () {
$("#p_output").empty();
$.each(pRows, function (index, value) {
$('#participants').bootstrapTable('remove', {
field: 'id',
@ -143,6 +140,19 @@
});
pRows = [];
});
$("#update").click(function () {
$.each(pRows, function (index, value) {
$('#participants').bootstrapTable('insertRow', {
index: 0,
row: {
name: value.name,
id: value.id
}
});
});
pRows = [];
});
})
</script>
{% endblock script %}