Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9564bd8524 | ||
|
|
ad64df2ec3 | ||
|
|
aab11c1cba | ||
|
|
1fe139e0c5 |
@@ -1,111 +1,58 @@
|
||||
// src/components/login/login.rs
|
||||
// src/components/auth/login.rs
|
||||
|
||||
use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
|
||||
style::Style,
|
||||
text::{Line, Span, Text},
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::Style,
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::{
|
||||
config::colors::themes::Theme,
|
||||
state::pages::auth::AuthState
|
||||
};
|
||||
|
||||
pub struct LoginState {
|
||||
pub fields: Vec<String>,
|
||||
pub values: Vec<String>,
|
||||
pub selected_field: usize,
|
||||
}
|
||||
|
||||
impl LoginState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fields: vec!["Username".to_string(), "Password".to_string()],
|
||||
values: vec![String::new(), String::new()],
|
||||
selected_field: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_field(&mut self) {
|
||||
self.selected_field = (self.selected_field + 1) % self.fields.len();
|
||||
}
|
||||
|
||||
pub fn previous_field(&mut self) {
|
||||
self.selected_field = if self.selected_field == 0 {
|
||||
self.fields.len() - 1
|
||||
} else {
|
||||
self.selected_field - 1
|
||||
};
|
||||
}
|
||||
|
||||
pub fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
let inner_area = block.inner(area);
|
||||
f.render_widget(block, area);
|
||||
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(5),
|
||||
Constraint::Min(1),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Title
|
||||
let title = Line::from(Span::styled("Login", Style::default().fg(theme.highlight)));
|
||||
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
|
||||
f.render_widget(title_widget, chunks[0]);
|
||||
|
||||
// Login form
|
||||
let form_chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(3),
|
||||
])
|
||||
.split(chunks[1]);
|
||||
|
||||
// Username field
|
||||
let username_block = Block::default()
|
||||
.title("Username")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 0 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let username = Paragraph::new(self.values[0].as_str())
|
||||
.block(username_block);
|
||||
f.render_widget(username, form_chunks[0]);
|
||||
|
||||
// Password field
|
||||
let password_block = Block::default()
|
||||
.title("Password")
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 1 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let password = Paragraph::new("*".repeat(self.values[1].len()))
|
||||
.block(password_block);
|
||||
f.render_widget(password, form_chunks[1]);
|
||||
|
||||
// Submit button
|
||||
let submit_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_style(if self.selected_field == 2 {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
});
|
||||
let submit = Paragraph::new("Submit")
|
||||
.block(submit_block)
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(submit, form_chunks[2]);
|
||||
}
|
||||
pub fn render_login(f: &mut Frame, area: Rect, theme: &Theme, state: &mut AuthState) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg))
|
||||
.title(" Login ");
|
||||
|
||||
let inner_area = block.inner(area);
|
||||
f.render_widget(block, area);
|
||||
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage(40),
|
||||
Constraint::Length(3),
|
||||
Constraint::Percentage(40),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
let button_style = if state.return_selected {
|
||||
Style::default()
|
||||
.fg(theme.highlight)
|
||||
.add_modifier(ratatui::style::Modifier::BOLD)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
};
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new("Return to Intro")
|
||||
.style(button_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
.border_style(if state.return_selected {
|
||||
Style::default().fg(theme.accent)
|
||||
} else {
|
||||
Style::default().fg(theme.border)
|
||||
}),
|
||||
),
|
||||
chunks[1]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -122,12 +122,10 @@ impl IntroState {
|
||||
pub fn handle_selection(&self, app_state: &mut crate::state::state::AppState) {
|
||||
match self.selected_option {
|
||||
0 => { /* Continue logic */ }
|
||||
1 => { /* Admin logic */ }
|
||||
2 => {
|
||||
app_state.ui.show_intro = false;
|
||||
app_state.ui.show_login = true;
|
||||
}
|
||||
1 => {}
|
||||
2 => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -124,14 +124,23 @@ pub fn previous_option(app_state: &mut AppState) {
|
||||
pub fn select(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
// Handle selection in intro screen
|
||||
if app_state.ui.intro_state.selected_option == 0 {
|
||||
// First option selected - show form
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
} else {
|
||||
// Second option selected - show admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
match app_state.ui.intro_state.selected_option {
|
||||
0 => { // Continue - show form
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = false;
|
||||
}
|
||||
1 => { // Admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
app_state.ui.show_login = false;
|
||||
}
|
||||
2 => { // Login
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = true;
|
||||
}
|
||||
_ => {} // Other options (shouldn't happen)
|
||||
}
|
||||
app_state.ui.show_intro = false;
|
||||
} else if app_state.ui.show_admin {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// src/state/pages.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod auth;
|
||||
|
||||
20
client/src/state/pages/auth.rs
Normal file
20
client/src/state/pages/auth.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
// src/state/pages/auth.rs
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AuthState {
|
||||
pub return_selected: bool,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
return_selected: false,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
error_message: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,16 +8,19 @@ use crate::components::{
|
||||
form::form::render_form,
|
||||
intro::{intro},
|
||||
admin::{admin_panel::AdminPanelState},
|
||||
auth::login::render_login,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::{Constraint, Direction, Layout};
|
||||
use ratatui::Frame;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
total_count: u64,
|
||||
@@ -44,6 +47,8 @@ pub fn render_ui(
|
||||
if app_state.ui.show_intro {
|
||||
// Use app_state's intro_state directly
|
||||
app_state.ui.intro_state.render(f, main_content_area, theme);
|
||||
}else if app_state.ui.show_login {
|
||||
render_login(f, main_content_area, theme, auth_state);
|
||||
} else if app_state.ui.show_admin {
|
||||
// Create temporary AdminPanelState for rendering
|
||||
let mut admin_state = AdminPanelState::new(
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::config::colors::themes::Theme;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::state::AppState;
|
||||
use crate::components::admin::{admin_panel::AdminPanelState};
|
||||
@@ -20,6 +21,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut intro_state = IntroState::new();
|
||||
let mut auth_state = AuthState::default();
|
||||
|
||||
// Initialize app_state first
|
||||
let mut app_state = AppState::new()?;
|
||||
@@ -28,13 +30,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let profile_tree = grpc_client.get_profile_tree().await?;
|
||||
app_state.profile_tree = profile_tree;
|
||||
|
||||
// Now create admin panel with profiles from app_state
|
||||
if intro_state.selected_option == 1 {
|
||||
app_state.ui.show_admin = true;
|
||||
app_state.general.selected_item = 0;
|
||||
app_state.general.current_option = 0;
|
||||
}
|
||||
|
||||
// Fetch table structure at startup (one-time)
|
||||
let table_structure = grpc_client.get_table_structure().await?;
|
||||
|
||||
@@ -66,6 +61,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&mut auth_state,
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
app_state.total_count,
|
||||
|
||||
Reference in New Issue
Block a user