From a2161216d81624b742e6c46709c86c81c21f3f86 Mon Sep 17 00:00:00 2001 From: ENsu Date: Sun, 4 Dec 2022 16:32:37 +0800 Subject: [PATCH] feat: can transform options from single & multi select (#1493) * feat: can transform options from single & multi select * test: add single-multi switch test --- .../multi_select_type_option.rs | 21 +++++++++++++++++++ .../single_select_type_option.rs | 21 +++++++++++++++++++ .../type_option_transform.rs | 21 +++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs index 610e32f072..3fd3f7db52 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/multi_select_type_option.rs @@ -134,6 +134,27 @@ mod tests { debug_assert_eq!(multi_select.0.options.len(), 2); } + #[test] + fn multi_select_transform_with_multiselect_option_test() { + let mut singleselect_type_option_builder = SingleSelectTypeOptionBuilder::default(); + + let google = SelectOptionPB::new("Google"); + singleselect_type_option_builder = singleselect_type_option_builder.add_option(google); + + let facebook = SelectOptionPB::new("Facebook"); + singleselect_type_option_builder = singleselect_type_option_builder.add_option(facebook); + + let singleselect_type_option_data = singleselect_type_option_builder.serializer().json_str(); + + let mut multi_select = MultiSelectTypeOptionBuilder::default(); + multi_select.transform(&FieldType::MultiSelect, singleselect_type_option_data.clone()); + debug_assert_eq!(multi_select.0.options.len(), 2); + + // Already contain the yes/no option. It doesn't need to insert new options + multi_select.transform(&FieldType::MultiSelect, singleselect_type_option_data); + debug_assert_eq!(multi_select.0.options.len(), 2); + } + // #[test] #[test] diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/single_select_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/single_select_type_option.rs index d36c09c766..1eaf945202 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/single_select_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/single_select_type_option.rs @@ -120,6 +120,27 @@ mod tests { debug_assert_eq!(single_select.0.options.len(), 2); } + #[test] + fn single_select_transform_with_multiselect_option_test() { + let mut multiselect_type_option_builder = MultiSelectTypeOptionBuilder::default(); + + let google = SelectOptionPB::new("Google"); + multiselect_type_option_builder = multiselect_type_option_builder.add_option(google); + + let facebook = SelectOptionPB::new("Facebook"); + multiselect_type_option_builder = multiselect_type_option_builder.add_option(facebook); + + let multiselect_type_option_data = multiselect_type_option_builder.serializer().json_str(); + + let mut single_select = SingleSelectTypeOptionBuilder::default(); + single_select.transform(&FieldType::MultiSelect, multiselect_type_option_data.clone()); + debug_assert_eq!(single_select.0.options.len(), 2); + + // Already contain the yes/no option. It doesn't need to insert new options + single_select.transform(&FieldType::MultiSelect, multiselect_type_option_data); + debug_assert_eq!(single_select.0.options.len(), 2); + } + #[test] fn single_select_insert_multi_option_test() { let google = SelectOptionPB::new("Google"); diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/type_option_transform.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/type_option_transform.rs index 7af1328496..ed673e56b3 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/type_option_transform.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option/type_option_transform.rs @@ -1,10 +1,12 @@ use crate::entities::FieldType; use crate::services::cell::{CellBytes, CellData}; use crate::services::field::{ - SelectOptionColorPB, SelectOptionIds, SelectOptionPB, SelectTypeOptionSharedAction, CHECK, UNCHECK, + MultiSelectTypeOptionPB, SelectOptionColorPB, SelectOptionIds, SelectOptionPB, SelectTypeOptionSharedAction, + SingleSelectTypeOptionPB, CHECK, UNCHECK, }; use flowy_error::FlowyResult; use grid_rev_model::FieldRevision; +use serde_json; /// Handles how to transform the cell data when switching between different field types pub struct SelectOptionTypeOptionTransformer(); @@ -33,7 +35,22 @@ impl SelectOptionTypeOptionTransformer { shared.mut_options().push(uncheck_option); } } - FieldType::MultiSelect => {} + FieldType::MultiSelect => { + let option_pb: MultiSelectTypeOptionPB = serde_json::from_str(_type_option_data.as_str()).unwrap(); + option_pb.options.iter().for_each(|new_option| { + if !shared.options().iter().any(|option| option.name == new_option.name) { + shared.mut_options().push(new_option.clone()); + } + }) + } + FieldType::SingleSelect => { + let option_pb: SingleSelectTypeOptionPB = serde_json::from_str(_type_option_data.as_str()).unwrap(); + option_pb.options.iter().for_each(|new_option| { + if !shared.options().iter().any(|option| option.name == new_option.name) { + shared.mut_options().push(new_option.clone()); + } + }) + } _ => {} } }