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>
|
||||
}
|
||||
|
||||
#[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 {
|
||||
pub fn login(&self, email: &str, password: &str, mut callback: impl 'static + Send + FnMut(LoginResponse)) {
|
||||
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 eframe::egui;
|
||||
|
||||
mod components;
|
||||
mod api;
|
||||
|
||||
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([320.0, 240.0]),
|
||||
viewport: egui::ViewportBuilder::default().with_inner_size([1280.0, 720.0]),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Our application state:
|
||||
let mut username: String = "replace@gmail.com".to_owned();
|
||||
let mut password: String = "replace123".to_owned();
|
||||
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)
|
||||
|
||||
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| {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("Login");
|
||||
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."));
|
||||
}
|
||||
}
|
||||
egui::CentralPanel::default().show(ctx, |_ui| {
|
||||
components::log_in::main(&mut app_state, &api, ctx);
|
||||
});
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue