Skip to main content

edc_web_components/pages/
show_contract_negotiation.rs

1use crate::components::ContractNegotiationStatus;
2use crate::contexts::use_edc_connector_context;
3#[cfg(feature = "contract-negotiation-review")]
4use edc_connector_client::types::contract_negotiation::{
5  ContractNegotiationKind, ContractNegotiationState,
6};
7use patternfly_yew::prelude::*;
8use yew::prelude::*;
9use yew::suspense::use_future_with;
10
11#[derive(Clone, Debug, PartialEq, Properties)]
12pub struct ShowContractNegotiationPageProps {
13  pub contract_negotiation_id: String,
14  #[prop_or_default]
15  pub on_contract_agreement_click: Callback<String>,
16}
17
18#[component]
19pub fn ShowContractNegotiationPage(props: &ShowContractNegotiationPageProps) -> Html {
20  html!(
21    <>
22      <Title level={Level::H2} size={Size::XXXLarge}>{ "Contract Negotiation" }</Title>
23      <Suspense fallback="Loading ...">
24        <ShowContractNegotiationPageInner
25          contract_negotiation_id={props.contract_negotiation_id.clone()}
26          on_contract_agreement_click={props.on_contract_agreement_click.clone()}
27        />
28      </Suspense>
29    </>
30  )
31}
32
33#[component]
34pub fn ShowContractNegotiationPageInner(props: &ShowContractNegotiationPageProps) -> HtmlResult {
35  let edc_connector_client = use_edc_connector_context();
36
37  let refresh = use_state(|| 0usize);
38
39  let contract_negotiation = use_future_with(
40    (
41      props.contract_negotiation_id.clone(),
42      edc_connector_client.clone(),
43      refresh.clone(),
44    ),
45    |properties| async move {
46      let (contract_negotiation_id, edc_connector_client, _) = (*properties).clone();
47
48      if let Some(client) = edc_connector_client.get_client() {
49        client
50          .contract_negotiations(edc_connector_client::EdcConnectorApiVersion::V4)
51          .get(&contract_negotiation_id)
52          .await
53          .ok()
54      } else {
55        None
56      }
57    },
58  )?;
59
60  let on_finalized = use_callback(refresh.setter(), |_, refresh_setter| {
61    refresh_setter.set(1);
62  });
63
64  #[cfg(feature = "contract-negotiation-review")]
65  let on_reviewed = use_callback(refresh.setter(), |_, refresh_setter| {
66    refresh_setter.set(1);
67  });
68
69  let contract_negotiation = (*contract_negotiation).clone();
70
71  if let Some(contract_negotiation) = contract_negotiation {
72    let contract_agreement =
73      if let Some(contract_agreement_id) = contract_negotiation.contract_agreement_id() {
74        let onclick = {
75          let contract_agreement_id = contract_agreement_id.clone();
76
77          props
78            .on_contract_agreement_click
79            .reform(move |_| contract_agreement_id.clone())
80        };
81
82        html!(
83          <Button variant={ButtonVariant::InlineLink} {onclick}>{ contract_agreement_id }</Button>
84        )
85      } else {
86        html!()
87      };
88
89    #[cfg(feature = "contract-negotiation-review")]
90    let review = if contract_negotiation.state() == &ContractNegotiationState::Requested
91      && contract_negotiation.kind() == &ContractNegotiationKind::Provider
92    {
93      let contract_negotiation_id = contract_negotiation.id().to_string();
94
95      html!(
96        <Alert title="Review the Contract Negotiation">
97          <Suspense fallback={html!(<Bullseye><Spinner /></Bullseye>)}>
98            <crate::components::ReviewContractNegotiation {contract_negotiation_id} {on_reviewed} />
99          </Suspense>
100        </Alert>
101      )
102    } else {
103      html!()
104    };
105
106    #[cfg(not(feature = "contract-negotiation-review"))]
107    let review = html!();
108
109    Ok(html!(
110      <Stack gutter=true>
111        <StackItem>
112          <DescriptionList mode={[DescriptionListMode::Horizontal]}>
113            <DescriptionGroup term="Id">{ contract_negotiation.id() }</DescriptionGroup>
114            <DescriptionGroup term="Contract Agreement Id">{ contract_agreement }</DescriptionGroup>
115            <DescriptionGroup term="Counter Party ID">
116              { contract_negotiation.counter_party_id().clone().unwrap_or_default() }
117            </DescriptionGroup>
118          </DescriptionList>
119        </StackItem>
120        <StackItem>
121          <ContractNegotiationStatus
122            contract_negotiation_id={props.contract_negotiation_id.clone()}
123            {on_finalized}
124          />
125        </StackItem>
126        <StackItem>{ review }</StackItem>
127      </Stack>
128    ))
129  } else {
130    Ok(html!(
131      format!(
132      "Contract Negotiation with id {} not found.",
133      props.contract_negotiation_id
134    )
135    ))
136  }
137}