role restricted admin panel
This commit is contained in:
@@ -1,24 +1,25 @@
|
|||||||
// src/components/admin/admin_panel.rs
|
// src/components/admin/admin_panel.rs
|
||||||
|
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::state::pages::admin::AdminState; // Import the persistent state
|
use crate::state::pages::auth::AuthState;
|
||||||
|
use crate::state::pages::admin::AdminState;
|
||||||
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::Style, // Added Modifier
|
style::Style,
|
||||||
text::{Line, Span, Text},
|
text::{Line, Span, Text},
|
||||||
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
|
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Renamed from AdminPanelState::render and made a standalone function
|
|
||||||
pub fn render_admin_panel(
|
pub fn render_admin_panel(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
admin_state: &AdminState, // Accept the persistent state (immutable borrow is enough for rendering)
|
auth_state: &AuthState,
|
||||||
|
admin_state: &AdminState,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
profile_tree: &ProfileTreeResponse,
|
profile_tree: &ProfileTreeResponse,
|
||||||
selected_profile: &Option<String>, // The globally selected profile for the checkmark
|
selected_profile: &Option<String>,
|
||||||
) {
|
) {
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
@@ -45,14 +46,14 @@ pub fn render_admin_panel(
|
|||||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||||
.split(chunks[1]);
|
.split(chunks[1]);
|
||||||
|
|
||||||
|
if auth_state.role.as_deref() != Some("admin") {
|
||||||
// Profile list - Use data from admin_state
|
// Profile list - Use data from admin_state
|
||||||
let items: Vec<ListItem> = admin_state
|
let items: Vec<ListItem> = admin_state
|
||||||
.profiles // Use profiles from the persistent state
|
.profiles
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| {
|
.map(|p| {
|
||||||
ListItem::new(Line::from(vec![
|
ListItem::new(Line::from(vec![
|
||||||
Span::styled(
|
Span::styled(
|
||||||
// Check against the globally selected profile for the checkmark
|
|
||||||
if Some(p) == selected_profile.as_ref() { "✓ " } else { " " },
|
if Some(p) == selected_profile.as_ref() { "✓ " } else { " " },
|
||||||
Style::default().fg(theme.accent),
|
Style::default().fg(theme.accent),
|
||||||
),
|
),
|
||||||
@@ -65,15 +66,12 @@ pub fn render_admin_panel(
|
|||||||
.block(Block::default().title("Profiles"))
|
.block(Block::default().title("Profiles"))
|
||||||
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
|
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
|
||||||
|
|
||||||
// Render statefully using a CLONE of the list_state from persistent AdminState
|
|
||||||
// Cloning is necessary because render_stateful_widget needs `&mut ListState`
|
|
||||||
// but we only have `&AdminState`. This is a common pattern in Ratatui.
|
|
||||||
let mut list_state_clone = admin_state.list_state.clone();
|
let mut list_state_clone = admin_state.list_state.clone();
|
||||||
f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone);
|
f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone);
|
||||||
|
|
||||||
// Profile details - Use selection info from admin_state
|
// Profile details - Use selection info from admin_state
|
||||||
if let Some(profile) = admin_state
|
if let Some(profile) = admin_state
|
||||||
.get_selected_index() // Use the method from persistent state
|
.get_selected_index()
|
||||||
.and_then(|i| profile_tree.profiles.get(i))
|
.and_then(|i| profile_tree.profiles.get(i))
|
||||||
{
|
{
|
||||||
let mut text = Text::default();
|
let mut text = Text::default();
|
||||||
@@ -101,8 +99,14 @@ pub fn render_admin_panel(
|
|||||||
|
|
||||||
let details_widget = Paragraph::new(text)
|
let details_widget = Paragraph::new(text)
|
||||||
.block(Block::default().title("Details"))
|
.block(Block::default().title("Details"))
|
||||||
.wrap(Wrap { trim: true }); // Add wrapping
|
.wrap(Wrap { trim: true });
|
||||||
f.render_widget(details_widget, content_chunks[1]);
|
f.render_widget(details_widget, content_chunks[1]);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Admin-specific view
|
||||||
|
let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.")
|
||||||
|
.style(Style::default().fg(theme.fg))
|
||||||
|
.alignment(Alignment::Center);
|
||||||
|
f.render_widget(admin_message, content_chunks[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ pub fn render_ui(
|
|||||||
} else if app_state.ui.show_admin {
|
} else if app_state.ui.show_admin {
|
||||||
crate::components::admin::admin_panel::render_admin_panel(
|
crate::components::admin::admin_panel::render_admin_panel(
|
||||||
f,
|
f,
|
||||||
|
auth_state,
|
||||||
admin_state,
|
admin_state,
|
||||||
main_content_area,
|
main_content_area,
|
||||||
theme,
|
theme,
|
||||||
|
|||||||
Reference in New Issue
Block a user