use super::{LoggedInApplication, ToggleTheme};
use patternfly_yew::prelude::*;
use yew::prelude::*;
use yew_oauth2::openid::use_auth_agent;
use yew_oauth2::prelude::*;
#[derive(Properties, PartialEq, Clone)]
pub struct OauthInnerApplicationProps {
pub management_url: String,
pub participant_id: String,
pub api_key: Option<String>,
pub onlogout: Callback<()>,
}
#[component]
pub fn OauthInnerApplication(props: &OauthInnerApplicationProps) -> Html {
let agent = use_auth_agent().expect("Must be nested inside an OAuth2 component");
let login = use_callback(agent.clone(), |_, agent| {
let _ = agent.start_login();
});
let auth = use_context::<OAuth2Context>();
if let Some(OAuth2Context::Authenticated(_)) = auth {
log::warn!("Authenticated");
} else {
if web_sys::window().unwrap().location().pathname().unwrap() != "/" {
web_sys::window().unwrap().location().assign("/").unwrap();
}
}
let logout = use_callback((agent, props.onlogout.clone()), |_, (agent, onlogout)| {
let _ = agent.logout();
onlogout.emit(());
});
html!(
<>
<Failure>
<FailureMessage />
</Failure>
<Authenticated>
<LoggedInApplication
management_url={props.management_url.clone()}
participant_id={props.participant_id.clone()}
api_key={props.api_key.clone()}
onlogout={logout}
/>
</Authenticated>
<NotAuthenticated>
<Background
style="logo.png"
additional_style="background-size: 200px 200px; background-position: calc(100vw - 220px) calc(100vh - 220px); background-color: var(--pf-t--global--background--color--secondary--default);"
/>
<Login>
<Card>
<CardHeader>
<Title level={Level::H3} size={Size::XXLarge}>{ "EDC Connector Management" }</Title>
</CardHeader>
<CardBody>
<Button variant={ButtonVariant::Primary} label="Login" onclick={login} />
</CardBody>
<CardFooter>
<Bullseye>
<ToggleTheme />
</Bullseye>
</CardFooter>
</Card>
</Login>
</NotAuthenticated>
</>
)
}