From 77002200a325792de15f6754b993ebd528a2dfb2 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 24 Dec 2022 20:16:36 +0200 Subject: [PATCH] Before I nuke my life --- Cargo.lock | 4 +- common/src/lib.rs | 1 - frontend/Cargo.toml | 17 +++++++- frontend/src/fetching.rs | 42 ++++++++++++++++++ frontend/src/main.rs | 94 +++++++++++++++++++++++----------------- src/main.rs | 4 +- 6 files changed, 116 insertions(+), 46 deletions(-) create mode 100644 frontend/src/fetching.rs diff --git a/Cargo.lock b/Cargo.lock index b5de244..7bb48e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,7 +505,9 @@ name = "db-frontend" version = "0.1.0" dependencies = [ "common", - "diesel", + "gloo-net", + "serde", + "serde_json", "yew", ] diff --git a/common/src/lib.rs b/common/src/lib.rs index 7899bf3..c6b8fb9 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -23,4 +23,3 @@ pub struct CommonDriver { pub name: String, pub address: String, } - diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 18ac030..d62c410 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -8,4 +8,19 @@ edition = "2021" [dependencies] yew = { version = "0.20.0", features = ["csr"] } common = { path = "../common"} -diesel = { version = "2.0.0",features = ["postgres","r2d2"] } +gloo-net = {version = "0.2"} +serde = { version = "1.0.126", features = ["derive"] } +serde_json = "1.0.64" +wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" + +[dependencies.web-sys] +version = "0.3" +features = [ + "Headers", + "Request", + "RequestInit", + "RequestMode", + "Response", + "Window", +] diff --git a/frontend/src/fetching.rs b/frontend/src/fetching.rs new file mode 100644 index 0000000..48f5270 --- /dev/null +++ b/frontend/src/fetching.rs @@ -0,0 +1,42 @@ +use common::{CommonAdmin, CommonDriver}; +use yew::{html, Html}; +//Trait to handle Future yew HTMLS +pub trait UseAble { + //Return HTML if didn't fail return error if not + fn use_it(self) -> Result; +} +impl UseAble for CommonDriver { + fn use_it(self) -> Result { + let name = self.name; + let email = self.address; + let id = self.id; + let html = html! { +
+

{ "Driver:" }

+

{ format!("Name: {}", name) }

+

{ format!("Email: {}", email) }

+

{ format!("ID: {}", id) }

+
+ }; + Ok(html) + } +} +impl UseAble for CommonAdmin { + fn use_it(self) -> Result { + let name = self.name; + let email = self.address; + let id = self.id; + let html = html! { +
+

{ "Admin:" }

+

{ format!("Name: {}", name) }

+

{ format!("Email: {}", email) }

+

{ format!("ID: {}", id) }

+
+ }; + Ok(html) + } +} + + + diff --git a/frontend/src/main.rs b/frontend/src/main.rs index d693b6a..0872b21 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,51 +1,53 @@ +mod fetching; +use common::CommonDriver; +use fetching::PrinteAble; +use gloo_net::http::Request; use yew::prelude::*; -use common::CommonTicket; + +use crate::fetching::UseAble; + #[function_component] fn App() -> Html { - let counter = use_state(|| 0); - let onclick = { - let counter = counter.clone(); - move |_| { - let value = *counter + 1; - counter.set(value); - } - }; - -let stdnts = vec! [ - Stdnt { - id: 1, - name: "Hamada".to_string(), - email: "Hamada@Gmail.com".to_string(), - gpa: 3.4 - }, - Stdnt { - id: 2, - name: "Gamal".to_string(), - email: "Gamal@Gmail.com".to_string(), - gpa: 4.0 - }, - Stdnt { - id: 3, - name: "Joe".to_string(), - email: "joe@proton.mail".to_string(), - gpa: 3.8 - } -]; -let stdnts_comp = stdnts.iter().map(|stdnt| html! { - <> -

{format!("Name: {}", stdnt.name)}

-

{format!("Email: {}", stdnt.email)}

-

{format!("GPA: {}",stdnt.gpa)}

-
- -}).collect::(); + let stdnts = vec![ + Stdnt { + id: 1, + name: "Hamada".to_string(), + email: "Hamada@Gmail.com".to_string(), + gpa: 3.4, + }, + Stdnt { + id: 2, + name: "Gamal".to_string(), + email: "Gamal@Gmail.com".to_string(), + gpa: 4.0, + }, + Stdnt { + id: 3, + name: "Joe".to_string(), + email: "joe@proton.mail".to_string(), + gpa: 3.8, + }, + ]; + let driver_comp = fetch_person(); + let stdnts_comp = stdnts + .iter() + .map(|stdnt| { + html! { + <> +

{format!("Name: {}", stdnt.name)}

+

{format!("Email: {}", stdnt.email)}

+

{format!("GPA: {}",stdnt.gpa)}

+
+ + } + }) + .collect::(); html! {
- -

{ *counter }

{ "Students:" }

{ stdnts_comp }

+ //

{ driver_comp }

} } @@ -55,6 +57,18 @@ struct Stdnt { gpa: f32, email: String, } +//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; +} fn main() { yew::Renderer::::new().render(); diff --git a/src/main.rs b/src/main.rs index ae9bc52..c67eca7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,7 @@ use actix_web::{ use backend::{ admin_data::get_admin, driver_data::get_driver, establish_connection, ticket_data::get_ticket, }; -use common::CommonAdmin; -use common::CommonDriver; -use common::CommonTicket; +use common::{CommonAdmin, CommonDriver, CommonTicket}; // //Admin Services // #[get("/api/admin/{id}")]