From f1aeeed085239a71d8e04a40c000458c020b95f4 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 25 Dec 2022 10:09:06 +0200 Subject: [PATCH] Thanos did a thing --- frontend/src/fetching.rs | 161 +++++++++++++++++++++++++++++------- frontend/src/main.rs | 2 + frontend/src/shadowrealm.rs | 62 ++++++++++++++ 3 files changed, 197 insertions(+), 28 deletions(-) create mode 100644 frontend/src/shadowrealm.rs diff --git a/frontend/src/fetching.rs b/frontend/src/fetching.rs index 505f925..117a500 100644 --- a/frontend/src/fetching.rs +++ b/frontend/src/fetching.rs @@ -1,3 +1,4 @@ +use gloo::{console::log, utils::format::JsValueSerdeExt}; use std::{ error::Error, fmt::{self, Debug, Display, Formatter}, @@ -47,61 +48,65 @@ impl UseAble for CommonAdmin { } //Wwasm bingdengen code -const DRIVER_URL: &str = "http://libkyy.cf:48590/api/driver/1234"; +const DRIVER_URL: &str = "http://db.sewelam.tech/api/driver/1234"; +const FUNNY_TXT_URL: &str = "https://whatthecommit.com/index.txt"; const INCORRECT_URL: &str = "http://libkyy.cf"; - -enum Msg { - SetDriverFetchState(FetchState), - GetDriver, +//Generic Msg for our states +enum Msg { + SetDataFetchState(FetchState), + GetData, GetError, } -struct Driver { - driver: FetchState, +struct RandomCommit { + commit: FetchState, } -impl Component for Driver { - type Message = Msg; +//This trait is for all yew components +impl Component for RandomCommit { + type Message = Msg; type Properties = (); fn create(_ctx: &Context) -> Self { Self { - driver: FetchState::NotFetching, + commit: FetchState::NotFetching, //Default state is not fetching anything } } + fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { - Msg::SetDriverFetchState(fetch_state) => { - self.driver = fetch_state; + Msg::SetDataFetchState(fetch_state) => { + self.commit = fetch_state; true } - Msg::GetDriver => { + Msg::GetData => { 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)), + match fetch_commit(FUNNY_TXT_URL).await { + Ok(commit) => Msg::SetDataFetchState(FetchState::Success(commit)), + Err(err) => Msg::SetDataFetchState(FetchState::Failed(err)), } }); ctx.link() - .send_message(Msg::SetDriverFetchState(FetchState::Fetching)); + .send_message(Msg::SetDataFetchState(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)), + match fetch_commit(INCORRECT_URL).await { + Ok(commit) => Msg::SetDataFetchState(FetchState::Success(commit)), + Err(err) => Msg::SetDataFetchState(FetchState::Failed(err)), } }); ctx.link() - .send_message(Msg::SetDriverFetchState(FetchState::Fetching)); + .send_message(Msg::SetDataFetchState(FetchState::Fetching)); false } } } + fn view(&self, ctx: &Context) -> Html { - match &self.driver { + match &self.commit { FetchState::NotFetching => html! { <> - + + + }, + FetchState::Fetching => html! { "Fetching" }, + FetchState::Success(data) => match data.clone().use_it() { + Ok(html) => html, + Err(err) => html! { err }, + }, + FetchState::Failed(err) => html! { err }, + } + } +} #[derive(Debug, Clone, PartialEq)] pub struct FetchError { @@ -125,6 +196,7 @@ impl Display for FetchError { Debug::fmt(&self.err, f) } } + impl Error for FetchError {} impl From for FetchError { @@ -132,6 +204,7 @@ impl From for FetchError { Self { err: value } } } +//Enum for Fetchstates that takes a generic type --> String/Driver etc. pub enum FetchState { NotFetching, Fetching, @@ -139,22 +212,54 @@ pub enum FetchState { Failed(FetchError), } -//my code here poopy -async fn fetch_driver(url: &'static str) -> Result { +//Working example function +async fn fetch_commit(url: &'static str) -> Result { + //initialise request let mut opts = RequestInit::new(); opts.method("GET"); - opts.mode(RequestMode::Cors); + opts.mode(RequestMode::Cors); //Cors is required for fetch to work let request = Request::new_with_str_and_init(url, &opts)?; let window = gloo::utils::window(); + //get response value let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; let resp: Response = resp_value.dyn_into().unwrap(); + //parse response value as text let text = JsFuture::from(resp.text()?).await?; Ok(text.as_string().unwrap()) } -//render obama + +async fn fetch_driver(url: &'static str) -> Result { + let mut opts = RequestInit::new(); + opts.method("GET"); + //Available request modes: Cors, NoCors, SameOrigin + opts.mode(RequestMode::NoCors); //NoCors because cors simply doesn't work here + + let request = Request::new_with_str_and_init(url, &opts)?; + //api header for json + request.headers().set("Accept", "application/json")?; + + let window = web_sys::window().unwrap(); + let resp_value = JsFuture::from(window.fetch_with_request(&request)).await?; + assert!(resp_value.is_instance_of::()); + let resp: Response = resp_value.dyn_into().unwrap(); + + //parsing Json response + let fetched_json = JsFuture::from(resp.json()?).await?; + log!(fetched_json.clone()); + let fetched_json: JsValue = fetched_json.into(); + //parsing into CommonDriver struct + let fetched_json: CommonDriver = fetched_json.into_serde().unwrap(); + Ok(fetched_json) +} + +//renders components above +pub fn render_commit() { + yew::Renderer::::new().render(); +} + pub fn render_driver() { yew::Renderer::::new().render(); } diff --git a/frontend/src/main.rs b/frontend/src/main.rs index 415a094..1af79f8 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -65,5 +65,7 @@ struct Stdnt { // } fn main() { + yew::Renderer::::new().render(); + fetching::render_commit(); fetching::render_driver(); } diff --git a/frontend/src/shadowrealm.rs b/frontend/src/shadowrealm.rs new file mode 100644 index 0000000..fe98712 --- /dev/null +++ b/frontend/src/shadowrealm.rs @@ -0,0 +1,62 @@ +// impl Component for Driver { +// type Message = Msg; +// type Properties = (); +// fn create(_ctx: &Context) -> Self { +// Self { +// driver: FetchState::NotFetching, //Default state is not fetching anything +// } +// } +// +// fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { +// match msg { +// Msg::SetDataFetchState(fetch_state) => { +// self.driver = fetch_state; +// true +// } +// Msg::GetData => { +// ctx.link().send_future(async { +// match fetch_driver(DRIVER_URL).await { +// Ok(driver) => Msg::SetDataFetchState(FetchState::Success(driver)), +// Err(err) => Msg::SetDataFetchState(FetchState::Failed(err)), +// } +// }); +// ctx.link() +// .send_message(Msg::SetDataFetchState(FetchState::Fetching)); +// false +// } +// Msg::GetError => { +// ctx.link().send_future(async { +// match fetch_driver(INCORRECT_URL).await { +// Ok(driver) => Msg::SetDataFetchState(FetchState::Success(driver)), +// Err(err) => Msg::SetDataFetchState(FetchState::Failed(err)), +// } +// }); +// ctx.link() +// .send_message(Msg::SetDataFetchState(FetchState::Fetching)); +// false +// } +// } +// } +// +// fn view(&self, ctx: &Context) -> Html { +// //render the component into HTML +// match &self.driver { +// FetchState::NotFetching => html! { +// <> +// +// +// +// }, +// FetchState::Fetching => html! { "Fetching" }, +// FetchState::Success(data) => match data.clone().use_it() { +// Ok(html) => html, +// Err(err) => html! { err }, +// }, +// FetchState::Failed(err) => html! { err }, +// } +// } +// }