chore: Seperates files.
This commit is contained in:
parent
e226418ed8
commit
118e4a8f09
4 changed files with 67 additions and 53 deletions
|
@ -12,17 +12,6 @@ pub struct LoginResponse {
|
||||||
pub token: Option<String>
|
pub token: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub struct Backend {
|
|
||||||
pub id: Option<u32>,
|
|
||||||
pub backend: Option<String>,
|
|
||||||
pub name: Option<String>,
|
|
||||||
pub description: Option<String>,
|
|
||||||
pub connectionDetails: Option<String>,
|
|
||||||
|
|
||||||
pub logs: Option<Vec<String>>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NextAPIClient {
|
impl NextAPIClient {
|
||||||
pub fn login(&self, email: &str, password: &str, mut callback: impl 'static + Send + FnMut(LoginResponse)) {
|
pub fn login(&self, email: &str, password: &str, mut callback: impl 'static + Send + FnMut(LoginResponse)) {
|
||||||
let json_data = json!({
|
let json_data = json!({
|
||||||
|
|
46
gui/src/components/log_in.rs
Normal file
46
gui/src/components/log_in.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
use eframe::egui;
|
||||||
|
|
||||||
|
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.heading("Login");
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
match state.token.lock() {
|
||||||
|
Ok(x) => {
|
||||||
|
ui.label(format!("Token: {:?}", *x));
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
ui.label(format!("No token."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
1
gui/src/components/mod.rs
Normal file
1
gui/src/components/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod log_in;
|
|
@ -1,60 +1,38 @@
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
|
|
||||||
|
mod components;
|
||||||
mod api;
|
mod api;
|
||||||
|
|
||||||
|
pub struct ApplicationState {
|
||||||
|
token: Arc<Mutex<String>>,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), eframe::Error> {
|
fn main() -> Result<(), eframe::Error> {
|
||||||
let api = api::new("http://localhost:3000".to_string());
|
let api = api::new("http://localhost:3000".to_string());
|
||||||
|
|
||||||
let options = eframe::NativeOptions {
|
let options = eframe::NativeOptions {
|
||||||
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
|
viewport: egui::ViewportBuilder::default().with_inner_size([1280.0, 720.0]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Our application state:
|
let mut app_state: ApplicationState = ApplicationState {
|
||||||
let mut username: String = "replace@gmail.com".to_owned();
|
token: Arc::new(Mutex::new("".to_string())),
|
||||||
let mut password: String = "replace123".to_owned();
|
|
||||||
|
// /!\ NOT THREAD SAFE FIELDS /!\
|
||||||
|
// These are used internally for each application (immediate mode + functions which are stateless,
|
||||||
|
// and we need *a* state somehow)
|
||||||
|
|
||||||
let token: Arc<Mutex<String>> = Arc::new(Mutex::new("".to_string()));
|
// components/log_in.rs
|
||||||
|
username: "replace@gmail.com".to_owned(),
|
||||||
|
password: "replace123".to_owned()
|
||||||
|
};
|
||||||
|
|
||||||
eframe::run_simple_native("NextNet GUI", options, move |ctx, _frame| {
|
eframe::run_simple_native("NextNet GUI", options, move |ctx, _frame| {
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |_ui| {
|
||||||
ui.heading("Login");
|
components::log_in::main(&mut app_state, &api, ctx);
|
||||||
ui.horizontal(|ui| {
|
|
||||||
ui.label("Email: ");
|
|
||||||
ui.text_edit_singleline(&mut username);
|
|
||||||
});
|
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
|
||||||
let label = ui.label("Password: ");
|
|
||||||
ui.add(egui::TextEdit::singleline(&mut password).password(true))
|
|
||||||
.labelled_by(label.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
if ui.button("Login").clicked() {
|
|
||||||
let token_clone = Arc::clone(&token);
|
|
||||||
api.login(username.as_str(), 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
match token.lock() {
|
|
||||||
Ok(x) => {
|
|
||||||
ui.label(format!("Token: {:?}", *x));
|
|
||||||
},
|
|
||||||
Err(_) => {
|
|
||||||
ui.label(format!("No token."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue