Skip to main content

edc_web_components/pages/
contract_negotiation_page.rs

1use crate::components::ListContractNegotiations;
2use crate::contexts::use_edc_connector_context;
3use crate::models::ContractNegotiationItem;
4use edc_connector_client::EdcConnectorApiVersion;
5use edc_connector_client::types::query::Query;
6use patternfly_yew::prelude::*;
7use yew::prelude::*;
8use yew::suspense::use_future_with;
9
10#[derive(Clone, Debug, PartialEq, Properties)]
11pub struct ContractNegotiationPageProps {
12  pub on_show_contract_negotiation: Callback<String>,
13}
14
15#[component]
16pub fn ContractNegotiationPage(props: &ContractNegotiationPageProps) -> Html {
17  let refresh = use_state(|| 0usize);
18  let offset = use_state(|| 0usize);
19  let limit = use_state(|| 10usize);
20  let switch_view_consumer = use_state(|| false);
21
22  let on_offset = use_callback(
23    (refresh.clone(), offset.setter()),
24    |offset, (refresh, offset_setter)| {
25      offset_setter.set(offset);
26      refresh.set(**refresh + 1);
27    },
28  );
29
30  let on_limit = use_callback(
31    (refresh.clone(), limit.setter()),
32    |limit, (refresh, limit_setter)| {
33      limit_setter.set(limit);
34      refresh.set(**refresh + 1);
35    },
36  );
37
38  let on_switch_view_consumer = use_callback(
39    (refresh.clone(), switch_view_consumer.setter()),
40    |switch, (refresh, switch_setter)| {
41      switch_setter.set(switch);
42      refresh.set(**refresh + 1);
43    },
44  );
45
46  let fallback = html! {
47    <Bullseye>
48      <Spinner size={SpinnerSize::Lg} />
49    </Bullseye>
50  };
51
52  html!(
53    <Stack gutter=true>
54      <StackItem>
55        <Split gutter=true>
56          <SplitItem fill=true>
57            <Title level={Level::H3} size={Size::XXLarge}>{ "List Contract Negotiations" }</Title>
58          </SplitItem>
59        </Split>
60      </StackItem>
61      <StackItem>
62        <Suspense {fallback}>
63          <ContractNegotiationPageInner
64            offset={*offset}
65            limit={*limit}
66            switch_view_consumer={*switch_view_consumer}
67            {on_offset}
68            {on_limit}
69            {on_switch_view_consumer}
70            force_refresh={*refresh}
71            on_show_contract_negotiation={props.on_show_contract_negotiation.clone()}
72          />
73        </Suspense>
74      </StackItem>
75    </Stack>
76  )
77}
78
79#[derive(Clone, Debug, PartialEq, Properties)]
80pub struct ContractNegotiationPageInnerProps {
81  pub offset: usize,
82  pub limit: usize,
83  pub switch_view_consumer: bool,
84  pub on_offset: Callback<usize>,
85  pub on_limit: Callback<usize>,
86  pub on_switch_view_consumer: Callback<bool>,
87  pub force_refresh: usize,
88  pub on_show_contract_negotiation: Callback<String>,
89}
90
91#[component]
92pub fn ContractNegotiationPageInner(props: &ContractNegotiationPageInnerProps) -> HtmlResult {
93  let edc_connector_context = use_edc_connector_context();
94
95  let contract_negotiation_items = use_future_with(
96    (
97      edc_connector_context,
98      props.limit,
99      props.offset,
100      props.switch_view_consumer,
101      props.force_refresh,
102    ),
103    |parameters| async move {
104      let (edc_connector_context, limit, offset, switch_view_consumer, _) = (*parameters).clone();
105
106      let query = Query::builder()
107        .limit(limit as u32)
108        .offset(offset as u32)
109        .filter(
110          "type",
111          "=",
112          if switch_view_consumer {
113            "CONSUMER"
114          } else {
115            "PROVIDER"
116          },
117        )
118        .build();
119
120      if let Some(client) = edc_connector_context.get_client() {
121        client
122          .contract_negotiations(EdcConnectorApiVersion::V4)
123          .query(query)
124          .await
125          .unwrap_or_default()
126          .into_iter()
127          .map(ContractNegotiationItem::from)
128          .collect()
129      } else {
130        vec![]
131      }
132    },
133  )?;
134
135  let contract_negotiation_items = (*contract_negotiation_items).clone();
136
137  Ok(html!(
138    <ListContractNegotiations
139      contract_negotiation_items={contract_negotiation_items}
140      offset={props.offset}
141      limit={props.limit}
142      switch={props.switch_view_consumer}
143      on_offset={props.on_offset.clone()}
144      on_limit={props.on_limit.clone()}
145      on_switch_view_consumer={props.on_switch_view_consumer.clone()}
146      on_show_contract_negotiation={props.on_show_contract_negotiation.clone()}
147    />
148  ))
149}