grid_mock_data.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. use crate::database::mock_data::{
  2. COMPLETED, FACEBOOK, FIRST_THING, GOOGLE, PAUSED, PLANNED, SECOND_THING, THIRD_THING, TWITTER,
  3. };
  4. use collab_database::database::{gen_database_id, gen_database_view_id, DatabaseData};
  5. use collab_database::views::{DatabaseLayout, DatabaseView};
  6. use crate::database::database_editor::TestRowBuilder;
  7. use flowy_database2::entities::FieldType;
  8. use flowy_database2::services::field::{
  9. ChecklistTypeOption, DateFormat, DateTypeOption, FieldBuilder, MultiSelectTypeOption,
  10. NumberFormat, NumberTypeOption, SelectOption, SelectOptionColor, SingleSelectTypeOption,
  11. TimeFormat,
  12. };
  13. use strum::IntoEnumIterator;
  14. pub fn make_test_grid() -> DatabaseData {
  15. let mut fields = vec![];
  16. let mut rows = vec![];
  17. // Iterate through the FieldType to create the corresponding Field.
  18. for field_type in FieldType::iter() {
  19. match field_type {
  20. FieldType::RichText => {
  21. let text_field = FieldBuilder::from_field_type(field_type.clone())
  22. .name("Name")
  23. .visibility(true)
  24. .primary(true)
  25. .build();
  26. fields.push(text_field);
  27. },
  28. FieldType::Number => {
  29. // Number
  30. let mut type_option = NumberTypeOption::default();
  31. type_option.set_format(NumberFormat::USD);
  32. let number_field = FieldBuilder::new(field_type.clone(), type_option)
  33. .name("Price")
  34. .visibility(true)
  35. .build();
  36. fields.push(number_field);
  37. },
  38. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => {
  39. // Date
  40. let date_type_option = DateTypeOption {
  41. date_format: DateFormat::US,
  42. time_format: TimeFormat::TwentyFourHour,
  43. field_type: field_type.clone(),
  44. };
  45. let name = match field_type {
  46. FieldType::DateTime => "Time",
  47. FieldType::UpdatedAt => "Updated At",
  48. FieldType::CreatedAt => "Created At",
  49. _ => "",
  50. };
  51. let date_field = FieldBuilder::new(field_type.clone(), date_type_option)
  52. .name(name)
  53. .visibility(true)
  54. .build();
  55. fields.push(date_field);
  56. },
  57. FieldType::SingleSelect => {
  58. // Single Select
  59. let option1 = SelectOption::with_color(COMPLETED, SelectOptionColor::Purple);
  60. let option2 = SelectOption::with_color(PLANNED, SelectOptionColor::Orange);
  61. let option3 = SelectOption::with_color(PAUSED, SelectOptionColor::Yellow);
  62. let mut single_select_type_option = SingleSelectTypeOption::default();
  63. single_select_type_option
  64. .options
  65. .extend(vec![option1, option2, option3]);
  66. let single_select_field = FieldBuilder::new(field_type.clone(), single_select_type_option)
  67. .name("Status")
  68. .visibility(true)
  69. .build();
  70. fields.push(single_select_field);
  71. },
  72. FieldType::MultiSelect => {
  73. // MultiSelect
  74. let option1 = SelectOption::with_color(GOOGLE, SelectOptionColor::Purple);
  75. let option2 = SelectOption::with_color(FACEBOOK, SelectOptionColor::Orange);
  76. let option3 = SelectOption::with_color(TWITTER, SelectOptionColor::Yellow);
  77. let mut type_option = MultiSelectTypeOption::default();
  78. type_option.options.extend(vec![option1, option2, option3]);
  79. let multi_select_field = FieldBuilder::new(field_type.clone(), type_option)
  80. .name("Platform")
  81. .visibility(true)
  82. .build();
  83. fields.push(multi_select_field);
  84. },
  85. FieldType::Checkbox => {
  86. // Checkbox
  87. let checkbox_field = FieldBuilder::from_field_type(field_type.clone())
  88. .name("is urgent")
  89. .visibility(true)
  90. .build();
  91. fields.push(checkbox_field);
  92. },
  93. FieldType::URL => {
  94. // URL
  95. let url = FieldBuilder::from_field_type(field_type.clone())
  96. .name("link")
  97. .visibility(true)
  98. .build();
  99. fields.push(url);
  100. },
  101. FieldType::Checklist => {
  102. let option1 = SelectOption::with_color(FIRST_THING, SelectOptionColor::Purple);
  103. let option2 = SelectOption::with_color(SECOND_THING, SelectOptionColor::Orange);
  104. let option3 = SelectOption::with_color(THIRD_THING, SelectOptionColor::Yellow);
  105. let mut type_option = ChecklistTypeOption::default();
  106. type_option.options.extend(vec![option1, option2, option3]);
  107. let checklist_field = FieldBuilder::new(field_type.clone(), type_option)
  108. .name("TODO")
  109. .visibility(true)
  110. .build();
  111. fields.push(checklist_field);
  112. },
  113. }
  114. }
  115. for i in 0..6 {
  116. let mut row_builder = TestRowBuilder::new(i.into(), &fields);
  117. match i {
  118. 0 => {
  119. for field_type in FieldType::iter() {
  120. match field_type {
  121. FieldType::RichText => row_builder.insert_text_cell("A"),
  122. FieldType::Number => row_builder.insert_number_cell("1"),
  123. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  124. .insert_date_cell(
  125. "1647251762",
  126. None,
  127. None,
  128. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  129. &field_type,
  130. ),
  131. FieldType::MultiSelect => row_builder
  132. .insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(0)]),
  133. FieldType::Checklist => row_builder.insert_checklist_cell(|options| options),
  134. FieldType::Checkbox => row_builder.insert_checkbox_cell("true"),
  135. FieldType::URL => {
  136. row_builder.insert_url_cell("AppFlowy website - https://www.appflowy.io")
  137. },
  138. _ => "".to_owned(),
  139. };
  140. }
  141. },
  142. 1 => {
  143. for field_type in FieldType::iter() {
  144. match field_type {
  145. FieldType::RichText => row_builder.insert_text_cell(""),
  146. FieldType::Number => row_builder.insert_number_cell("2"),
  147. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  148. .insert_date_cell(
  149. "1647251762",
  150. None,
  151. None,
  152. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  153. &field_type,
  154. ),
  155. FieldType::MultiSelect => row_builder
  156. .insert_multi_select_cell(|mut options| vec![options.remove(0), options.remove(1)]),
  157. FieldType::Checkbox => row_builder.insert_checkbox_cell("true"),
  158. _ => "".to_owned(),
  159. };
  160. }
  161. },
  162. 2 => {
  163. for field_type in FieldType::iter() {
  164. match field_type {
  165. FieldType::RichText => row_builder.insert_text_cell("C"),
  166. FieldType::Number => row_builder.insert_number_cell("3"),
  167. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  168. .insert_date_cell(
  169. "1647251762",
  170. None,
  171. None,
  172. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  173. &field_type,
  174. ),
  175. FieldType::SingleSelect => {
  176. row_builder.insert_single_select_cell(|mut options| options.remove(0))
  177. },
  178. FieldType::MultiSelect => {
  179. row_builder.insert_multi_select_cell(|mut options| vec![options.remove(1)])
  180. },
  181. FieldType::Checkbox => row_builder.insert_checkbox_cell("false"),
  182. _ => "".to_owned(),
  183. };
  184. }
  185. },
  186. 3 => {
  187. for field_type in FieldType::iter() {
  188. match field_type {
  189. FieldType::RichText => row_builder.insert_text_cell("DA"),
  190. FieldType::Number => row_builder.insert_number_cell("14"),
  191. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  192. .insert_date_cell(
  193. "1668704685",
  194. None,
  195. None,
  196. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  197. &field_type,
  198. ),
  199. FieldType::SingleSelect => {
  200. row_builder.insert_single_select_cell(|mut options| options.remove(0))
  201. },
  202. FieldType::Checkbox => row_builder.insert_checkbox_cell("false"),
  203. _ => "".to_owned(),
  204. };
  205. }
  206. },
  207. 4 => {
  208. for field_type in FieldType::iter() {
  209. match field_type {
  210. FieldType::RichText => row_builder.insert_text_cell("AE"),
  211. FieldType::Number => row_builder.insert_number_cell(""),
  212. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  213. .insert_date_cell(
  214. "1668359085",
  215. None,
  216. None,
  217. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  218. &field_type,
  219. ),
  220. FieldType::SingleSelect => {
  221. row_builder.insert_single_select_cell(|mut options| options.remove(1))
  222. },
  223. FieldType::Checkbox => row_builder.insert_checkbox_cell("false"),
  224. _ => "".to_owned(),
  225. };
  226. }
  227. },
  228. 5 => {
  229. for field_type in FieldType::iter() {
  230. match field_type {
  231. FieldType::RichText => row_builder.insert_text_cell("AE"),
  232. FieldType::Number => row_builder.insert_number_cell("5"),
  233. FieldType::DateTime | FieldType::UpdatedAt | FieldType::CreatedAt => row_builder
  234. .insert_date_cell(
  235. "1671938394",
  236. None,
  237. None,
  238. Some(chrono_tz::Tz::Etc__GMTPlus8.to_string()),
  239. &field_type,
  240. ),
  241. FieldType::SingleSelect => {
  242. row_builder.insert_single_select_cell(|mut options| options.remove(1))
  243. },
  244. FieldType::Checkbox => row_builder.insert_checkbox_cell("true"),
  245. _ => "".to_owned(),
  246. };
  247. }
  248. },
  249. _ => {},
  250. }
  251. let row = row_builder.build();
  252. rows.push(row);
  253. }
  254. let view = DatabaseView {
  255. id: gen_database_view_id(),
  256. database_id: gen_database_id(),
  257. name: "".to_string(),
  258. layout: DatabaseLayout::Grid,
  259. layout_settings: Default::default(),
  260. filters: vec![],
  261. group_settings: vec![],
  262. sorts: vec![],
  263. row_orders: vec![],
  264. field_orders: vec![],
  265. created_at: 0,
  266. modified_at: 0,
  267. };
  268. DatabaseData { view, fields, rows }
  269. }