use crate::contexts::use_edc_connector_context;
use crate::models::AssetItem;
use edc_connector_client::EdcConnectorApiVersion;
use patternfly_yew::prelude::*;
use yew::platform::spawn_local;
use yew::prelude::*;
use yew::suspense::use_future_with;
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct ShowAssetPageProps {
pub asset_id: String,
pub on_deleted: Callback<()>,
pub dcterm_types: Vec<(String, String)>,
}
#[component]
pub fn ShowAssetPage(props: &ShowAssetPageProps) -> Html {
html!(
<>
<Title level={Level::H2} size={Size::XXXLarge}>{ "Show Asset" }</Title>
<Suspense fallback="Loading ...">
<ShowAssetPageInner
asset_id={props.asset_id.clone()}
on_deleted={props.on_deleted.clone()}
dcterm_types={props.dcterm_types.clone()}
/>
</Suspense>
</>
)
}
#[component]
pub fn ShowAssetPageInner(props: &ShowAssetPageProps) -> HtmlResult {
let edc_connector_client = use_edc_connector_context();
let asset = use_future_with(
(props.asset_id.clone(), edc_connector_client.clone()),
|properties| async move {
let (asset_id, edc_connector_client) = (*properties).clone();
if let Some(client) = edc_connector_client.get_client() {
client
.assets(EdcConnectorApiVersion::V4)
.get(&asset_id)
.await
.ok()
.map(AssetItem::from)
} else {
None
}
},
)?;
let asset = (*asset).clone();
let edc_connector_context = use_edc_connector_context();
let ondelete = use_callback(
(
props.asset_id.clone(),
edc_connector_context,
props.on_deleted.clone(),
),
|_, (asset_id, edc_connector_context, on_deleted)| {
let edc_connector_context = edc_connector_context.clone();
let asset_id = asset_id.clone();
let on_deleted = on_deleted.clone();
spawn_local(async move {
if let Some(client) = edc_connector_context.get_client()
&& client
.assets(EdcConnectorApiVersion::V4)
.delete(&asset_id)
.await
.is_ok()
{
on_deleted.emit(());
}
});
},
);
if let Some(asset_item) = asset {
let keywords = asset_item
.keywords
.iter()
.map(|keyword| html! { <Label label={keyword.to_string()} color={Color::Blue} /> });
let dcterm_types = if !asset_item.dcterm_types.is_empty() {
let labels = asset_item
.dcterm_types
.iter()
.map(|dcterm_type| {
props
.dcterm_types
.iter()
.find(|(dcterm_type_name, _)| dcterm_type_name == dcterm_type)
.map(|(_, dcterm_type_label)| dcterm_type_label.to_string())
.unwrap_or(dcterm_type.to_string())
})
.map(|dcterm_type| {
html_nested!(
<FlexItem>
<Label label={dcterm_type} />
</FlexItem>
)
});
html!(
<DescriptionGroup term="Dataset Type(s)">
<Flex>{ for labels }</Flex>
</DescriptionGroup>
)
} else {
html!()
};
Ok(html!(
<>
<Card>
<CardBody>
<DescriptionList>
<DescriptionGroup term="ID">{ asset_item.id }</DescriptionGroup>
</DescriptionList>
</CardBody>
</Card>
<Card>
<CardHeader>
<Title level={Level::H2} size={Size::XXXLarge}>{ "Provider Information" }</Title>
</CardHeader>
<CardBody>
<DescriptionList>
<DescriptionGroup term="Company Name">
{ asset_item.creator.clone().and_then(|creator| creator.name).unwrap_or_default() }
</DescriptionGroup>
<DescriptionGroup term="Company Logo URL">
{ asset_item.creator.and_then(|creator| creator.thumbnail).and_then(|thumbnail| thumbnail.resource).unwrap_or_default() }
</DescriptionGroup>
</DescriptionList>
</CardBody>
</Card>
<Card>
<CardHeader>
<Title level={Level::H2} size={Size::XXXLarge}>{ "Dataset Description" }</Title>
</CardHeader>
<CardBody>
<DescriptionList>
<DescriptionGroup term="Title">{ asset_item.name }</DescriptionGroup>
<DescriptionGroup term="Version">
{ asset_item.version.map(|version| version.to_string()).unwrap_or_default() }
</DescriptionGroup>
<DescriptionGroup term="Description">
{ asset_item.description.unwrap_or_default() }
</DescriptionGroup>
<DescriptionGroup term="Keywords">{ for keywords }</DescriptionGroup>
<DescriptionGroup term="Landing Page">
{ asset_item.landing_page.unwrap_or_default() }
</DescriptionGroup>
{ dcterm_types }
</DescriptionList>
</CardBody>
</Card>
<Card>
<CardHeader>
<Title level={Level::H2} size={Size::XXXLarge}>{ "Dataset Source" }</Title>
</CardHeader>
<CardBody>
<DescriptionList>
<DescriptionGroup term="Base URL">{ asset_item.base_url }</DescriptionGroup>
<DescriptionGroup term="Proxy Path">
<Switch disabled=true checked={asset_item.proxy_path} />
</DescriptionGroup>
<DescriptionGroup term="Proxy Query Parameters">
<Switch disabled=true checked={asset_item.proxy_query_params} />
</DescriptionGroup>
<DescriptionGroup term="Proxy Method">
<Switch disabled=true checked={asset_item.proxy_method} />
</DescriptionGroup>
<DescriptionGroup term="Proxy Body">
<Switch disabled=true checked={asset_item.proxy_body} />
</DescriptionGroup>
</DescriptionList>
</CardBody>
</Card>
<Flex>
<FlexItem modifiers={[FlexModifier::Grow.all()]} />
<FlexItem>
<Button variant={ButtonVariant::Danger} onclick={ondelete.clone()}>
{ "Delete Asset" }
</Button>
</FlexItem>
</Flex>
</>
))
} else {
Ok(html!(
format!(
"Asset with id {} not found.",
props.asset_id
)
))
}
}