1
//! The edit page over the shared forms: `TRANSACTION_EDIT` loaded by
2
//! `rpc::forms::load`, rendered by `form_render` and submitted through
3
//! `rpc::forms::submit`.
4

            
5
use askama::Template;
6
use axum::{
7
    Extension, Form,
8
    extract::Path,
9
    response::{IntoResponse, Response},
10
};
11
use forms::TRANSACTION_EDIT;
12
use uuid::Uuid;
13

            
14
use crate::pages::{
15
    HtmlTemplate,
16
    forms::edit::{Edit, Edited},
17
};
18
use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
19

            
20
pub const SUBMIT_PATH: &str = "/api/transaction/update";
21

            
22
const EDIT: Edit = Edit {
23
    form: &TRANSACTION_EDIT,
24
    noun: "Transaction",
25
    action: SUBMIT_PATH,
26
    page: "/transaction/{id}/edit",
27
    remedy: "",
28
};
29

            
30
#[derive(Template)]
31
#[template(path = "pages/transaction/update.html")]
32
struct UpdatePage {
33
    form: String,
34
}
35

            
36
/// The message of a successful edit, shown in the form's result box.
37
#[must_use]
38
11
pub fn updated_message(id: Uuid) -> String {
39
11
    format!("{}: {id}", t!("Transaction updated with ID"))
40
11
}
41

            
42
26
pub async fn transaction_update_page(
43
26
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
44
26
    Path(id): Path<String>,
45
26
) -> Result<impl IntoResponse, WebError> {
46
26
    let form = EDIT.loaded_form(jwt_auth.user.id, &id).await?;
47
15
    Ok(HtmlTemplate(UpdatePage { form }))
48
26
}
49

            
50
15
pub async fn transaction_update_submit(
51
15
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
52
15
    Form(pairs): Form<Vec<(String, String)>>,
53
15
) -> Result<Response, WebError> {
54
15
    Ok(match EDIT.submit(jwt_auth.user.id, pairs).await? {
55
        Edited::Saved { id, .. } => {
56
9
            EDIT.saved_response(jwt_auth.user.id, id, updated_message(id))
57
9
                .await?
58
        }
59
3
        Edited::Invalid(response) => response,
60
    })
61
15
}