Skip to content

Commit

Permalink
feat(tui): contract negotiations (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Sep 21, 2024
1 parent b91d6a2 commit 7916731
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 6 deletions.
6 changes: 5 additions & 1 deletion edc-connector-client/src/types/contract_negotiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ContractRequestBuilder {
}

#[serde_as]
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ContractNegotiation {
#[serde(rename = "@id")]
Expand Down Expand Up @@ -164,6 +164,10 @@ impl ContractNegotiation {
self.private_properties.get(property)
}

pub fn private_properties(&self) -> &Properties {
&self.private_properties
}

pub fn contract_agreement_id(&self) -> Option<&String> {
self.contract_agreement_id.as_ref()
}
Expand Down
33 changes: 32 additions & 1 deletion edc-connector-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use ratatui::{
use crate::{
components::{
assets::AssetsComponent, connectors::ConnectorsComponent,
contract_definitions::ContractDefinitionsComponent, footer::Footer,
contract_definitions::ContractDefinitionsComponent,
contract_negotiations::ContractNegotiationsComponent, footer::Footer,
header::HeaderComponent, launch_bar::LaunchBar, policies::PolicyDefinitionsComponent,
Action, Component, ComponentEvent, ComponentMsg, ComponentReturn, Notification,
NotificationMsg,
Expand All @@ -37,6 +38,7 @@ pub struct App {
policies: PolicyDefinitionsComponent,
assets: AssetsComponent,
contract_definitions: ContractDefinitionsComponent,
contract_negotiations: ContractNegotiationsComponent,
launch_bar: LaunchBar,
launch_bar_visible: bool,
focus: AppFocus,
Expand Down Expand Up @@ -91,6 +93,8 @@ impl App {
assets: AssetsComponent::default().on_fetch(Self::fetch_assets),
contract_definitions: ContractDefinitionsComponent::default()
.on_fetch(Self::fetch_contract_definitions),
contract_negotiations: ContractNegotiationsComponent::default()
.on_fetch(Self::fetch_contract_negotiations),
launch_bar: LaunchBar::default(),
launch_bar_visible: false,
focus: AppFocus::ConnectorList,
Expand Down Expand Up @@ -132,6 +136,7 @@ impl App {
Menu::Assets => self.assets.info_sheet(),
Menu::Policies => self.policies.info_sheet(),
Menu::ContractDefinitions => self.contract_definitions.info_sheet(),
Menu::ContractNegotiations => self.contract_negotiations.info_sheet(),
};

self.header.update_sheet(
Expand Down Expand Up @@ -189,6 +194,18 @@ impl App {
}
Ok(ComponentReturn::empty())
}
Menu::ContractNegotiations => {
self.focus = AppFocus::ContractNegotiations;
if let Some(connector) = self.connectors.selected() {
return Self::forward_init(
&mut self.contract_negotiations,
connector.clone(),
AppMsg::ContractNegotiations,
)
.await;
}
Ok(ComponentReturn::empty())
}
}
}
}
Expand All @@ -209,6 +226,7 @@ impl Component for App {
Menu::Assets => self.assets.view(f, main[2]),
Menu::Policies => self.policies.view(f, main[2]),
Menu::ContractDefinitions => self.contract_definitions.view(f, main[2]),
Menu::ContractNegotiations => self.contract_negotiations.view(f, main[2]),
}

self.footer.view(f, main[3]);
Expand Down Expand Up @@ -255,6 +273,14 @@ impl Component for App {
)
.await
}
AppMsg::ContractNegotiations(m) => {
Self::forward_update(
&mut self.contract_negotiations,
m.into(),
AppMsg::ContractNegotiations,
)
.await
}
AppMsg::HeaderMsg(m) => {
Self::forward_update(&mut self.header, m.into(), AppMsg::HeaderMsg).await
}
Expand Down Expand Up @@ -287,6 +313,11 @@ impl Component for App {
evt.clone(),
AppMsg::ContractDefinitions,
)?,
AppFocus::ContractNegotiations => Self::forward_event(
&mut self.contract_negotiations,
evt.clone(),
AppMsg::ContractNegotiations,
)?,
};

let msg = if msg.is_empty() {
Expand Down
14 changes: 13 additions & 1 deletion edc-connector-tui/src/app/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use edc_connector_client::types::query::Query;
use crate::{
components::{
assets::AssetEntry, contract_definitions::ContractDefinitionEntry,
policies::PolicyDefinitionEntry,
contract_negotiations::ContractNegotiationEntry, policies::PolicyDefinitionEntry,
},
types::connector::Connector,
};
Expand Down Expand Up @@ -35,6 +35,18 @@ impl App {
.collect())
}

pub async fn fetch_contract_negotiations(
connector: Connector,
) -> anyhow::Result<Vec<ContractNegotiationEntry>> {
Ok(connector
.client()
.contract_negotiations()
.query(Query::default())
.await?
.into_iter()
.map(ContractNegotiationEntry::new)
.collect())
}
pub async fn fetch_policies(
connector: Connector,
) -> anyhow::Result<Vec<PolicyDefinitionEntry>> {
Expand Down
1 change: 1 addition & 0 deletions edc-connector-tui/src/app/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub enum AppFocus {
Assets,
Policies,
ContractDefinitions,
ContractNegotiations,
}
4 changes: 3 additions & 1 deletion edc-connector-tui/src/app/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
components::{
assets::AssetsMsg, connectors::msg::ConnectorsMsg,
contract_definitions::ContractDefinitionsMsg, header::msg::HeaderMsg,
contract_definitions::ContractDefinitionsMsg,
contract_negotiations::ContractNegotiationMsg, header::msg::HeaderMsg,
launch_bar::msg::LaunchBarMsg, policies::PoliciesMsg, NotificationMsg,
},
types::nav::Nav,
Expand All @@ -16,6 +17,7 @@ pub enum AppMsg {
AssetsMsg(AssetsMsg),
PoliciesMsg(PoliciesMsg),
ContractDefinitions(ContractDefinitionsMsg),
ContractNegotiations(ContractNegotiationMsg),
HeaderMsg(HeaderMsg),
RoutingMsg(Nav),
NontificationMsg(NotificationMsg),
Expand Down
2 changes: 2 additions & 0 deletions edc-connector-tui/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::types::nav::Nav;
pub mod assets;
pub mod connectors;
pub mod contract_definitions;
pub mod contract_negotiations;
pub mod footer;
pub mod header;
pub mod launch_bar;
Expand Down Expand Up @@ -159,6 +160,7 @@ impl Notification {
}
}

#[allow(dead_code)]
pub fn info(msg: String) -> Notification {
Notification {
msg,
Expand Down
85 changes: 85 additions & 0 deletions edc-connector-tui/src/components/contract_negotiations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use edc_connector_client::types::contract_negotiation::ContractNegotiation;
use ratatui::widgets::Row;

use crate::components::resources::FieldValue;

use super::{
resources::{msg::ResourcesMsg, DrawableResource, Field, ResourcesComponent},
table::TableEntry,
};

#[derive(Debug, Clone)]
pub struct ContractNegotiationEntry(ContractNegotiation);

impl ContractNegotiationEntry {
pub fn new(contract_negotiation: ContractNegotiation) -> Self {
Self(contract_negotiation)
}
}

pub type ContractNegotiationMsg = ResourcesMsg<ContractNegotiationEntry>;
pub type ContractNegotiationsComponent = ResourcesComponent<ContractNegotiationEntry>;

impl TableEntry for ContractNegotiationEntry {
fn row(&self) -> Row {
let private_properties = serde_json::to_string(self.0.private_properties()).unwrap();
Row::new(vec![
self.0.id().to_string(),
format!("{:?}", self.0.kind()),
format!("{:?}", self.0.state()),
self.0.counter_party_id().to_string(),
self.0.contract_agreement_id().cloned().unwrap_or_default(),
private_properties,
self.0.created_at().to_string(),
])
}

fn headers() -> Row<'static> {
Row::new(vec![
"ID",
"TYPE",
"STATE",
"COUNTER_PARTY_ID",
"CONTRACT_AGREEMENT_ID",
"PRIVATE_PROPERTIES",
"CREATED_AT",
])
}
}

impl DrawableResource for ContractNegotiationEntry {
fn id(&self) -> &str {
self.0.id()
}

fn title() -> &'static str {
"Contract Negotiations"
}

fn fields(&self) -> Vec<Field> {
vec![
Field::string("id", self.0.id()),
Field::string("type", format!("{:?}", self.0.kind())),
Field::string("state", format!("{:?}", self.0.state())),
Field::string("counter_party_id", self.0.counter_party_id()),
Field::string("counter_party_address", self.0.counter_party_address()),
Field::string(
"contract_agreement_id",
self.0.contract_agreement_id().cloned().unwrap_or_default(),
),
Field::new(
"callback_addresses".to_string(),
FieldValue::Json(
serde_json::to_string_pretty(&self.0.callback_addresses()).unwrap(),
),
),
Field::new(
"private_properties".to_string(),
FieldValue::Json(
serde_json::to_string_pretty(&self.0.private_properties()).unwrap(),
),
),
Field::string("created_at", self.0.created_at().to_string()),
]
}
}
5 changes: 3 additions & 2 deletions edc-connector-tui/src/components/launch_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{Action, Component, ComponentEvent, ComponentMsg, ComponentReturn};
use ratatui::{
layout::Rect,
style::Style,
widgets::{Block, Borders},
widgets::{Block, Borders, Widget},
Frame,
};
use tui_textarea::{Input, Key, TextArea};
Expand Down Expand Up @@ -33,7 +33,8 @@ impl Component for LaunchBar {
text_area.set_block(Block::default().borders(Borders::all()));
text_area.set_cursor_line_style(Style::default());
text_area.set_placeholder_text("Enter command");
f.render_widget(self.area.widget(), rect)

self.area.render(rect, f.buffer_mut());
}

async fn update(
Expand Down
1 change: 1 addition & 0 deletions edc-connector-tui/src/nav.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions edc-connector-tui/src/types/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum Nav {
AssetsList,
PoliciesList,
ContractDefinitionsList,
ContractNegotiations,
}

impl FromStr for Nav {
Expand All @@ -34,6 +35,7 @@ pub enum Menu {
Assets,
Policies,
ContractDefinitions,
ContractNegotiations,
}

impl Menu {
Expand All @@ -52,6 +54,7 @@ impl From<Nav> for Menu {
Nav::AssetsList => Menu::Assets,
Nav::PoliciesList => Menu::Policies,
Nav::ContractDefinitionsList => Menu::ContractDefinitions,
Nav::ContractNegotiations => Menu::ContractNegotiations,
}
}
}
Expand All @@ -63,6 +66,7 @@ impl From<Menu> for Nav {
Menu::Assets => Nav::AssetsList,
Menu::Policies => Nav::PoliciesList,
Menu::ContractDefinitions => Nav::ContractDefinitionsList,
Menu::ContractNegotiations => Nav::ContractNegotiations,
}
}
}

0 comments on commit 7916731

Please sign in to comment.