edc_web_components/contexts/
edc_identity_hub_context.rs1use edc_identity_hub_client::{IdentityHubClient, IdentityHubClientVersion};
2use std::{ops::Deref, rc::Rc};
3use yew::prelude::*;
4use yew_oauth2::context::LatestAccessToken;
5use yew_oauth2::prelude::use_latest_access_token;
6
7#[derive(Debug, PartialEq, Clone)]
8pub enum EdcIdentityHubAction {}
9
10#[derive(Clone, PartialEq)]
11pub struct EdcIdentityHubState {
12 participant_id: String,
13 latest_access_token_context: LatestAccessToken,
14}
15
16impl EdcIdentityHubState {
17 pub fn get_client(&self) -> IdentityHubClient {
18 let client = reqwest::Client::new();
19
20 let server_url = web_sys::window().unwrap().location().origin().unwrap();
21
22 IdentityHubClient::new(
23 client,
24 format!("{server_url}/identity-hub"),
25 self.latest_access_token_context.access_token(),
26 IdentityHubClientVersion::V1Alpha,
27 )
28 }
29
30 pub fn participant_id(&self) -> &str {
31 &self.participant_id
32 }
33}
34
35impl Reducible for EdcIdentityHubState {
36 type Action = EdcIdentityHubAction;
37
38 fn reduce(self: Rc<Self>, _action: Self::Action) -> Rc<Self> {
39 let new_self = self.deref().clone();
40
41 new_self.into()
42 }
43}
44
45#[derive(Properties, PartialEq)]
46pub struct EdcIdentityHubContextProviderProps {
47 #[prop_or_default]
48 pub children: Html,
49 pub participant_id: String,
50}
51
52#[component]
53pub fn EdcIdentityHubContextProvider(props: &EdcIdentityHubContextProviderProps) -> Html {
54 let latest_access_token_context = use_latest_access_token().unwrap();
55
56 let edc_connector_context = use_reducer(move || EdcIdentityHubState {
57 participant_id: props.participant_id.clone(),
58 latest_access_token_context,
59 });
60
61 html! {
62 <ContextProvider<EdcIdentityHubContext> context={edc_connector_context}>
63 { props.children.clone() }
64 </ContextProvider<EdcIdentityHubContext>>
65 }
66}
67
68pub type EdcIdentityHubContext = UseReducerHandle<EdcIdentityHubState>;
69
70#[hook]
71pub fn use_edc_identity_hub_context() -> EdcIdentityHubContext {
72 use_context::<EdcIdentityHubContext>().expect("no EDC Identity Hub context found")
73}