chore: moving gui to separate repository
This commit is contained in:
parent
8c4179209d
commit
d57a2a4a1d
5 changed files with 0 additions and 149 deletions
|
@ -1,13 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "nextnet-gui"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
eframe = "0.27.2"
|
|
||||||
egui = "0.27.2"
|
|
||||||
ehttp = { version = "0.5.0", features = ["json"] }
|
|
||||||
serde = { version = "1.0.199", features = ["derive"] }
|
|
||||||
serde_json = "1.0.116"
|
|
|
@ -1,50 +0,0 @@
|
||||||
use ehttp::{fetch, Request};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
pub struct NextAPIClient {
|
|
||||||
pub url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Default)]
|
|
||||||
pub struct LoginResponse {
|
|
||||||
pub error: Option<String>,
|
|
||||||
pub token: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NextAPIClient {
|
|
||||||
pub fn login(
|
|
||||||
&self,
|
|
||||||
email: &str,
|
|
||||||
password: &str,
|
|
||||||
mut callback: impl 'static + Send + FnMut(LoginResponse),
|
|
||||||
) {
|
|
||||||
let json_data = json!({
|
|
||||||
"email": email,
|
|
||||||
"password": password
|
|
||||||
});
|
|
||||||
|
|
||||||
let json_str_raw: String = json_data.to_string();
|
|
||||||
let json_str: &str = json_str_raw.as_str();
|
|
||||||
|
|
||||||
println!("{}", json_str);
|
|
||||||
|
|
||||||
let mut request = Request::post(
|
|
||||||
self.url.clone() + "/api/v1/users/login",
|
|
||||||
json_str.as_bytes().to_vec(),
|
|
||||||
);
|
|
||||||
request.headers.insert("Content-Type", "application/json");
|
|
||||||
|
|
||||||
fetch(request, move |result: ehttp::Result<ehttp::Response>| {
|
|
||||||
let res = result.unwrap();
|
|
||||||
let json: LoginResponse = res.json().unwrap();
|
|
||||||
callback(json);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(url: String) -> NextAPIClient {
|
|
||||||
let api_client: NextAPIClient = NextAPIClient { url };
|
|
||||||
|
|
||||||
return api_client;
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
use eframe::egui;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::api;
|
|
||||||
use crate::ApplicationState;
|
|
||||||
|
|
||||||
pub fn main(state: &mut ApplicationState, api: &api::NextAPIClient, ctx: &eframe::egui::Context) {
|
|
||||||
egui::Window::new("Log In").show(ctx, move |ui| {
|
|
||||||
ui.set_max_width(275.0);
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
|
||||||
ui.label("Email: ");
|
|
||||||
ui.text_edit_singleline(&mut state.username);
|
|
||||||
});
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
|
||||||
let label = ui.label("Password: ");
|
|
||||||
ui.add(egui::TextEdit::singleline(&mut state.password).password(true))
|
|
||||||
.labelled_by(label.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
if ui.button("Login").clicked() {
|
|
||||||
let token_clone = Arc::clone(&state.token);
|
|
||||||
api.login(
|
|
||||||
state.username.as_str(),
|
|
||||||
state.password.as_str(),
|
|
||||||
Box::new(move |res: api::LoginResponse| match res.token {
|
|
||||||
Some(x) => {
|
|
||||||
let mut token = token_clone.lock().unwrap();
|
|
||||||
*token = x;
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let mut token = token_clone.lock().unwrap();
|
|
||||||
*token = "".to_string();
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
pub mod log_in;
|
|
|
@ -1,45 +0,0 @@
|
||||||
use eframe::egui;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
mod api;
|
|
||||||
mod components;
|
|
||||||
|
|
||||||
pub struct ApplicationState {
|
|
||||||
token: Arc<Mutex<String>>,
|
|
||||||
username: String,
|
|
||||||
password: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), eframe::Error> {
|
|
||||||
let api = api::new("http://localhost:3000".to_string());
|
|
||||||
|
|
||||||
let options = eframe::NativeOptions {
|
|
||||||
viewport: egui::ViewportBuilder::default().with_inner_size([1280.0, 720.0]),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut app_state: ApplicationState = ApplicationState {
|
|
||||||
token: Arc::new(Mutex::new("".to_string())),
|
|
||||||
|
|
||||||
// /!\ NOT THREAD SAFE FIELDS /!\
|
|
||||||
// These are used internally for each application (immediate mode + functions which are stateless,
|
|
||||||
// and we need *a* state somehow)
|
|
||||||
|
|
||||||
// components/log_in.rs
|
|
||||||
username: "replace@gmail.com".to_owned(),
|
|
||||||
password: "replace123".to_owned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
eframe::run_simple_native("NextNet GUI", options, move |ctx, _frame| {
|
|
||||||
egui::CentralPanel::default().show(ctx, |_ui| {
|
|
||||||
let token_clone = Arc::clone(&app_state.token);
|
|
||||||
let token = token_clone.lock().unwrap();
|
|
||||||
|
|
||||||
if *token == "".to_string() {
|
|
||||||
components::log_in::main(&mut app_state, &api, ctx);
|
|
||||||
} else {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue