chore: fix bugs

This commit is contained in:
appflowy
2022-05-19 10:41:00 +08:00
parent f3b87d419f
commit 23900b49f5
20 changed files with 322 additions and 260 deletions

View File

@ -2,7 +2,6 @@ use crate::manager::GridManager;
use crate::services::entities::*;
use crate::services::field::type_options::*;
use crate::services::field::{default_type_option_builder_from_type, type_option_builder_from_json_str};
use crate::services::grid_editor::ClientGridEditor;
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_grid_data_model::entities::*;
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
@ -100,15 +99,24 @@ pub(crate) async fn switch_to_field_handler(
manager: AppData<Arc<GridManager>>,
) -> DataResult<FieldTypeOptionData, FlowyError> {
let params: EditFieldParams = data.into_inner().try_into()?;
if params.field_id.is_none() {
return Err(ErrorCode::FieldIdIsEmpty.into());
}
let field_id = params.field_id.unwrap();
let editor = manager.get_grid_editor(&params.grid_id)?;
editor.switch_to_field_type(&field_id, &params.field_type).await?;
let field_meta = editor.get_field_meta(&field_id).await;
let data =
make_field_type_option_data(&params.grid_id, Some(field_id), params.field_type, editor, field_meta).await?;
editor
.switch_to_field_type(&params.field_id, &params.field_type)
.await?;
// Get the FieldMeta with field_id, if it doesn't exist, we create the default FieldMeta from the FieldType.
let field_meta = editor
.get_field_meta(&params.field_id)
.await
.unwrap_or(editor.next_field_meta(&params.field_type).await?);
let type_option_data = get_type_option_data(&field_meta, &params.field_type).await?;
let data = FieldTypeOptionData {
grid_id: params.grid_id,
field: field_meta.into(),
type_option_data,
};
data_result(data)
}
@ -123,6 +131,7 @@ pub(crate) async fn duplicate_field_handler(
Ok(())
}
/// Return the FieldTypeOptionData if the Field exists otherwise return record not found error.
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn get_field_type_option_data_handler(
data: Data<EditFieldPayload>,
@ -130,7 +139,29 @@ pub(crate) async fn get_field_type_option_data_handler(
) -> DataResult<FieldTypeOptionData, FlowyError> {
let params: EditFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let field_meta = get_or_create_field_meta(params.field_id, &params.field_type, editor).await?;
match editor.get_field_meta(&params.field_id).await {
None => Err(FlowyError::record_not_found()),
Some(field_meta) => {
let type_option_data = get_type_option_data(&field_meta, &field_meta.field_type).await?;
let data = FieldTypeOptionData {
grid_id: params.grid_id,
field: field_meta.into(),
type_option_data,
};
data_result(data)
}
}
}
/// Create FieldMeta and save it. Return the FieldTypeOptionData.
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn create_field_type_option_data_handler(
data: Data<EditFieldPayload>,
manager: AppData<Arc<GridManager>>,
) -> DataResult<FieldTypeOptionData, FlowyError> {
let params: CreateFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let field_meta = editor.create_next_field_meta(&params.field_type).await?;
let type_option_data = get_type_option_data(&field_meta, &field_meta.field_type).await?;
data_result(FieldTypeOptionData {
@ -151,23 +182,6 @@ pub(crate) async fn move_item_handler(
Ok(())
}
async fn make_field_type_option_data(
grid_id: &str,
field_id: Option<String>,
field_type: FieldType,
editor: Arc<ClientGridEditor>,
field_meta: Option<FieldMeta>,
) -> FlowyResult<FieldTypeOptionData> {
let field_meta = field_meta.unwrap_or(get_or_create_field_meta(field_id, &field_type, editor).await?);
let type_option_data = get_type_option_data(&field_meta, &field_type).await?;
Ok(FieldTypeOptionData {
grid_id: grid_id.to_string(),
field: field_meta.into(),
type_option_data,
})
}
/// The FieldMeta contains multiple data, each of them belongs to a specific FieldType.
async fn get_type_option_data(field_meta: &FieldMeta, field_type: &FieldType) -> FlowyResult<Vec<u8>> {
let s = field_meta
@ -179,20 +193,6 @@ async fn get_type_option_data(field_meta: &FieldMeta, field_type: &FieldType) ->
Ok(type_option_data)
}
async fn get_or_create_field_meta(
field_id: Option<String>,
field_type: &FieldType,
editor: Arc<ClientGridEditor>,
) -> FlowyResult<FieldMeta> {
match field_id {
None => editor.create_next_field_meta(field_type).await,
Some(field_id) => match editor.get_field_meta(&field_id).await {
None => editor.create_next_field_meta(field_type).await,
Some(field_meta) => Ok(field_meta),
},
}
}
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn get_row_handler(
data: Data<RowIdentifierPayload>,

View File

@ -20,6 +20,7 @@ pub fn create(grid_manager: Arc<GridManager>) -> Module {
.event(GridEvent::DuplicateField, duplicate_field_handler)
.event(GridEvent::MoveItem, move_item_handler)
.event(GridEvent::GetFieldTypeOption, get_field_type_option_data_handler)
.event(GridEvent::CreateFieldTypeOption, create_field_type_option_data_handler)
// Row
.event(GridEvent::CreateRow, create_row_handler)
.event(GridEvent::GetRow, get_row_handler)
@ -76,6 +77,9 @@ pub enum GridEvent {
#[event(input = "EditFieldPayload", output = "FieldTypeOptionData")]
GetFieldTypeOption = 23,
#[event(input = "EditFieldPayload", output = "FieldTypeOptionData")]
CreateFieldTypeOption = 24,
#[event(input = "CreateSelectOptionPayload", output = "SelectOption")]
NewSelectOption = 30,

View File

@ -36,6 +36,7 @@ pub enum GridEvent {
DuplicateField = 21,
MoveItem = 22,
GetFieldTypeOption = 23,
CreateFieldTypeOption = 24,
NewSelectOption = 30,
GetSelectOptionCellData = 31,
UpdateSelectOption = 32,
@ -68,6 +69,7 @@ impl ::protobuf::ProtobufEnum for GridEvent {
21 => ::std::option::Option::Some(GridEvent::DuplicateField),
22 => ::std::option::Option::Some(GridEvent::MoveItem),
23 => ::std::option::Option::Some(GridEvent::GetFieldTypeOption),
24 => ::std::option::Option::Some(GridEvent::CreateFieldTypeOption),
30 => ::std::option::Option::Some(GridEvent::NewSelectOption),
31 => ::std::option::Option::Some(GridEvent::GetSelectOptionCellData),
32 => ::std::option::Option::Some(GridEvent::UpdateSelectOption),
@ -97,6 +99,7 @@ impl ::protobuf::ProtobufEnum for GridEvent {
GridEvent::DuplicateField,
GridEvent::MoveItem,
GridEvent::GetFieldTypeOption,
GridEvent::CreateFieldTypeOption,
GridEvent::NewSelectOption,
GridEvent::GetSelectOptionCellData,
GridEvent::UpdateSelectOption,
@ -137,18 +140,18 @@ impl ::protobuf::reflect::ProtobufValue for GridEvent {
}
static file_descriptor_proto_data: &'static [u8] = b"\
\n\x0fevent_map.proto*\xc1\x03\n\tGridEvent\x12\x0f\n\x0bGetGridData\x10\
\n\x0fevent_map.proto*\xdc\x03\n\tGridEvent\x12\x0f\n\x0bGetGridData\x10\
\0\x12\x11\n\rGetGridBlocks\x10\x01\x12\r\n\tGetFields\x10\n\x12\x0f\n\
\x0bUpdateField\x10\x0b\x12\x19\n\x15UpdateFieldTypeOption\x10\x0c\x12\
\x0f\n\x0bInsertField\x10\r\x12\x0f\n\x0bDeleteField\x10\x0e\x12\x11\n\r\
SwitchToField\x10\x14\x12\x12\n\x0eDuplicateField\x10\x15\x12\x0c\n\x08M\
oveItem\x10\x16\x12\x16\n\x12GetFieldTypeOption\x10\x17\x12\x13\n\x0fNew\
SelectOption\x10\x1e\x12\x1b\n\x17GetSelectOptionCellData\x10\x1f\x12\
\x16\n\x12UpdateSelectOption\x10\x20\x12\r\n\tCreateRow\x102\x12\n\n\x06\
GetRow\x103\x12\r\n\tDeleteRow\x104\x12\x10\n\x0cDuplicateRow\x105\x12\
\x0b\n\x07GetCell\x10F\x12\x0e\n\nUpdateCell\x10G\x12\x1a\n\x16UpdateSel\
ectOptionCell\x10H\x12\x12\n\x0eUpdateDateCell\x10P\x12\x13\n\x0fGetDate\
CellData\x10Zb\x06proto3\
oveItem\x10\x16\x12\x16\n\x12GetFieldTypeOption\x10\x17\x12\x19\n\x15Cre\
ateFieldTypeOption\x10\x18\x12\x13\n\x0fNewSelectOption\x10\x1e\x12\x1b\
\n\x17GetSelectOptionCellData\x10\x1f\x12\x16\n\x12UpdateSelectOption\
\x10\x20\x12\r\n\tCreateRow\x102\x12\n\n\x06GetRow\x103\x12\r\n\tDeleteR\
ow\x104\x12\x10\n\x0cDuplicateRow\x105\x12\x0b\n\x07GetCell\x10F\x12\x0e\
\n\nUpdateCell\x10G\x12\x1a\n\x16UpdateSelectOptionCell\x10H\x12\x12\n\
\x0eUpdateDateCell\x10P\x12\x13\n\x0fGetDateCellData\x10Zb\x06proto3\
";
static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT;

View File

@ -12,6 +12,7 @@ enum GridEvent {
DuplicateField = 21;
MoveItem = 22;
GetFieldTypeOption = 23;
CreateFieldTypeOption = 24;
NewSelectOption = 30;
GetSelectOptionCellData = 31;
UpdateSelectOption = 32;

View File

@ -121,12 +121,22 @@ impl ClientGridEditor {
Ok(())
}
pub async fn create_next_field_meta(&self, field_type: &FieldType) -> FlowyResult<FieldMeta> {
pub async fn next_field_meta(&self, field_type: &FieldType) -> FlowyResult<FieldMeta> {
let name = format!("Property {}", self.grid_pad.read().await.fields().len() + 1);
let field_meta = FieldBuilder::from_field_type(field_type).name(&name).build();
Ok(field_meta)
}
pub async fn create_next_field_meta(&self, field_type: &FieldType) -> FlowyResult<FieldMeta> {
let field_meta = self.next_field_meta(field_type).await?;
let _ = self
.modify(|grid| Ok(grid.create_field_meta(field_meta.clone(), None)?))
.await?;
let _ = self.notify_did_insert_grid_field(&field_meta.id).await?;
Ok(field_meta)
}
pub async fn contain_field(&self, field_id: &str) -> bool {
self.grid_pad.read().await.contain_field(field_id)
}