Skip to main content

edc_web_components/contexts/
counter_party_dids_context.rs

1use yew::prelude::*;
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct CounterParty {
5  pub did: String,
6  pub name: String,
7  pub logo: Option<String>,
8}
9
10#[derive(Clone, PartialEq)]
11pub struct CounterPartiesState {
12  counter_parties: Vec<CounterParty>,
13}
14
15impl CounterPartiesState {
16  pub fn counter_parties(&self) -> &[CounterParty] {
17    &self.counter_parties
18  }
19}
20
21#[derive(Properties, PartialEq)]
22pub struct CounterPartiesContextProviderProps {
23  #[prop_or_default]
24  pub children: Html,
25  pub counter_parties: Vec<CounterParty>,
26}
27
28#[component]
29pub fn CounterPartiesContextProvider(props: &CounterPartiesContextProviderProps) -> Html {
30  let counter_parties_context = use_state(move || CounterPartiesState {
31    counter_parties: props.counter_parties.clone(),
32  });
33
34  use_effect_with(
35    (
36      props.counter_parties.clone(),
37      counter_parties_context.setter(),
38    ),
39    |(counter_parties, counter_parties_context_setter)| {
40      counter_parties_context_setter.set(CounterPartiesState {
41        counter_parties: counter_parties.clone(),
42      });
43    },
44  );
45
46  html! {
47    <ContextProvider<CounterPartiesState> context={(*counter_parties_context).clone()}>
48      { props.children.clone() }
49    </ContextProvider<CounterPartiesState>>
50  }
51}
52
53#[hook]
54pub fn use_counter_parties_context() -> Option<CounterPartiesState> {
55  use_context::<CounterPartiesState>()
56}