Skip to main content

shield_dioxus/routes/
action.rs

1use dioxus::prelude::*;
2use serde_json::Value;
3use shield::{ActionForms, ResponseType};
4
5use crate::ErasedDioxusStyle;
6
7#[derive(Clone, PartialEq, Props)]
8pub struct ActionProps {
9    #[props(default = "index".to_owned())]
10    action_id: String,
11}
12
13#[component]
14pub fn Action(props: ActionProps) -> Element {
15    let response = use_server_future({
16        let action_id = props.action_id.clone();
17
18        move || forms(action_id.clone())
19    })?;
20    let style = use_context::<ErasedDioxusStyle>();
21
22    let response_read = response.read();
23    let response = response_read.as_ref().unwrap();
24
25    match response {
26        Ok(forms) => style.render(forms),
27        Err(err) => rsx! { "{err}" },
28    }
29}
30
31#[get("/api/auth/forms", parts: dioxus::fullstack::http::request::Parts)]
32async fn forms(action_id: String) -> Result<ActionForms> {
33    use anyhow::anyhow;
34
35    use crate::integration::DioxusIntegrationDyn;
36
37    let integration = parts
38        .extensions
39        .get::<DioxusIntegrationDyn>()
40        .ok_or_else(|| anyhow!("Dioxus Shield integration should be extracted."))?;
41    let shield = integration.extract_shield(&parts.extensions)?;
42    let session = integration.extract_session(&parts.extensions)?;
43
44    let forms = shield
45        .action_forms(&action_id, session)
46        .await
47        .context("Failed to get Shield action forms.")?;
48
49    Ok(forms)
50}
51
52#[post("/api/auth/call", parts: dioxus::fullstack::http::request::Parts)]
53pub async fn call(
54    action_id: String,
55    method_id: String,
56    provider_id: Option<String>,
57    // TODO: Would be nice if this argument could fill up with all unknown keys instead of setting name to `data[...]`.
58    data: Value,
59) -> Result<ResponseType> {
60    use anyhow::anyhow;
61    use serde_json::Value;
62    use shield::Request;
63
64    use crate::integration::DioxusIntegrationDyn;
65
66    tracing::info!("call data {data:#?}");
67
68    let integration = parts
69        .extensions
70        .get::<DioxusIntegrationDyn>()
71        .ok_or_else(|| anyhow!("Dioxus Shield integration should be extracted."))?;
72    let shield = integration.extract_shield(&parts.extensions)?;
73    let session = integration.extract_session(&parts.extensions)?;
74
75    let response = shield
76        .call(
77            &action_id,
78            &method_id,
79            provider_id.as_deref(),
80            session,
81            Request {
82                query: Value::Null,
83                form_data: data,
84            },
85        )
86        .await
87        .context("Failed to call Shield action.")?;
88
89    Ok(response)
90}