a state of no errors
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
fmt::{self, Debug, Display, Formatter},
|
||||
};
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
|
||||
use common::{CommonAdmin, CommonDriver};
|
||||
use yew::{html, Html};
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use web_sys::{Request, RequestInit, RequestMode, Response};
|
||||
use yew::{html, Component, Context, Html};
|
||||
//Trait to handle Future yew HTMLS
|
||||
pub trait UseAble {
|
||||
//Return HTML if didn't fail return error if not
|
||||
@@ -38,5 +46,111 @@ impl UseAble for CommonAdmin {
|
||||
}
|
||||
}
|
||||
|
||||
//Wwasm bingdengen code
|
||||
const DRIVER_URL: &str = "http://libkyy.cf:48590/api/driver/1234";
|
||||
const INCORRECT_URL: &str = "http://libkyy.cf";
|
||||
|
||||
enum Msg {
|
||||
SetDriverFetchState(FetchState<String>),
|
||||
GetDriver,
|
||||
GetError,
|
||||
}
|
||||
struct Driver {
|
||||
driver: FetchState<String>,
|
||||
}
|
||||
impl Component for Driver {
|
||||
type Message = Msg;
|
||||
type Properties = ();
|
||||
fn create(_ctx: &Context<Self>) -> Self {
|
||||
Self {
|
||||
driver: FetchState::NotFetching,
|
||||
}
|
||||
}
|
||||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||
match msg {
|
||||
Msg::SetDriverFetchState(fetch_state) => {
|
||||
self.driver = fetch_state;
|
||||
true
|
||||
}
|
||||
Msg::GetDriver => {
|
||||
ctx.link().send_future(async {
|
||||
match fetch_driver(DRIVER_URL).await {
|
||||
Ok(driver) => Msg::SetDriverFetchState(FetchState::Success(driver)),
|
||||
Err(err) => Msg::SetDriverFetchState(FetchState::Failed(err)),
|
||||
}
|
||||
});
|
||||
ctx.link()
|
||||
.send_message(Msg::SetDriverFetchState(FetchState::Fetching));
|
||||
false
|
||||
}
|
||||
Msg::GetError => {
|
||||
ctx.link().send_future(async {
|
||||
match fetch_driver(INCORRECT_URL).await {
|
||||
Ok(driver) => Msg::SetDriverFetchState(FetchState::Success(driver)),
|
||||
Err(err) => Msg::SetDriverFetchState(FetchState::Failed(err)),
|
||||
}
|
||||
});
|
||||
ctx.link()
|
||||
.send_message(Msg::SetDriverFetchState(FetchState::Fetching));
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||
match &self.driver {
|
||||
FetchState::NotFetching => html! {
|
||||
<>
|
||||
<button onclick={ctx.link().callback(|_| Msg::GetDriver)}>
|
||||
{ "Get Driver" }
|
||||
</button>
|
||||
<button onclick={ctx.link().callback(|_| Msg::GetError)}>
|
||||
{ "Get using incorrect URL" }
|
||||
</button>
|
||||
</>
|
||||
},
|
||||
FetchState::Fetching => html! { "Fetching" },
|
||||
FetchState::Success(data) => html! { <p> {data} </p> },
|
||||
FetchState::Failed(err) => html! { err },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FetchError {
|
||||
err: JsValue,
|
||||
}
|
||||
|
||||
impl Display for FetchError {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
Debug::fmt(&self.err, f)
|
||||
}
|
||||
}
|
||||
impl Error for FetchError {}
|
||||
|
||||
impl From<JsValue> for FetchError {
|
||||
fn from(value: JsValue) -> Self {
|
||||
Self { err: value }
|
||||
}
|
||||
}
|
||||
pub enum FetchState<T> {
|
||||
NotFetching,
|
||||
Fetching,
|
||||
Success(T),
|
||||
Failed(FetchError),
|
||||
}
|
||||
|
||||
//my code here poopy
|
||||
async fn fetch_driver(url: &'static str) -> Result<String, FetchError> {
|
||||
let mut opts = RequestInit::new();
|
||||
opts.method("GET");
|
||||
opts.mode(RequestMode::Cors);
|
||||
|
||||
let request = Request::new_with_str_and_init(url, &opts)?;
|
||||
|
||||
let window = gloo::utils::window();
|
||||
let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?;
|
||||
let resp: Response = resp_value.dyn_into().unwrap();
|
||||
|
||||
let text = JsFuture::from(resp.text()?).await?;
|
||||
Ok(text.as_string().unwrap())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user