| 
									
										
										
										
											2024-09-25 11:44:19 +08:00
										 |  |  | use collab_database::fields::date_type_option::{
 | 
					
						
							|  |  |  |   DateFormat, DateTypeOption, TimeFormat, TimeTypeOption,
 | 
					
						
							|  |  |  | };
 | 
					
						
							|  |  |  | use collab_database::fields::select_type_option::{SelectOption, SingleSelectTypeOption};
 | 
					
						
							|  |  |  | use collab_database::fields::text_type_option::RichTextTypeOption;
 | 
					
						
							|  |  |  | use collab_database::fields::timestamp_type_option::TimestampTypeOption;
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use collab_database::fields::Field;
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  | use collab_database::views::OrderObjectPosition;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | use flowy_database2::entities::{CreateFieldParams, FieldType};
 | 
					
						
							| 
									
										
										
										
											2024-09-25 11:44:19 +08:00
										 |  |  | use flowy_database2::services::field::{type_option_to_pb, FieldBuilder};
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub fn create_text_field(grid_id: &str) -> (CreateFieldParams, Field) {
 | 
					
						
							|  |  |  |   let field_type = FieldType::RichText;
 | 
					
						
							| 
									
										
										
										
											2024-09-25 11:44:19 +08:00
										 |  |  |   let type_option = RichTextTypeOption;
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |   let text_field = FieldBuilder::new(field_type, type_option.clone())
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     .name("Name")
 | 
					
						
							|  |  |  |     .primary(true)
 | 
					
						
							|  |  |  |     .build();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
 | 
					
						
							|  |  |  |   let params = CreateFieldParams {
 | 
					
						
							|  |  |  |     view_id: grid_id.to_owned(),
 | 
					
						
							|  |  |  |     field_type,
 | 
					
						
							|  |  |  |     type_option_data: Some(type_option_data),
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  |     field_name: None,
 | 
					
						
							|  |  |  |     position: OrderObjectPosition::default(),
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   };
 | 
					
						
							|  |  |  |   (params, text_field)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn create_single_select_field(grid_id: &str) -> (CreateFieldParams, Field) {
 | 
					
						
							|  |  |  |   let field_type = FieldType::SingleSelect;
 | 
					
						
							|  |  |  |   let mut type_option = SingleSelectTypeOption::default();
 | 
					
						
							|  |  |  |   type_option.options.push(SelectOption::new("Done"));
 | 
					
						
							|  |  |  |   type_option.options.push(SelectOption::new("Progress"));
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |   let single_select_field = FieldBuilder::new(field_type, type_option.clone())
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |     .name("Name")
 | 
					
						
							|  |  |  |     .build();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
 | 
					
						
							|  |  |  |   let params = CreateFieldParams {
 | 
					
						
							|  |  |  |     view_id: grid_id.to_owned(),
 | 
					
						
							|  |  |  |     field_type,
 | 
					
						
							|  |  |  |     type_option_data: Some(type_option_data),
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  |     field_name: None,
 | 
					
						
							|  |  |  |     position: OrderObjectPosition::default(),
 | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  |   };
 | 
					
						
							|  |  |  |   (params, single_select_field)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2023-09-08 10:07:24 +08:00
										 |  |  | #[allow(dead_code)]
 | 
					
						
							|  |  |  | pub fn create_date_field(grid_id: &str) -> (CreateFieldParams, Field) {
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |   let date_type_option = DateTypeOption {
 | 
					
						
							|  |  |  |     date_format: DateFormat::US,
 | 
					
						
							|  |  |  |     time_format: TimeFormat::TwentyFourHour,
 | 
					
						
							| 
									
										
										
										
											2023-05-31 16:52:37 +08:00
										 |  |  |     timezone_id: "Etc/UTC".to_owned(),
 | 
					
						
							| 
									
										
										
										
											2023-09-08 10:07:24 +08:00
										 |  |  |   };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let field = FieldBuilder::new(FieldType::DateTime, date_type_option.clone())
 | 
					
						
							|  |  |  |     .name("Date")
 | 
					
						
							|  |  |  |     .build();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let type_option_data = type_option_to_pb(date_type_option.into(), &FieldType::DateTime).to_vec();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let params = CreateFieldParams {
 | 
					
						
							|  |  |  |     view_id: grid_id.to_owned(),
 | 
					
						
							|  |  |  |     field_type: FieldType::DateTime,
 | 
					
						
							|  |  |  |     type_option_data: Some(type_option_data),
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  |     field_name: None,
 | 
					
						
							|  |  |  |     position: OrderObjectPosition::default(),
 | 
					
						
							| 
									
										
										
										
											2023-09-08 10:07:24 +08:00
										 |  |  |   };
 | 
					
						
							|  |  |  |   (params, field)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn create_timestamp_field(grid_id: &str, field_type: FieldType) -> (CreateFieldParams, Field) {
 | 
					
						
							|  |  |  |   let timestamp_type_option = TimestampTypeOption {
 | 
					
						
							|  |  |  |     date_format: DateFormat::US,
 | 
					
						
							|  |  |  |     time_format: TimeFormat::TwentyFourHour,
 | 
					
						
							|  |  |  |     include_time: true,
 | 
					
						
							| 
									
										
										
										
											2024-09-25 11:44:19 +08:00
										 |  |  |     field_type: field_type.into(),
 | 
					
						
							| 
									
										
										
										
											2024-12-05 14:12:25 +08:00
										 |  |  |     timezone: None,
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |   };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let field: Field = match field_type {
 | 
					
						
							| 
									
										
										
										
											2023-12-06 09:14:02 -08:00
										 |  |  |     FieldType::LastEditedTime => FieldBuilder::new(field_type, timestamp_type_option.clone())
 | 
					
						
							|  |  |  |       .name("Updated At")
 | 
					
						
							|  |  |  |       .build(),
 | 
					
						
							|  |  |  |     FieldType::CreatedTime => FieldBuilder::new(field_type, timestamp_type_option.clone())
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |       .name("Created At")
 | 
					
						
							|  |  |  |       .build(),
 | 
					
						
							|  |  |  |     _ => panic!("Unsupported group field type"),
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 10:07:24 +08:00
										 |  |  |   let type_option_data = type_option_to_pb(timestamp_type_option.into(), &field_type).to_vec();
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  | 
 | 
					
						
							|  |  |  |   let params = CreateFieldParams {
 | 
					
						
							|  |  |  |     view_id: grid_id.to_owned(),
 | 
					
						
							|  |  |  |     field_type,
 | 
					
						
							|  |  |  |     type_option_data: Some(type_option_data),
 | 
					
						
							| 
									
										
										
										
											2023-11-29 04:42:53 +08:00
										 |  |  |     field_name: None,
 | 
					
						
							|  |  |  |     position: OrderObjectPosition::default(),
 | 
					
						
							| 
									
										
										
										
											2023-05-26 14:04:17 +03:30
										 |  |  |   };
 | 
					
						
							|  |  |  |   (params, field)
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2024-06-13 10:22:13 +03:30
										 |  |  | 
 | 
					
						
							|  |  |  | pub fn create_time_field(grid_id: &str) -> (CreateFieldParams, Field) {
 | 
					
						
							|  |  |  |   let field_type = FieldType::Time;
 | 
					
						
							|  |  |  |   let type_option = TimeTypeOption;
 | 
					
						
							|  |  |  |   let text_field = FieldBuilder::new(field_type, type_option.clone())
 | 
					
						
							|  |  |  |     .name("Time field")
 | 
					
						
							|  |  |  |     .build();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let type_option_data = type_option_to_pb(type_option.into(), &field_type).to_vec();
 | 
					
						
							|  |  |  |   let params = CreateFieldParams {
 | 
					
						
							|  |  |  |     view_id: grid_id.to_owned(),
 | 
					
						
							|  |  |  |     field_type,
 | 
					
						
							|  |  |  |     type_option_data: Some(type_option_data),
 | 
					
						
							|  |  |  |     field_name: None,
 | 
					
						
							|  |  |  |     position: OrderObjectPosition::default(),
 | 
					
						
							|  |  |  |   };
 | 
					
						
							|  |  |  |   (params, text_field)
 | 
					
						
							|  |  |  | }
 |