login logic in the components

This commit is contained in:
filipriec
2025-03-25 22:56:24 +01:00
parent 1fe139e0c5
commit aab11c1cba
3 changed files with 47 additions and 94 deletions

View File

@@ -1,62 +1,58 @@
// src/components/auth/login.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use crate::config::colors::themes::Theme;
use crate::{
config::colors::themes::Theme,
state::pages::auth::AuthState
};
#[derive(Default)]
pub struct LoginState {
pub return_selected: bool,
}
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 ");
impl LoginState {
pub fn render(&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))
.title(" Login ");
let inner_area = block.inner(area);
f.render_widget(block, area);
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);
// Simple layout with return button
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)
};
let button_style = if self.return_selected {
Style::default()
.fg(theme.highlight)
.add_modifier(ratatui::style::Modifier::BOLD)
} else {
Style::default().fg(theme.fg)
};
let button = Paragraph::new("Return to Intro")
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 self.return_selected {
.border_style(if state.return_selected {
Style::default().fg(theme.accent)
} else {
Style::default().fg(theme.border)
}),
);
f.render_widget(button, chunks[1]);
}
),
chunks[1]
);
}

View File

@@ -1,64 +1,20 @@
// src/state/pages/auth.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
prelude::Widget,
Frame,
};
use crate::config::colors::themes::Theme;
#[derive(Default)]
pub struct AuthState {
pub return_selected: bool,
pub username: String,
pub password: String,
pub error_message: Option<String>,
}
impl AuthState {
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))
.title(" Authentication ");
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);
// Return button
let button_style = if self.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 self.return_selected {
Style::default().fg(theme.accent)
} else {
Style::default().fg(theme.border)
}),
),
chunks[1]
);
pub fn new() -> Self {
Self {
return_selected: false,
username: String::new(),
password: String::new(),
error_message: None,
}
}
}

View File

@@ -8,6 +8,7 @@ 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};
@@ -47,7 +48,7 @@ pub fn render_ui(
// 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 {
auth_state.render(f, main_content_area, theme);
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(