peillute/views/
user.rs

1//! User management component for the Peillute application
2//!
3//! This module provides a component for displaying user information and
4//! managing user-specific actions, including viewing balance and accessing
5//! various transaction operations.
6
7use crate::Route;
8use dioxus::prelude::*;
9
10/// User management component
11///
12/// Displays user information and provides navigation to various transaction
13/// operations, including:
14/// - Viewing transaction history
15/// - Making withdrawals
16/// - Making payments
17/// - Processing refunds
18/// - Transferring money
19/// - Making deposits
20#[component]
21pub fn User(name: String) -> Element {
22    let mut solde = use_signal(|| 0f64);
23
24    let name = std::rc::Rc::new(name);
25    let name_for_future = name.clone();
26
27    {
28        use_future(move || {
29            let name = name_for_future.clone();
30            async move {
31                if let Ok(data) = get_solde(name.to_string()).await {
32                    solde.set(data);
33                }
34            }
35        });
36    }
37
38    let history_route = Route::History {
39        name: name.to_string(),
40    };
41    let withdraw_route = Route::Withdraw {
42        name: name.to_string(),
43    };
44    let pay_route = Route::Pay {
45        name: name.to_string(),
46    };
47    let refund_route = Route::Refund {
48        name: name.to_string(),
49    };
50    let transfer_route = Route::Transfer {
51        name: name.to_string(),
52    };
53    let deposit_route = Route::Deposit {
54        name: name.to_string(),
55    };
56
57    rsx! {
58        div { id: "user-info",
59            h1 { "Welcome {name}!" }
60            h2 { "{solde()} €" }
61        }
62        div { id: "user-page",
63            Link { to: history_route, "History" }
64            Link { to: withdraw_route, "Withdraw" }
65            Link { to: pay_route, "Pay" }
66            Link { to: refund_route, "Refund" }
67            Link { to: transfer_route, "Transfer" }
68            Link { to: deposit_route, "Deposit" }
69        }
70        Outlet::<Route> {}
71    }
72}
73
74/// Server function to retrieve a user's current balance
75#[server]
76async fn get_solde(name: String) -> Result<f64, ServerFnError> {
77    use crate::db;
78    let solde = db::calculate_solde(&name)?;
79    Ok(solde)
80}