From d01292626110faff37db806591ddaf55953690a5 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 24 Dec 2022 21:06:13 +0200 Subject: [PATCH] a state of no errors --- Cargo.lock | 4 ++ frontend/Cargo.toml | 1 + frontend/src/fetching.rs | 116 ++++++++++++++++++++++++++++++++++++++- frontend/src/main.rs | 24 ++++---- 4 files changed, 130 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7bb48e3..8cc4d24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,9 +505,13 @@ name = "db-frontend" version = "0.1.0" dependencies = [ "common", + "gloo", "gloo-net", "serde", "serde_json", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", "yew", ] diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index d62c410..0b64a31 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" yew = { version = "0.20.0", features = ["csr"] } common = { path = "../common"} gloo-net = {version = "0.2"} +gloo = "0.8" serde = { version = "1.0.126", features = ["derive"] } serde_json = "1.0.64" wasm-bindgen = "0.2" diff --git a/frontend/src/fetching.rs b/frontend/src/fetching.rs index 48f5270..9cdc830 100644 --- a/frontend/src/fetching.rs +++ b/frontend/src/fetching.rs @@ -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), + GetDriver, + GetError, +} +struct Driver { + driver: FetchState, +} +impl Component for Driver { + type Message = Msg; + type Properties = (); + fn create(_ctx: &Context) -> Self { + Self { + driver: FetchState::NotFetching, + } + } + fn update(&mut self, ctx: &Context, 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) -> Html { + match &self.driver { + FetchState::NotFetching => html! { + <> + + + + }, + FetchState::Fetching => html! { "Fetching" }, + FetchState::Success(data) => html! {

{data}

}, + 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 for FetchError { + fn from(value: JsValue) -> Self { + Self { err: value } + } +} +pub enum FetchState { + NotFetching, + Fetching, + Success(T), + Failed(FetchError), +} + +//my code here poopy +async fn fetch_driver(url: &'static str) -> Result { + 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()) +} diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 0872b21..14f399f 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,11 +1,8 @@ mod fetching; use common::CommonDriver; -use fetching::PrinteAble; use gloo_net::http::Request; use yew::prelude::*; -use crate::fetching::UseAble; - #[function_component] fn App() -> Html { let stdnts = vec![ @@ -28,7 +25,6 @@ fn App() -> Html { gpa: 3.8, }, ]; - let driver_comp = fetch_person(); let stdnts_comp = stdnts .iter() .map(|stdnt| { @@ -59,16 +55,16 @@ struct Stdnt { } //testing this rn //Component that returns HTML from json fetched from Api yes :D -async fn fetch_person() -> Html { - let resp = Request::get("http://libkyy.cf/api/driver/1234") - .send() - .await - .unwrap(); - assert_eq!(resp.status(), 200); - let driver: CommonDriver = resp.json().await.unwrap(); - let result = driver.use_it(); - return result; -} +// async fn fetch_person() -> Html { +// let resp = Request::get("http://libkyy.cf/api/driver/1234") +// .send() +// .await +// .unwrap(); +// assert_eq!(resp.status(), 200); +// let driver: CommonDriver = resp.json().await.unwrap(); +// let result = driver.use_it(); +// return result; +// } fn main() { yew::Renderer::::new().render();