101 lines
2.8 KiB
Rust
101 lines
2.8 KiB
Rust
use graphql_client::reqwest::post_graphql;
|
|
use yew::prelude::*;
|
|
|
|
use crate::graphql;
|
|
|
|
pub enum Msg {
|
|
DoneFetching {
|
|
errors: Option<Vec<String>>,
|
|
data: graphql::queries::me::me::ResponseData,
|
|
},
|
|
}
|
|
|
|
#[derive(Properties, PartialEq)]
|
|
pub struct HomeProps {
|
|
pub token: String,
|
|
}
|
|
|
|
enum State {
|
|
Fetching,
|
|
Done,
|
|
}
|
|
|
|
pub struct Home {
|
|
state: State,
|
|
errors: Option<Vec<String>>,
|
|
data: Option<graphql::queries::me::me::ResponseData>,
|
|
}
|
|
|
|
impl Component for Home {
|
|
type Message = Msg;
|
|
type Properties = HomeProps;
|
|
|
|
fn create(_ctx: &Context<Self>) -> Self {
|
|
Self {
|
|
state: State::Fetching,
|
|
errors: None,
|
|
data: None,
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::DoneFetching { errors, data } => {
|
|
self.state = State::Done;
|
|
self.errors = errors;
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
match self.state {
|
|
State::Fetching => {
|
|
let mut headers = reqwest::header::HeaderMap::new();
|
|
headers.insert(
|
|
"Authorization",
|
|
reqwest::header::HeaderValue::from_str(&format!(
|
|
"Bearer {}",
|
|
ctx.props().token
|
|
))
|
|
.unwrap(),
|
|
);
|
|
ctx.link().send_future(async move {
|
|
let response = post_graphql::<graphql::queries::me::Me, _>(
|
|
&reqwest::Client::builder()
|
|
.default_headers(headers)
|
|
.build()
|
|
.unwrap(),
|
|
graphql::URL.as_str(),
|
|
graphql::queries::me::me::Variables,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
log::debug!("{:?}", response.data);
|
|
|
|
Msg::DoneFetching {
|
|
errors: response
|
|
.errors
|
|
.map(|x| x.iter().map(|e| e.message.to_owned()).collect()),
|
|
data: response.data.unwrap(),
|
|
}
|
|
});
|
|
|
|
html! {
|
|
<p>{ "Fetching..." }</p>
|
|
}
|
|
}
|
|
State::Done => html! {
|
|
if let Some(errors) = &self.errors {
|
|
<p>{ "Errors:" }</p>
|
|
<div>
|
|
{ for errors.iter().map(|e| html! { <p style="color: red;">{ e }</p> }) }
|
|
</div>
|
|
} else {
|
|
<h1>{ "Hey, !" }</h1>
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|