Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix: select option logic (#7893)

Richard Shiueb048407b4e62
diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/mod.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/mod.rs
index 8a4d4e40c9..93ff3866a6 100644
--- a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/mod.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/mod.rs
@@ -1,5 +1,6 @@
mod multi_select_type_option;
mod select_filter;
+mod select_option_tests;
mod select_type_option;
mod single_select_type_option;
mod type_option_transform;
diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/multi_select_type_option.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/multi_select_type_option.rs
index 5f1cbeb5d8..63356d9a04 100644
--- a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/multi_select_type_option.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/multi_select_type_option.rs
@@ -13,7 +13,6 @@ use collab_database::fields::TypeOptionData;
use collab_database::rows::Cell;
use flowy_error::FlowyResult;
-use collab_database::template::util::ToCellString;
use std::cmp::Ordering;
impl TypeOption for MultiSelectTypeOption {
@@ -56,35 +55,25 @@ impl CellDataChangeset for MultiSelectTypeOption {
changeset: <Self as TypeOption>::CellChangeset,
cell: Option<Cell>,
) -> FlowyResult<(Cell, <Self as TypeOption>::CellData)> {
- let insert_option_ids = changeset
+ let mut existing_ids: Vec<_> = cell
+ .map(|cell| SelectOptionIds::from(&cell))
+ .unwrap_or_default()
+ .into_inner()
+ .into_iter()
+ .filter(|id| !changeset.delete_option_ids.contains(id))
+ .collect();
+
+ let valid_inserted_ids: Vec<_> = changeset
.insert_option_ids
.into_iter()
- .filter(|insert_option_id| {
- self
- .options
- .iter()
- .any(|option| &option.id == insert_option_id)
- })
- .collect::<Vec<String>>();
-
- let select_option_ids = match cell {
- None => SelectOptionIds::from(insert_option_ids),
- Some(cell) => {
- let mut select_ids = SelectOptionIds::from(&cell);
- for insert_option_id in insert_option_ids {
- if !select_ids.contains(&insert_option_id) {
- select_ids.push(insert_option_id);
- }
- }
+ .filter(|id| self.options.iter().any(|option| &option.id == id))
+ .filter(|id| !existing_ids.iter().any(|existing_id| existing_id == id))
+ .collect();
- for delete_option_id in changeset.delete_option_ids {
- select_ids.retain(|id| id != &delete_option_id);
- }
+ existing_ids.extend(valid_inserted_ids);
+
+ let select_option_ids: SelectOptionIds = existing_ids.into();
- tracing::trace!("Multi-select cell data: {}", select_ids.to_cell_string());
- select_ids
- },
- };
Ok((
select_option_ids.to_cell(FieldType::MultiSelect),
select_option_ids,
@@ -146,96 +135,3 @@ impl TypeOptionCellDataCompare for MultiSelectTypeOption {
}
}
}
-
-#[cfg(test)]
-mod tests {
- use crate::services::cell::CellDataChangeset;
- use crate::services::field::type_options::selection_type_option::*;
- use collab_database::fields::select_type_option::{
- MultiSelectTypeOption, SelectOption, SelectOptionIds, SelectTypeOption,
- };
- use collab_database::template::util::ToCellString;
-
- #[test]
- fn multi_select_insert_multi_option_test() {
- let google = SelectOption::new("Google");
- let facebook = SelectOption::new("Facebook");
- let multi_select = MultiSelectTypeOption(SelectTypeOption {
- options: vec![google.clone(), facebook.clone()],
- disable_color: false,
- });
-
- let option_ids = vec![google.id, facebook.id];
- let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
- let select_option_ids: SelectOptionIds =
- multi_select.apply_changeset(changeset, None).unwrap().1;
-
- assert_eq!(&*select_option_ids, &option_ids);
- }
-
- #[test]
- fn multi_select_unselect_multi_option_test() {
- let google = SelectOption::new("Google");
- let facebook = SelectOption::new("Facebook");
- let multi_select = MultiSelectTypeOption(SelectTypeOption {
- options: vec![google.clone(), facebook.clone()],
- disable_color: false,
- });
- let option_ids = vec![google.id, facebook.id];
-
- // insert
- let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
- let select_option_ids = multi_select.apply_changeset(changeset, None).unwrap().1;
- assert_eq!(&*select_option_ids, &option_ids);
-
- // delete
- let changeset = SelectOptionCellChangeset::from_delete_options(option_ids);
- let select_option_ids = multi_select.apply_changeset(changeset, None).unwrap().1;
- assert!(select_option_ids.is_empty());
- }
-
- #[test]
- fn multi_select_insert_single_option_test() {
- let google = SelectOption::new("Google");
- let multi_select = MultiSelectTypeOption(SelectTypeOption {
- options: vec![google.clone()],
- disable_color: false,
- });
-
- let changeset = SelectOptionCellChangeset::from_insert_option_id(&google.id);
- let select_option_ids = multi_select.apply_changeset(changeset, None).unwrap().1;
- assert_eq!(select_option_ids.to_cell_string(), google.id);
- }
-
- #[test]
- fn multi_select_insert_non_exist_option_test() {
- let google = SelectOption::new("Google");
- let multi_select = MultiSelectTypeOption(SelectTypeOption {
- options: vec![],
- disable_color: false,
- });
-
- let changeset = SelectOptionCellChangeset::from_insert_option_id(&google.id);
- let (_, select_option_ids) = multi_select.apply_changeset(changeset, None).unwrap();
- assert!(select_option_ids.is_empty());
- }
-
- #[test]
- fn multi_select_insert_invalid_option_id_test() {
- let google = SelectOption::new("Google");
- let multi_select = MultiSelectTypeOption(SelectTypeOption {
- options: vec![google],
- disable_color: false,
- });
-
- // empty option id string
- let changeset = SelectOptionCellChangeset::from_insert_option_id("");
- let (cell, _) = multi_select.apply_changeset(changeset, None).unwrap();
- let option_ids = SelectOptionIds::from(&cell);
- assert!(option_ids.is_empty());
-
- let changeset = SelectOptionCellChangeset::from_insert_option_id("123,456");
- let select_option_ids = multi_select.apply_changeset(changeset, None).unwrap().1;
- assert!(select_option_ids.is_empty());
- }
-}
diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/select_option_tests.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/select_option_tests.rs
new file mode 100644
index 0000000000..af7ec6a34e
--- /dev/null
+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/select_option_tests.rs
@@ -0,0 +1,183 @@
+#[cfg(test)]
+mod tests {
+ use crate::services::{cell::CellDataChangeset, field::SelectOptionCellChangeset};
+
+ use collab_database::{
+ entity::FieldType,
+ fields::select_type_option::{
+ MultiSelectTypeOption, SelectOption, SelectOptionIds, SelectTypeOption,
+ SingleSelectTypeOption,
+ },
+ };
+
+ #[test]
+ fn empty_select_type_option_test() {
+ let type_option = SingleSelectTypeOption::default();
+ let google = SelectOption::new("Google");
+
+ let (_, cell_data) = type_option
+ .apply_changeset(
+ SelectOptionCellChangeset::from_insert_option_id(&google.id),
+ None,
+ )
+ .unwrap();
+ assert_eq!(cell_data.into_inner(), Vec::<String>::new());
+
+ let (_, cell_data) = type_option
+ .apply_changeset(
+ SelectOptionCellChangeset::from_delete_option_id(&google.id),
+ None,
+ )
+ .unwrap();
+ assert_eq!(cell_data.into_inner(), Vec::<String>::new());
+
+ let type_option = MultiSelectTypeOption::default();
+
+ let (_, cell_data) = type_option
+ .apply_changeset(
+ SelectOptionCellChangeset::from_insert_option_id(&google.id),
+ None,
+ )
+ .unwrap();
+ assert_eq!(cell_data.into_inner(), Vec::<String>::new());
+
+ let (_, cell_data) = type_option
+ .apply_changeset(
+ SelectOptionCellChangeset::from_delete_option_id(&google.id),
+ None,
+ )
+ .unwrap();
+ assert_eq!(cell_data.into_inner(), Vec::<String>::new());
+ }
+
+ #[test]
+ fn single_select_insert_option_test() {
+ let google = SelectOption::new("Google");
+ let facebook = SelectOption::new("Facebook");
+ let type_option = SelectTypeOption {
+ options: vec![google.clone(), facebook.clone()],
+ disable_color: false,
+ };
+ let single_select_type_option = SingleSelectTypeOption(type_option);
+
+ // insert a single option
+ let changeset = SelectOptionCellChangeset::from_insert_options(vec![google.id.clone()]);
+ let (_, select_option_ids) = single_select_type_option
+ .apply_changeset(changeset, None)
+ .unwrap();
+ assert_eq!(select_option_ids.into_inner(), vec![google.id.clone()]);
+
+ // insert multiple options, including an invalid one
+ let changeset = SelectOptionCellChangeset::from_insert_options(vec![
+ google.id.clone(),
+ facebook.id.clone(),
+ "".to_string(),
+ ]);
+ let (_, select_option_ids) = single_select_type_option
+ .apply_changeset(changeset, None)
+ .unwrap();
+ assert_eq!(select_option_ids.into_inner(), vec![google.id.clone()]);
+
+ // insert an option when one is already selected
+ let cell = SelectOptionIds::from(vec![google.id]).to_cell(FieldType::SingleSelect);
+ let changeset = SelectOptionCellChangeset::from_insert_options(vec![facebook.id.clone()]);
+ let (_, select_option_ids) = single_select_type_option
+ .apply_changeset(changeset, Some(cell))
+ .unwrap();
+ assert_eq!(select_option_ids.into_inner(), vec![facebook.id]);
+ }
+
+ #[test]
+ fn multi_select_insert_option_test() {
+ let amazon = SelectOption::new("Amazon");
+ let google = SelectOption::new("Google");
+ let facebook = SelectOption::new("Facebook");
+ let type_option = SelectTypeOption {
+ options: vec![amazon.clone(), google.clone(), facebook.clone()],
+ disable_color: false,
+ };
+ let multi_select_type_option = MultiSelectTypeOption(type_option);
+
+ // insert a single option
+ let changeset = SelectOptionCellChangeset::from_insert_options(vec![google.id.clone()]);
+ let (_, select_option_ids) = multi_select_type_option
+ .apply_changeset(changeset, None)
+ .unwrap();
+ assert_eq!(select_option_ids.into_inner(), vec![google.id.clone()]);
+
+ // insert multiple options
+ let changeset = SelectOptionCellChangeset::from_insert_options(vec![
+ google.id.clone(),
+ facebook.id.clone(),
+ "".to_string(),
+ ]);
+ let (_, select_option_ids) = multi_select_type_option
+ .apply_changeset(changeset, None)
+ .unwrap();
+ assert_eq!(
+ select_option_ids.into_inner(),
+ vec![google.id.clone(), facebook.id.clone()]
+ );
+
+ // insert an option when one is already selected
+ let cell = SelectOptionIds::from(vec![google.id.clone(), facebook.id.clone()])
+ .to_cell(FieldType::MultiSelect);
+ let changeset =
+ SelectOptionCellChangeset::from_insert_options(vec![google.id.clone(), amazon.id.clone()]);
+ let (_, select_option_ids) = multi_select_type_option
+ .apply_changeset(changeset, Some(cell))
+ .unwrap();
+ assert_eq!(
+ select_option_ids.into_inner(),
+ vec![google.id, facebook.id, amazon.id]
+ );
+ }
+
+ #[test]
+ fn single_select_unselect_multi_option_test() {
+ let google = SelectOption::new("Google");
+ let facebook = SelectOption::new("Facebook");
+ let single_select = SingleSelectTypeOption(SelectTypeOption {
+ options: vec![google.clone(), facebook.clone()],
+ disable_color: false,
+ });
+ let option_ids = vec![google.id.clone(), facebook.id];
+
+ // insert
+ let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
+ let (cell, select_option_ids) = single_select.apply_changeset(changeset, None).unwrap();
+ assert_eq!(&*select_option_ids, &vec![google.id]);
+
+ // delete
+ let changeset = SelectOptionCellChangeset::from_delete_options(option_ids);
+ let select_option_ids = single_select
+ .apply_changeset(changeset, Some(cell))
+ .unwrap()
+ .1;
+ assert!(select_option_ids.is_empty());
+ }
+
+ #[test]
+ fn multi_select_unselect_multi_option_test() {
+ let google = SelectOption::new("Google");
+ let facebook = SelectOption::new("Facebook");
+ let multi_select = MultiSelectTypeOption(SelectTypeOption {
+ options: vec![google.clone(), facebook.clone()],
+ disable_color: false,
+ });
+ let option_ids = vec![google.id, facebook.id];
+
+ // insert
+ let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
+ let (cell, select_option_ids) = multi_select.apply_changeset(changeset, None).unwrap();
+ assert_eq!(&*select_option_ids, &option_ids);
+
+ // delete
+ let changeset = SelectOptionCellChangeset::from_delete_options(option_ids);
+ let select_option_ids = multi_select
+ .apply_changeset(changeset, Some(cell))
+ .unwrap()
+ .1;
+ assert!(select_option_ids.is_empty());
+ }
+}
diff --git a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/single_select_type_option.rs b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/single_select_type_option.rs
index b91cb5be3b..993f25b47f 100644
--- a/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/single_select_type_option.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/field/type_options/selection_type_option/single_select_type_option.rs
@@ -56,29 +56,26 @@ impl CellDataChangeset for SingleSelectTypeOption {
fn apply_changeset(
&self,
changeset: <Self as TypeOption>::CellChangeset,
- _cell: Option<Cell>,
+ cell: Option<Cell>,
) -> FlowyResult<(Cell, <Self as TypeOption>::CellData)> {
- let mut insert_option_ids = changeset
+ let valid_inserted_ids = changeset
.insert_option_ids
.into_iter()
- .filter(|insert_option_id| {
- self
- .options
- .iter()
- .any(|option| &option.id == insert_option_id)
- })
- .collect::<Vec<String>>();
+ .filter(|id| self.options.iter().any(|option| &option.id == id));
+
+ let existing_ids = cell
+ .map(|cell| SelectOptionIds::from(&cell))
+ .unwrap_or_default()
+ .into_inner()
+ .into_iter()
+ .filter(|id| !changeset.delete_option_ids.contains(id));
+
+ let select_option_ids: SelectOptionIds = valid_inserted_ids
+ .chain(existing_ids)
+ .take(1)
+ .collect::<Vec<_>>()
+ .into();
- // In single select, the insert_option_ids should only contain one select option id.
- // Sometimes, the insert_option_ids may contain list of option ids. For example,
- // copy/paste a ids string.
- let select_option_ids = if insert_option_ids.is_empty() {
- SelectOptionIds::from(insert_option_ids)
- } else {
- // Just take the first select option
- let _ = insert_option_ids.drain(1..);
- SelectOptionIds::from(insert_option_ids)
- };
Ok((
select_option_ids.to_cell(FieldType::SingleSelect),
select_option_ids,
@@ -122,48 +119,3 @@ impl TypeOptionCellDataCompare for SingleSelectTypeOption {
}
}
}
-
-#[cfg(test)]
-mod tests {
- use crate::services::cell::CellDataChangeset;
- use crate::services::field::type_options::*;
- use collab_database::fields::select_type_option::{
- SelectOption, SelectTypeOption, SingleSelectTypeOption,
- };
-
- #[test]
- fn single_select_insert_multi_option_test() {
- let google = SelectOption::new("Google");
- let facebook = SelectOption::new("Facebook");
- let single_select = SingleSelectTypeOption(SelectTypeOption {
- options: vec![google.clone(), facebook.clone()],
- disable_color: false,
- });
-
- let option_ids = vec![google.id.clone(), facebook.id];
- let changeset = SelectOptionCellChangeset::from_insert_options(option_ids);
- let select_option_ids = single_select.apply_changeset(changeset, None).unwrap().1;
- assert_eq!(&*select_option_ids, &vec![google.id]);
- }
-
- #[test]
- fn single_select_unselect_multi_option_test() {
- let google = SelectOption::new("Google");
- let facebook = SelectOption::new("Facebook");
- let single_select = SingleSelectTypeOption(SelectTypeOption {
- options: vec![google.clone(), facebook.clone()],
- disable_color: false,
- });
- let option_ids = vec![google.id.clone(), facebook.id];
-
- // insert
- let changeset = SelectOptionCellChangeset::from_insert_options(option_ids.clone());
- let select_option_ids = single_select.apply_changeset(changeset, None).unwrap().1;
- assert_eq!(&*select_option_ids, &vec![google.id]);
-
- // delete
- let changeset = SelectOptionCellChangeset::from_delete_options(option_ids);
- let select_option_ids = single_select.apply_changeset(changeset, None).unwrap().1;
- assert!(select_option_ids.is_empty());
- }
-}
diff --git a/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs b/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs
index 852a4ea591..ca4a1e5cee 100644
--- a/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs
+++ b/frontend/rust-lib/flowy-database2/tests/database/database_editor.rs
@@ -5,7 +5,7 @@ use collab_database::database::gen_database_view_id;
use collab_database::fields::checkbox_type_option::CheckboxTypeOption;
use collab_database::fields::checklist_type_option::ChecklistTypeOption;
use collab_database::fields::select_type_option::{
- MultiSelectTypeOption, SelectOption, SingleSelectTypeOption,
+ MultiSelectTypeOption, SelectOption, SelectOptionIds, SingleSelectTypeOption,
};
use collab_database::fields::Field;
use collab_database::rows::{Row, RowId};
@@ -274,7 +274,19 @@ impl DatabaseEditorTest {
.unwrap()
.clone();
- let cell_changeset = SelectOptionCellChangeset::from_insert_option_id(option_id);
+ let cell_changeset = if option_id.is_empty() {
+ let cell = self.editor.get_cell(&field.id, &row_id).await.unwrap();
+ let mut selected_ids = SelectOptionIds::from(&cell).into_inner();
+ if selected_ids.is_empty() {
+ SelectOptionCellChangeset::default()
+ } else {
+ let id = selected_ids.pop().unwrap();
+ SelectOptionCellChangeset::from_delete_option_id(&id)
+ }
+ } else {
+ SelectOptionCellChangeset::from_insert_option_id(option_id)
+ };
+
self
.update_cell(&field.id, row_id, BoxAny::new(cell_changeset))
.await