use axum::{ Form, extract::{Path, Request, State}, http::HeaderMap, response::{IntoResponse, Redirect, Response}, }; use maud::html; use serde::Deserialize; use uuid::Uuid; use crate::{ MnemoState, error::CompositeError, logs::{LogAction, LogEntry}, persons::{Name, Person}, users::{ User, auth::{UserAuthRequired, UserAuthenticate}, }, web::{components::nav::nav, pages::base}, }; pub async fn page( State(state): State, Path(id): Path, req: Request, ) -> Result { let mut conn = state.pool.acquire().await?; let u = match User::authenticate(&mut *conn, req.headers()).await? { Some(u) => u, None => return Ok(Redirect::to(&format!("/login?r={}", req.uri().path())).into_response()), }; let p = Person::get_by_id(&mut *conn, id).await; let title = match &p { Ok(p) => format!("~{} | Mnemosyne", p.primary_name), Err(_) => "Error! | Mnemosyne".into(), }; let mut names_with_attribution = Vec::new(); let mut names_ok = false; if let Ok(ref person) = p { if let Ok(names) = person.get_all_names(&mut *conn).await { names_ok = true; for name in names { let attr = name.times_attributed(&mut *conn).await.unwrap_or(0); names_with_attribution.push((name, attr)); } } } Ok(base( &title, html!( (nav(&mut conn, Some(&u), req.uri().path()).await) div class="mx-auto max-w-4xl px-2 my-4" { @if let Ok(p) = p { div class="flex items-center gap-2 mb-6" { span class="text-neutral-500 scale-150" {"~"} h1 class="text-3xl font-semibold font-lora" {(p.primary_name)} } div { h2 class="text-lg font-semibold font-lora mb-2 text-neutral-300" {"Names"} div class="flex flex-wrap gap-2 mb-4" { @if names_ok { @for (name, attr) in names_with_attribution { div class="rounded px-3 py-1 bg-neutral-200/5 border border-neutral-200/10 text-sm flex items-center gap-2" { (name.name) @if name.is_primary { span class="text-xs text-neutral-500" {"(primary)"} } @if attr == 0 && !name.is_primary { form action=(format!("/names/{}/delete", name.id)) method="post" class="flex items-center ml-1" { button type="submit" class="text-neutral-500 hover:text-red-400 flex items-center justify-center cursor-pointer" title="Delete" { "✕" } } } } } } @else { "Failed to get names." } } form action=(format!("/persons/{}/add-name", p.id)) method="post" { label for="name" class="text-neutral-500 font-light text-sm" {"Add Name"} div class="flex gap-2 mt-1" { input type="text" autocomplete="off" id="name" name="name" placeholder="e.g. Frank" class="px-2 py-1 border border-neutral-200/25 bg-neutral-950/50 rounded w-full sm:w-auto"; button type="submit" class="px-4 py-1 border border-neutral-200/25 bg-neutral-200/5 rounded cursor-pointer hover:bg-neutral-200/10 hover:border-neutral-200/45" {"Add"} } } } } @else { p class="text-center p-2 my-4 text-red-400" {"Person not found."} } } ), ) .into_response()) } #[derive(Deserialize)] pub struct AddNameForm { name: String, } pub async fn add_name( State(state): State, Path(id): Path, headers: HeaderMap, Form(form): Form, ) -> Result { let mut tx = state.pool.begin().await?; let u = User::authenticate(&mut *tx, &headers).await?.required()?; let p = Person::get_by_id(&mut *tx, id).await?; let n = p.add_name(&mut *tx, form.name, u.id).await?; LogEntry::new( &mut *tx, u, LogAction::AddPersonName { pid: p.id, nid: n.id, pn: p.primary_name, nn: n.name, }, ) .await?; tx.commit().await?; Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response()) } pub async fn delete_name( State(state): State, Path(id): Path, headers: HeaderMap, ) -> Result { let mut tx = state.pool.begin().await?; let u = User::authenticate(&mut *tx, &headers).await?.required()?; let n = Name::get_by_id(&mut *tx, id).await?; let p = Person::get_by_id(&mut *tx, n.person_id).await?; let nn = n.name.clone(); n.delete(&mut *tx).await?; LogEntry::new( &mut *tx, u, LogAction::DeletePersonName { pid: p.id, nid: id, pn: p.primary_name, n: nn, }, ) .await?; tx.commit().await?; Ok(Redirect::to(&format!("/persons/{}", p.id)).into_response()) }