edc_web_components/contexts/
redirection_context.rs1use std::{ops::Deref, rc::Rc};
2use yew::prelude::*;
3
4#[derive(Debug, PartialEq, Clone)]
5pub enum RedirectionAction {
6 Assets,
7 Asset(String),
8 Policies,
9 Policy(String),
10 ContractDefinitions,
11 ContractNegotiations,
12 ContractNegotiation(String),
13 ContractAgreements,
14 ContractAgreement(String),
15 TransferProcesses,
16 TransferProcess(String),
17 VerifiableCredentials,
18 VerifiableCredential(String),
19}
20
21#[derive(Clone, PartialEq)]
22pub struct RedirectionState {
23 redirect_to: Callback<RedirectionAction>,
24}
25
26impl RedirectionState {
27 pub fn redirect_to(&self) -> Callback<RedirectionAction> {
28 self.redirect_to.clone()
29 }
30}
31
32impl Reducible for RedirectionState {
33 type Action = RedirectionAction;
34
35 fn reduce(self: Rc<Self>, action: Self::Action) -> Rc<Self> {
36 let new_self = self.deref().clone();
37 new_self.redirect_to.emit(action);
38 new_self.into()
39 }
40}
41
42#[derive(Properties, PartialEq)]
43pub struct RedirectionContextProviderProps {
44 #[prop_or_default]
45 pub children: Html,
46 pub redirect_to: Callback<RedirectionAction>,
47}
48
49#[component]
50pub fn RedirectionContextProvider(props: &RedirectionContextProviderProps) -> Html {
51 let redirection_context = use_reducer(move || RedirectionState {
52 redirect_to: props.redirect_to.clone(),
53 });
54
55 html! {
56 <ContextProvider<RedirectionContext> context={redirection_context}>
57 { props.children.clone() }
58 </ContextProvider<RedirectionContext>>
59 }
60}
61
62pub type RedirectionContext = UseReducerHandle<RedirectionState>;
63
64#[hook]
65pub fn use_redirection_context() -> Option<RedirectionContext> {
66 use_context::<RedirectionContext>()
67}