feat: can transform options from single & multi select (#1493)

* feat: can transform options from single & multi select

* test: add single-multi switch test
This commit is contained in:
ENsu 2022-12-04 16:32:37 +08:00 committed by GitHub
parent 63613689cd
commit a2161216d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 2 deletions

View File

@ -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]

View File

@ -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");

View File

@ -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());
}
})
}
_ => {}
}
}