Before I nuke my life

This commit is contained in:
LinlyBoi
2022-12-24 20:16:36 +02:00
parent 367d778ec1
commit 77002200a3
6 changed files with 116 additions and 46 deletions

42
frontend/src/fetching.rs Normal file
View File

@@ -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<Html, String>;
}
impl UseAble for CommonDriver {
fn use_it(self) -> Result<Html, String> {
let name = self.name;
let email = self.address;
let id = self.id;
let html = html! {
<div>
<h2> { "Driver:" } </h2>
<p> { format!("Name: {}", name) } </p>
<p> { format!("Email: {}", email) } </p>
<p> { format!("ID: {}", id) } </p>
</div>
};
Ok(html)
}
}
impl UseAble for CommonAdmin {
fn use_it(self) -> Result<Html, String> {
let name = self.name;
let email = self.address;
let id = self.id;
let html = html! {
<div>
<h2> { "Admin:" } </h2>
<p> { format!("Name: {}", name) } </p>
<p> { format!("Email: {}", email) } </p>
<p> { format!("ID: {}", id) } </p>
</div>
};
Ok(html)
}
}

View File

@@ -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! {
<>
<p key={stdnt.id}>{format!("Name: {}", stdnt.name)} </p>
<p key={stdnt.id}>{format!("Email: {}", stdnt.email)} </p>
<p key={stdnt.id}>{format!("GPA: {}",stdnt.gpa)} </p>
<br/>
</>
}).collect::<Html>();
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! {
<>
<p key={stdnt.id}>{format!("Name: {}", stdnt.name)} </p>
<p key={stdnt.id}>{format!("Email: {}", stdnt.email)} </p>
<p key={stdnt.id}>{format!("GPA: {}",stdnt.gpa)} </p>
<br/>
</>
}
})
.collect::<Html>();
html! {
<div>
<button {onclick}>{ "+1" }</button>
<p>{ *counter }</p>
<h2> { "Students:" } </h2>
<p> { stdnts_comp } </p>
// <p> { driver_comp } </p>
</div>
}
}
@@ -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::<App>::new().render();