quokka-admin 0.1.0

An admin panel for quokka
Documentation
use std::collections::HashMap;

///
/// Provides a way to add widgets to the admin dashboard by loading a template and populating it with the provided data.
///
/// For more-interactive widgets see the [AdminDashboardWidget::htmx] constructor which allows you to include a widget from any HTML-
/// serving URL using htmx.
///
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct AdminDashboardWidget {
    pub template: String,
    pub data: HashMap<String, serde_json::Value>,
}

impl AdminDashboardWidget {
    pub fn htmx(url: impl ToString) -> Self {
        let mut data = HashMap::new();

        data.insert("hx_get".to_string(), url.to_string().into());
        data.insert("hx_trigger".to_string(), "load".to_string().into());

        Self {
            template: "partials/admin/htmx_widget".to_string(),
            data,
        }
    }

    pub fn new(template: impl ToString) -> Self {
        Self {
            template: template.to_string(),
            ..Default::default()
        }
    }

    pub fn data<S: serde::Serialize>(mut self, name: impl ToString, value: S) -> Self {
        self.data.insert(
            name.to_string(),
            serde_json::to_value(value).unwrap_or_default(),
        );

        self
    }

    pub fn hx_trigger(mut self, trigger: impl ToString) -> Self {
        self.data
            .insert("hx_trigger".to_string(), trigger.to_string().into());

        self
    }
}