use std::collections::BTreeMap;
use std::future::Future;
use std::sync::Arc;
use futures_util::future::BoxFuture;
use laravel::http::Request;
use laravel::model::Model;
use crate::protocol::{ActionConfirmationDto, ActionResponse, ActionSchemaDto};
use crate::schema::form::Field;
#[derive(Debug, Clone)]
pub struct ActionConfirmation {
pub title: String,
pub description: String,
pub confirm_label: String,
}
impl ActionConfirmation {
#[must_use]
pub fn new(title: impl Into<String>, description: impl Into<String>) -> Self {
Self {
title: title.into(),
description: description.into(),
confirm_label: "Confirm".to_owned(),
}
}
#[must_use]
pub fn confirm_label(mut self, label: impl Into<String>) -> Self {
self.confirm_label = label.into();
self
}
pub(crate) fn to_dto(&self) -> ActionConfirmationDto {
ActionConfirmationDto {
title: self.title.clone(),
description: self.description.clone(),
confirm_label: self.confirm_label.clone(),
}
}
}
#[derive(Debug, Clone)]
pub struct ActionContext {
request: Arc<Request>,
data: Arc<BTreeMap<String, serde_json::Value>>,
}
impl ActionContext {
pub(crate) fn new(request: &Arc<Request>, data: BTreeMap<String, serde_json::Value>) -> Self {
Self {
request: Arc::clone(request),
data: Arc::new(data),
}
}
#[must_use]
pub fn request(&self) -> &Request {
&self.request
}
#[must_use]
pub fn data(&self) -> &BTreeMap<String, serde_json::Value> {
&self.data
}
#[must_use]
pub fn string(&self, key: &str) -> Option<&str> {
self.data.get(key).and_then(serde_json::Value::as_str)
}
#[must_use]
pub fn bool(&self, key: &str) -> Option<bool> {
self.data.get(key).and_then(serde_json::Value::as_bool)
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
self.data.get(key)
}
}
#[derive(Debug, Clone)]
pub struct ActionOutcome {
pub message: String,
pub affected: u64,
pub redirect_to: Option<String>,
pub refresh: bool,
}
impl ActionOutcome {
#[must_use]
pub fn message(message: impl Into<String>) -> Self {
Self {
message: message.into(),
affected: 1,
redirect_to: None,
refresh: true,
}
}
#[must_use]
pub fn affected(mut self, count: u64) -> Self {
self.affected = count;
self
}
#[must_use]
pub fn redirect(mut self, path: impl Into<String>) -> Self {
self.redirect_to = Some(path.into());
self
}
pub(crate) fn into_response(self) -> ActionResponse {
ActionResponse {
message: self.message,
affected: self.affected,
redirect_to: self.redirect_to,
refresh: self.refresh,
}
}
}
type Handler<T> = Arc<dyn Fn(ActionContext, T) -> BoxFuture<'static, laravel::Result<ActionOutcome>> + Send + Sync>;
type Availability<M> = Arc<dyn Fn(&M) -> bool + Send + Sync>;
pub struct RowAction<M: Model> {
pub(crate) name: String,
pub(crate) label: String,
pub(crate) icon: Option<String>,
pub(crate) destructive: bool,
pub(crate) confirmation: Option<ActionConfirmation>,
pub(crate) fields: Vec<Field>,
pub(crate) handler: Handler<M>,
pub(crate) available: Option<Availability<M>>,
}
pub struct BulkAction<M: Model> {
pub(crate) name: String,
pub(crate) label: String,
pub(crate) icon: Option<String>,
pub(crate) destructive: bool,
pub(crate) confirmation: Option<ActionConfirmation>,
pub(crate) fields: Vec<Field>,
pub(crate) handler: Handler<Vec<M>>,
}
macro_rules! action {
($name:ident, $target:ty, $new_doc:literal $(, $field:ident: $value:expr)?) => {
impl<M: Model> $name<M> {
#[doc = $new_doc]
#[must_use]
pub fn new<F, Fut>(name: impl Into<String>, label: impl Into<String>, handler: F) -> Self
where
F: Fn(ActionContext, $target) -> Fut + Send + Sync + 'static,
Fut: Future<Output = laravel::Result<ActionOutcome>> + Send + 'static,
{
Self {
name: name.into(),
label: label.into(),
icon: None,
destructive: false,
confirmation: None,
fields: Vec::new(),
handler: Arc::new(move |context, target| Box::pin(handler(context, target))),
$($field: $value,)?
}
}
#[must_use]
pub fn icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
#[must_use]
pub fn destructive(mut self) -> Self {
self.destructive = true;
self
}
#[must_use]
pub fn confirm(mut self, title: impl Into<String>, description: impl Into<String>) -> Self {
self.confirmation = Some(ActionConfirmation::new(title, description));
self
}
#[must_use]
pub fn confirmation(mut self, confirmation: ActionConfirmation) -> Self {
self.confirmation = Some(confirmation);
self
}
#[must_use]
pub fn field(mut self, field: Field) -> Self {
self.fields.push(field);
self
}
pub(crate) fn to_dto(&self) -> ActionSchemaDto {
ActionSchemaDto {
name: self.name.clone(),
label: self.label.clone(),
icon: self.icon.clone(),
destructive: self.destructive,
confirmation: self.confirmation.as_ref().map(ActionConfirmation::to_dto),
fields: self.fields.iter().map(Field::to_dto).collect(),
}
}
}
};
}
action!(
RowAction,
M,
"An action whose handler receives the record it runs on.",
available: None
);
action!(
BulkAction,
Vec<M>,
"An action whose handler receives every selected record."
);
impl<M: Model> RowAction<M> {
#[must_use]
pub fn available_when(mut self, available: impl Fn(&M) -> bool + Send + Sync + 'static) -> Self {
self.available = Some(Arc::new(available));
self
}
pub(crate) fn is_available(&self, record: &M) -> bool {
self.available.as_ref().is_none_or(|available| available(record))
}
}