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
21  let onoffset = use_callback(
22    (refresh.clone(), offset.setter()),
23    |offset, (refresh, offset_setter)| {
24      offset_setter.set(offset);
25      refresh.set(**refresh + 1);
26    },
27  );
28
29  let onlimit = use_callback(
30    (refresh.clone(), limit.setter()),
31    |limit, (refresh, limit_setter)| {
32      limit_setter.set(limit);
33      refresh.set(**refresh + 1);
34    },
35  );
36
37  html!(
38    <Stack gutter=true>
39      <StackItem>
40        <Split gutter=true>
41          <SplitItem fill=true>
42            <Title level={Level::H3} size={Size::XXLarge}>{ "List Contract Negotiations" }</Title>
43          </SplitItem>
44        </Split>
45      </StackItem>
46      <StackItem>
47        <Suspense>
48          <ContractNegotiationPageInner
49            offset={*offset}
50            limit={*limit}
51            {onoffset}
52            {onlimit}
53            force_refresh={*refresh}
54            on_show_contract_negotiation={props.on_show_contract_negotiation.clone()}
55          />
56        </Suspense>
57      </StackItem>
58    </Stack>
59  )
60}
61
62#[derive(Clone, Debug, PartialEq, Properties)]
63pub struct ContractNegotiationPageInnerProps {
64  pub offset: usize,
65  pub limit: usize,
66  pub onoffset: Callback<usize>,
67  pub onlimit: Callback<usize>,
68  pub force_refresh: usize,
69  pub on_show_contract_negotiation: Callback<String>,
70}
71
72#[component]
73pub fn ContractNegotiationPageInner(props: &ContractNegotiationPageInnerProps) -> HtmlResult {
74  let edc_connector_context = use_edc_connector_context();
75
76  let contract_negotiation_items = use_future_with(
77    (
78      edc_connector_context,
79      props.limit,
80      props.offset,
81      props.force_refresh,
82    ),
83    |parameters| async move {
84      let (edc_connector_context, limit, offset, _) = (*parameters).clone();
85
86      let query = Query::builder()
87        .limit(limit as u32)
88        .offset(offset as u32)
89        .build();
90
91      if let Some(client) = edc_connector_context.get_client() {
92        client
93          .contract_negotiations(EdcConnectorApiVersion::V4)
94          .query(query)
95          .await
96          .unwrap_or_default()
97          .into_iter()
98          .map(ContractNegotiationItem::from)
99          .collect()
100      } else {
101        vec![]
102      }
103    },
104  )?;
105
106  let contract_negotiation_items = (*contract_negotiation_items).clone();
107
108  Ok(html!(
109    <ListContractNegotiations
110      contract_negotiation_items={contract_negotiation_items}
111      offset={props.offset}
112      limit={props.limit}
113      onoffset={props.onoffset.clone()}
114      onlimit={props.onlimit.clone()}
115      on_show_contract_negotiation={props.on_show_contract_negotiation.clone()}
116    />
117  ))
118}