♻️ (admin): Reduce pagetop-admin a los ajustes
El panel pasa al núcleo y pagetop-admin se queda con los ajustes guardados en la base de datos: DeclareConfigForms, ConfigForm y la tabla settings. Un campo obligatorio vacío ya no se guarda, y un fallo al guardar se muestra con los mensajes del núcleo.
This commit is contained in:
parent
ab2677be44
commit
824dc4a3ab
15 changed files with 392 additions and 1244 deletions
|
|
@ -1,150 +0,0 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
use crate::LOCALES_ADMIN;
|
||||
use crate::registry;
|
||||
|
||||
/// Componente de layout principal de una página del panel de administración.
|
||||
///
|
||||
/// Renderiza breadcrumb, encabezado (título + acciones locales), tareas locales (pestañas) y el
|
||||
/// contenido de la página. No incluye ningún menú de navegación: mostrar las secciones del panel
|
||||
/// (el "sidebar" o el "menú superior", según el tema) es responsabilidad del tema que intercepte
|
||||
/// [`CoreTemplates::Admin`](pagetop::core::theme::CoreTemplates::Admin) (por ejemplo
|
||||
/// `pagetop-bootsier`) leyendo directamente [`crate::registry::global()`]. El tema básico de
|
||||
/// PageTop no lo hace, y una página de administración sigue siendo completamente navegable sin él,
|
||||
/// a través del *dashboard* (`/admin`), las páginas de sección y este mismo breadcrumb.
|
||||
///
|
||||
/// ```html
|
||||
/// <nav class="admin-breadcrumb">...</nav>
|
||||
/// <header class="admin-header">
|
||||
/// <h1 class="admin-page-title">Title</h1>
|
||||
/// <ul class="admin-actions-list">...</ul>
|
||||
/// </header>
|
||||
/// <nav class="admin-local-tasks">...</nav>
|
||||
/// <div class="admin-content">...</div>
|
||||
/// ```
|
||||
///
|
||||
/// El contenido son sus propios hijos ([`Children`]), igual que en cualquier otro componente
|
||||
/// contenedor (p. ej. [`Block`](pagetop::base::component::Block)): admite tantos `with_child(...)`
|
||||
/// como haga falta, cada uno renderizado en el ciclo normal de componentes.
|
||||
#[derive(AutoDefault, Clone, Debug, Getters)]
|
||||
pub struct AdminFrame {
|
||||
/// Devuelve el título de la página.
|
||||
title: Lc,
|
||||
/// Devuelve la lista de componentes hijo de la página.
|
||||
children: Children,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Component for AdminFrame {
|
||||
fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let path = cx
|
||||
.request()
|
||||
.map(|r| r.path().to_string())
|
||||
.unwrap_or_else(|| "/".to_string());
|
||||
|
||||
let breadcrumbs = render_breadcrumbs(cx, &path).await;
|
||||
let local_tasks = render_local_tasks(cx, &path);
|
||||
let local_acts = render_local_actions(cx, &path);
|
||||
let body = self.children().render(cx).await;
|
||||
|
||||
Ok(html! {
|
||||
(breadcrumbs)
|
||||
header class="admin-header" {
|
||||
h1 class="admin-page-title" { (self.title().using(cx)) }
|
||||
(local_acts)
|
||||
}
|
||||
@if !local_tasks.0.is_empty() {
|
||||
nav class="admin-local-tasks" { (local_tasks) }
|
||||
}
|
||||
div class="admin-content" {
|
||||
(body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[builder_impl]
|
||||
impl AdminFrame {
|
||||
/// Establece el título de la página.
|
||||
pub fn with_title(mut self, title: Lc) -> Self {
|
||||
self.title = title;
|
||||
self
|
||||
}
|
||||
|
||||
/// Añade un componente hijo al contenido de la página.
|
||||
pub fn with_child(mut self, op: impl Into<ChildOp>) -> Self {
|
||||
self.children.alter_child(op.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// **< Breadcrumbs >********************************************************************************
|
||||
|
||||
async fn render_breadcrumbs(cx: &mut Context, current_path: &str) -> Markup {
|
||||
let reg = registry::global();
|
||||
let base = crate::ADMIN_BASE_PATH;
|
||||
|
||||
let home_label = Lc::t("admin-breadcrumb-home", &LOCALES_ADMIN);
|
||||
let mut bc = Breadcrumb::new().with_crumb(breadcrumb::Crumb::new(home_label, base));
|
||||
|
||||
if let Some(page) = reg.pages().get(current_path) {
|
||||
if let Some(section) = reg.sections().get(&page.section) {
|
||||
bc = bc.with_crumb(breadcrumb::Crumb::new(
|
||||
section.title.clone(),
|
||||
section.path.as_str(),
|
||||
));
|
||||
}
|
||||
bc = bc.with_crumb(breadcrumb::Crumb::current(page.title.clone()));
|
||||
} else if let Some(section) = reg.sections().values().find(|s| s.path == current_path) {
|
||||
// La ruta actual es la propia sección (su página de aterrizaje), no una `AdminPage`
|
||||
// registrada dentro de ella.
|
||||
bc = bc.with_crumb(breadcrumb::Crumb::current(section.title.clone()));
|
||||
}
|
||||
|
||||
bc.render(cx).await
|
||||
}
|
||||
|
||||
// **< Tareas locales >*****************************************************************************
|
||||
|
||||
fn render_local_tasks(cx: &Context, current_path: &str) -> Markup {
|
||||
let reg = registry::global();
|
||||
let tasks = reg.tasks_for(current_path);
|
||||
if tasks.is_empty() {
|
||||
return html! {};
|
||||
}
|
||||
html! {
|
||||
ul class="admin-tasks-list" {
|
||||
@for task in tasks {
|
||||
@let is_active = current_path == task.path;
|
||||
li class=(if is_active { "admin-task admin-task-active" } else { "admin-task" }) {
|
||||
a href=(cx.route(task.path.as_str())) { (task.title.using(cx)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **< Acciones locales >***************************************************************************
|
||||
|
||||
fn render_local_actions(cx: &Context, current_path: &str) -> Markup {
|
||||
let reg = registry::global();
|
||||
let actions = reg.actions_for(current_path);
|
||||
if actions.is_empty() {
|
||||
return html! {};
|
||||
}
|
||||
html! {
|
||||
ul class="admin-actions-list" {
|
||||
@for action in actions {
|
||||
li class="admin-action" {
|
||||
a class="admin-action-link" href=(cx.route(action.url.as_str())) {
|
||||
(action.title.using(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
use crate::registry;
|
||||
|
||||
/// Componente que renderiza la barra de navegación del panel de administración.
|
||||
///
|
||||
/// Construye la [`Navbar`] en cada petición a partir de [`registry::admin_navbar()`], ya filtrada
|
||||
/// por el usuario de la petición actual. Pensado para que un tema la registre en su propia región
|
||||
/// de navegación e intercepte `Navbar`/`Nav`/`Dropdown` en
|
||||
/// [`Theme::render_component()`](pagetop::core::theme::Theme::render_component) si quiere darle un
|
||||
/// aspecto propio (p. ej. `pagetop-bootsier` la reconoce automáticamente al ser componentes del
|
||||
/// núcleo); sin intercepción, se renderiza con el marcado por defecto de cada componente.
|
||||
///
|
||||
/// Sólo se renderiza en páginas creadas con
|
||||
/// [`Page::admin()`](pagetop::response::Page::admin) (plantilla
|
||||
/// [`CoreTemplates::Admin`](pagetop::core::theme::CoreTemplates::Admin)). Es necesario comprobarlo
|
||||
/// explícitamente porque, si se registra en una región de propósito general como
|
||||
/// [`CoreRegions::Aside`](pagetop::core::theme::CoreRegions::Aside), se renderizaría también en
|
||||
/// páginas `Standard` si no se autolimitara.
|
||||
#[derive(AutoDefault, Clone, Debug)]
|
||||
pub struct AdminMenu;
|
||||
|
||||
#[async_trait]
|
||||
impl Component for AdminMenu {
|
||||
fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
if !matches!(
|
||||
cx.template().downcast_ref::<CoreTemplates>(),
|
||||
Some(CoreTemplates::Admin)
|
||||
) {
|
||||
return Ok(html! {});
|
||||
}
|
||||
Ok(registry::admin_navbar(cx).render(cx).await)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,35 +3,27 @@ use pagetop::prelude::*;
|
|||
use crate::LOCALES_ADMIN;
|
||||
use crate::settings::{SettingFieldType, SettingsSchema, get_or};
|
||||
|
||||
/// Componente que renderiza un formulario de configuración persistente.
|
||||
/// Formulario de un [`SettingsSchema`], con los valores guardados en la tabla `settings`.
|
||||
///
|
||||
/// Genera campos HTML a partir de un [`SettingsSchema`] y carga los valores
|
||||
/// actuales desde `settings`. El POST es procesado por el handler interno
|
||||
/// `config_form_post`.
|
||||
/// En las páginas declaradas con [`DeclareConfigForms`](crate::action::DeclareConfigForms),
|
||||
/// `pagetop-admin` lo muestra y guarda lo que se envía. En una página propia sólo lo muestra: el
|
||||
/// envío llega a la ruta de la página, o a la de [`with_action_path()`](Self::with_action_path), y
|
||||
/// lo guarda su *handler* (ver [`settings::set()`](crate::settings::set)).
|
||||
///
|
||||
/// # Ejemplo
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use pagetop::prelude::*;
|
||||
/// use pagetop_admin::component::{AdminFrame, ConfigForm};
|
||||
/// use pagetop_admin::component::ConfigForm;
|
||||
/// use pagetop_admin::settings::{SettingField, SettingsSchema};
|
||||
///
|
||||
/// async fn settings_handler(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
||||
/// let schema = SettingsSchema::new("myapp.general")
|
||||
/// .with_field(SettingField::text("site_name", "Site name").with_required(true));
|
||||
///
|
||||
/// let title = "General settings";
|
||||
/// let mut form = ConfigForm::with_schema(schema);
|
||||
/// let mut cx = Context::new(request.clone());
|
||||
/// let content = form.render(&mut cx).await;
|
||||
/// .with_field(SettingField::text("site_name", Lc::n("Site name")).with_required(true));
|
||||
///
|
||||
/// Page::admin(request)
|
||||
/// .with_title(Lc::n(title))
|
||||
/// .with_child(
|
||||
/// AdminFrame::new()
|
||||
/// .with_title(Lc::n(title))
|
||||
/// .with_child(Html::with(move |_| content.clone())),
|
||||
/// )
|
||||
/// .with_title(Lc::n("General settings"))
|
||||
/// .with_child(ConfigForm::with_schema(schema))
|
||||
/// .render()
|
||||
/// .await
|
||||
/// }
|
||||
|
|
@ -42,10 +34,6 @@ pub struct ConfigForm {
|
|||
schema: Option<SettingsSchema>,
|
||||
/// Devuelve la ruta de destino del formulario, si se ha personalizado.
|
||||
action_path: Option<Route>,
|
||||
/// Devuelve si el último envío se guardó correctamente.
|
||||
saved: bool,
|
||||
/// Devuelve si el último envío produjo un error al guardar.
|
||||
error: bool,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
|
@ -64,22 +52,11 @@ impl Component for ConfigForm {
|
|||
None => cx.route(cx.request().map(|r| r.path()).unwrap_or("/")),
|
||||
};
|
||||
|
||||
let save_label = Lc::t("config-save-label", &LOCALES_ADMIN)
|
||||
.lookup(cx)
|
||||
.unwrap_or_else(|| "Save configuration".into());
|
||||
|
||||
let saved_msg = Lc::t("config-saved-ok", &LOCALES_ADMIN)
|
||||
.lookup(cx)
|
||||
.unwrap_or_else(|| "Configuration saved.".into());
|
||||
|
||||
let error_msg = Lc::t("config-saved-error", &LOCALES_ADMIN)
|
||||
.lookup(cx)
|
||||
.unwrap_or_else(|| "Could not save configuration.".into());
|
||||
|
||||
// Pre-computa los valores de BD para cada campo antes del macro html! (que es síncrono).
|
||||
// Pre-computa los valores de BD para cada campo antes del macro html! (que es síncrono). El
|
||||
// formulario guarda todos los valores como texto (`"42"`, `"true"`), así que se leen como
|
||||
// texto también los de los campos numéricos y las casillas.
|
||||
struct FieldVals {
|
||||
raw_val: String,
|
||||
num_val: String,
|
||||
checked: bool,
|
||||
}
|
||||
let mut prepared: Vec<FieldVals> = Vec::with_capacity(schema.fields().len());
|
||||
|
|
@ -87,27 +64,18 @@ impl Component for ConfigForm {
|
|||
let key = schema.key_for(field.name());
|
||||
let raw_val =
|
||||
get_or::<String>(&key, field.default_value().cloned().unwrap_or_default()).await;
|
||||
let num_val = get_or::<f64>(&key, 0.0).await.to_string();
|
||||
let checked = get_or::<bool>(&key, false).await;
|
||||
prepared.push(FieldVals {
|
||||
raw_val,
|
||||
num_val,
|
||||
checked,
|
||||
});
|
||||
let checked = raw_val == "true";
|
||||
prepared.push(FieldVals { raw_val, checked });
|
||||
}
|
||||
|
||||
Ok(html! {
|
||||
@if *self.saved() {
|
||||
div class="admin-message admin-message-ok" { (&saved_msg) }
|
||||
}
|
||||
@if *self.error() {
|
||||
div class="admin-message admin-message-error" { (&error_msg) }
|
||||
}
|
||||
form class="admin-config-form" method="post" action=(action) {
|
||||
@for (i, field) in schema.fields().iter().enumerate() {
|
||||
@let pf = &prepared[i];
|
||||
div class="admin-form-field" {
|
||||
label class="admin-form-label" for=(field.name()) { (field.label()) }
|
||||
label class="admin-form-label" for=(field.name()) {
|
||||
(field.label().using(cx))
|
||||
}
|
||||
@match field.field_type() {
|
||||
SettingFieldType::Text { max_length } => {
|
||||
@if let Some(max) = max_length {
|
||||
|
|
@ -133,7 +101,7 @@ impl Component for ConfigForm {
|
|||
type="number"
|
||||
id=(field.name())
|
||||
name=(field.name())
|
||||
value=(&pf.num_val)
|
||||
value=(&pf.raw_val)
|
||||
min=(lo)
|
||||
max=(hi)
|
||||
required[*field.required()];
|
||||
|
|
@ -142,7 +110,7 @@ impl Component for ConfigForm {
|
|||
type="number"
|
||||
id=(field.name())
|
||||
name=(field.name())
|
||||
value=(&pf.num_val)
|
||||
value=(&pf.raw_val)
|
||||
min=(lo)
|
||||
required[*field.required()];
|
||||
} @else if let Some(hi) = max {
|
||||
|
|
@ -150,7 +118,7 @@ impl Component for ConfigForm {
|
|||
type="number"
|
||||
id=(field.name())
|
||||
name=(field.name())
|
||||
value=(&pf.num_val)
|
||||
value=(&pf.raw_val)
|
||||
max=(hi)
|
||||
required[*field.required()];
|
||||
} @else {
|
||||
|
|
@ -158,7 +126,7 @@ impl Component for ConfigForm {
|
|||
type="number"
|
||||
id=(field.name())
|
||||
name=(field.name())
|
||||
value=(&pf.num_val)
|
||||
value=(&pf.raw_val)
|
||||
required[*field.required()];
|
||||
}
|
||||
}
|
||||
|
|
@ -178,22 +146,22 @@ impl Component for ConfigForm {
|
|||
{
|
||||
@for (val, label) in options {
|
||||
@if &pf.raw_val == val {
|
||||
option value=(val) selected { (label) }
|
||||
option value=(val) selected { (label.using(cx)) }
|
||||
} @else {
|
||||
option value=(val) { (label) }
|
||||
option value=(val) { (label.using(cx)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@if let Some(help) = field.help_text() {
|
||||
small class="admin-form-help" { (help) }
|
||||
@if !field.help_text().is_none() {
|
||||
small class="admin-form-help" { (field.help_text().using(cx)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
div class="admin-form-actions" {
|
||||
button class="admin-btn admin-btn-primary" type="submit" {
|
||||
(&save_label)
|
||||
(Lc::t("config-save-label", &LOCALES_ADMIN).using(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -212,17 +180,10 @@ impl ConfigForm {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_action_path(mut self, v: impl Into<Option<Route>>) -> Self {
|
||||
if let Some(v) = v.into() {
|
||||
self.action_path = Some(v);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
#[builder_skip]
|
||||
pub(crate) fn with_saved(mut self, saved: bool, error: bool) -> Self {
|
||||
self.saved = saved;
|
||||
self.error = error;
|
||||
/// Establece la ruta a la que se envía el formulario, o `None` para enviarlo a la de la
|
||||
/// página.
|
||||
pub fn with_action_path(mut self, path: impl Into<Option<Route>>) -> Self {
|
||||
self.action_path = path.into();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue