2023-04-28 14:08:53 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use collab_database::fields::TypeOptionData;
|
|
|
|
|
|
|
|
use flowy_error::FlowyResult;
|
|
|
|
|
|
|
|
use crate::entities::FieldType;
|
|
|
|
use crate::services::database::DatabaseEditor;
|
|
|
|
use crate::services::field::{MultiSelectTypeOption, SingleSelectTypeOption};
|
|
|
|
|
|
|
|
pub async fn edit_field_type_option<T: From<TypeOptionData> + Into<TypeOptionData>>(
|
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut T),
|
|
|
|
) -> FlowyResult<()> {
|
|
|
|
let get_type_option = async {
|
|
|
|
let field = editor.get_field(field_id)?;
|
|
|
|
let field_type = FieldType::from(field.field_type);
|
|
|
|
field.get_type_option::<T>(field_type)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(mut type_option) = get_type_option.await {
|
|
|
|
if let Some(old_field) = editor.get_field(field_id) {
|
|
|
|
action(&mut type_option);
|
|
|
|
let type_option_data: TypeOptionData = type_option.into();
|
|
|
|
editor
|
2023-12-29 08:11:26 +08:00
|
|
|
.update_field_type_option(field_id, type_option_data, old_field)
|
2023-04-28 14:08:53 +08:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn edit_single_select_type_option(
|
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut SingleSelectTypeOption),
|
|
|
|
) -> FlowyResult<()> {
|
2023-12-29 08:11:26 +08:00
|
|
|
edit_field_type_option(field_id, editor, action).await
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn edit_multi_select_type_option(
|
|
|
|
field_id: &str,
|
|
|
|
editor: Arc<DatabaseEditor>,
|
|
|
|
action: impl FnOnce(&mut MultiSelectTypeOption),
|
|
|
|
) -> FlowyResult<()> {
|
2023-12-29 08:11:26 +08:00
|
|
|
edit_field_type_option(field_id, editor, action).await
|
2023-04-28 14:08:53 +08:00
|
|
|
}
|