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