this feels like the jankiest code but it works

might use it in the settings page once i refactor it
This commit is contained in:
Artemis 2025-02-11 11:59:24 +01:00
parent 9d674863df
commit 897ab437c1
4 changed files with 84 additions and 14 deletions

View file

@ -84,7 +84,7 @@ fn rocket() -> _ {
account::export_data,
],
)
.mount("/admin", routes![admin::index,])
.mount("/admin", routes![admin::index, admin::handle_in_page_forms])
.mount(
"/",
routes![

View file

@ -71,7 +71,7 @@ impl<'r> FromRequest<'r> for CommonTemplateState {
/// FakeContext exists to be used as a replacement for Context when reusing a form for creation and edition, to avoid repeating lots of code.
///
/// Note: i made this custom context thingy because i couldn't find a way to create a real context with the right populated data.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
#[serde(crate = "rocket::serde")]
pub struct FakeContext {
/// Values are made to simulate the same structure as the real Context

View file

@ -1,3 +1,7 @@
use rocket::{
form::{Context, Contextual, Form},
serde::Serialize,
};
use rocket_dyn_templates::{context, Template};
use sqlx::Acquire;
@ -9,14 +13,6 @@ use crate::{
use super::error_handlers::PageResult;
#[derive(FromForm)]
pub struct TagHandover<'a> {
#[field(validate=crate::ids::validate_id())]
pub tag_id: &'a str,
#[field(validate=len(..=256))]
pub dest_account: &'a str,
}
#[get("/")]
pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) -> PageResult {
let mut trx = db.begin().await?;
@ -32,3 +28,76 @@ pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) -
)
.into())
}
#[derive(Debug, FromFormField, Serialize)]
#[serde(crate = "rocket::serde")]
pub enum SelectedForm {
TagHandover,
}
#[derive(Debug, FromForm)]
pub struct Forms<'a> {
pub form: SelectedForm,
pub tag_handover: Contextual<'a, TagHandover<'a>>,
}
#[derive(Debug, Serialize)]
#[serde(crate = "rocket::serde")]
pub struct Contexts<'a, 'b> {
pub form: &'a SelectedForm,
pub tag_handover: &'a Context<'b>,
}
impl<'a, 'b> From<&'a Form<Forms<'b>>> for Contexts<'a, 'b> {
fn from(value: &'a Form<Forms<'b>>) -> Self {
Contexts {
form: &value.form,
tag_handover: &value.tag_handover.context,
}
}
}
#[derive(Debug, FromForm)]
pub struct TagHandover<'a> {
#[field(validate=crate::ids::validate_id())]
pub tag_id: &'a str,
#[field(validate=len(..=256))]
pub dest_account: &'a str,
}
#[post("/", data = "<form>")]
pub async fn handle_in_page_forms(
meta: CommonTemplateState,
mut db: DollTagsDb,
user: Admin,
form: Form<Forms<'_>>,
) -> PageResult {
println!("{:?}", form);
match form.form {
SelectedForm::TagHandover => {
if let Some(ref values) = form.tag_handover.value {
// work
println!(
"woof handover of {} to {}",
values.tag_id, values.dest_account
);
}
}
};
let mut trx = db.begin().await?;
let service_status = admin::get_service_status(&mut trx).await?;
trx.commit().await?;
let previous = Contexts::from(&form);
Ok(Template::render(
"admin/index",
context! {
meta: meta.for_user(&user.0),
service_status,
previous,
},
)
.into())
}

View file

@ -31,17 +31,18 @@
</p>
<form method="post">
<input type="hidden" name="form" value="TagHandover" />
<div class="fields raised">
<div class="dual-fields">
<div>
<p class="ident center">ID of the tag to hand over</p>
<input type="text" name="tag_id" id="tag_id" placeholder="000000" minlength="6" maxlength="6"
required />
<input type="text" name="tag_handover.tag_id" id="tag_id" placeholder="000000" minlength="6"
maxlength="6" required />
</div>
<div>
<p class="ident center">Destination account username</p>
<input type="text" name="dest_account" id="dest_account" autocomplete="off" required />
<input type="text" name="tag_handover.dest_account" id="dest_account" autocomplete="off" required />
</div>
</div>
@ -52,4 +53,4 @@
</form>
</section>
{% endblock main %}
{% endblock main %}