mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: remove insertField function in dart
This commit is contained in:
parent
4e122a9452
commit
a2d27657dc
@ -67,25 +67,6 @@ class FieldService {
|
||||
return GridEventUpdateField(payload).send();
|
||||
}
|
||||
|
||||
// Create the field if it does not exist. Otherwise, update the field.
|
||||
static Future<Either<Unit, FlowyError>> insertField({
|
||||
required String gridId,
|
||||
required FieldPB field,
|
||||
List<int>? typeOptionData,
|
||||
String? startFieldId,
|
||||
}) {
|
||||
var payload = InsertFieldPayloadPB.create()
|
||||
..gridId = gridId
|
||||
..field_2 = field
|
||||
..typeOptionData = typeOptionData ?? [];
|
||||
|
||||
if (startFieldId != null) {
|
||||
payload.startFieldId = startFieldId;
|
||||
}
|
||||
|
||||
return GridEventInsertField(payload).send();
|
||||
}
|
||||
|
||||
static Future<Either<Unit, FlowyError>> updateFieldTypeOption({
|
||||
required String gridId,
|
||||
required String fieldId,
|
||||
|
@ -145,7 +145,7 @@ abstract class IFieldTypeOptionLoader {
|
||||
String get gridId;
|
||||
Future<Either<FieldTypeOptionDataPB, FlowyError>> load();
|
||||
|
||||
Future<Either<FieldTypeOptionDataPB, FlowyError>> switchToField(
|
||||
Future<Either<Unit, FlowyError>> switchToField(
|
||||
String fieldId, FieldType fieldType) {
|
||||
final payload = EditFieldPayloadPB.create()
|
||||
..gridId = gridId
|
||||
|
@ -47,64 +47,41 @@ class TypeOptionDataController {
|
||||
return _data.field_2;
|
||||
}
|
||||
|
||||
set field(FieldPB field) {
|
||||
_updateData(newField: field);
|
||||
}
|
||||
|
||||
T getTypeOption<T>(TypeOptionDataParser<T> parser) {
|
||||
return parser.fromBuffer(_data.typeOptionData);
|
||||
}
|
||||
|
||||
set fieldName(String name) {
|
||||
_updateData(newName: name);
|
||||
}
|
||||
|
||||
set typeOptionData(List<int> typeOptionData) {
|
||||
_updateData(newTypeOptionData: typeOptionData);
|
||||
}
|
||||
|
||||
void _updateData({
|
||||
String? newName,
|
||||
FieldPB? newField,
|
||||
List<int>? newTypeOptionData,
|
||||
}) {
|
||||
_data = _data.rebuild((rebuildData) {
|
||||
if (newName != null) {
|
||||
rebuildData.field_2 = rebuildData.field_2.rebuild((rebuildField) {
|
||||
rebuildField.name = newName;
|
||||
});
|
||||
}
|
||||
|
||||
if (newField != null) {
|
||||
rebuildData.field_2 = newField;
|
||||
}
|
||||
|
||||
if (newTypeOptionData != null) {
|
||||
rebuildData.typeOptionData = newTypeOptionData;
|
||||
}
|
||||
rebuildData.field_2 = rebuildData.field_2.rebuild((rebuildField) {
|
||||
rebuildField.name = name;
|
||||
});
|
||||
});
|
||||
|
||||
_fieldNotifier.value = _data.field_2;
|
||||
|
||||
FieldService.insertField(
|
||||
FieldService(gridId: gridId, fieldId: field.id).updateField(name: name);
|
||||
}
|
||||
|
||||
set typeOptionData(List<int> typeOptionData) {
|
||||
_data = _data.rebuild((rebuildData) {
|
||||
if (typeOptionData.isNotEmpty) {
|
||||
rebuildData.typeOptionData = typeOptionData;
|
||||
}
|
||||
});
|
||||
|
||||
FieldService.updateFieldTypeOption(
|
||||
gridId: gridId,
|
||||
field: field,
|
||||
typeOptionData: _data.typeOptionData,
|
||||
fieldId: field.id,
|
||||
typeOptionData: typeOptionData,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> switchToField(FieldType newFieldType) {
|
||||
return loader.switchToField(field.id, newFieldType).then((result) {
|
||||
return result.fold(
|
||||
(fieldTypeOptionData) {
|
||||
_updateData(
|
||||
newField: fieldTypeOptionData.field_2,
|
||||
newTypeOptionData: fieldTypeOptionData.typeOptionData,
|
||||
);
|
||||
},
|
||||
(err) {
|
||||
Log.error(err);
|
||||
},
|
||||
(_) {},
|
||||
(err) => Log.error(err),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -101,17 +101,6 @@ pub(crate) async fn update_field_handler(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn insert_field_handler(
|
||||
data: Data<InsertFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: InsertFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
||||
let _ = editor.insert_field(params).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
pub(crate) async fn update_field_type_option_handler(
|
||||
data: Data<UpdateFieldTypeOptionPayloadPB>,
|
||||
@ -140,27 +129,25 @@ pub(crate) async fn delete_field_handler(
|
||||
pub(crate) async fn switch_to_field_handler(
|
||||
data: Data<EditFieldPayloadPB>,
|
||||
manager: AppData<Arc<GridManager>>,
|
||||
) -> DataResult<FieldTypeOptionDataPB, FlowyError> {
|
||||
) -> Result<(), FlowyError> {
|
||||
let params: EditFieldParams = data.into_inner().try_into()?;
|
||||
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
||||
editor
|
||||
.switch_to_field_type(¶ms.field_id, ¶ms.field_type)
|
||||
.await?;
|
||||
|
||||
// Get the FieldMeta with field_id, if it doesn't exist, we create the default FieldMeta from the FieldType.
|
||||
// Get the field_rev with field_id, if it doesn't exist, we create the default FieldMeta from the FieldType.
|
||||
let field_rev = editor
|
||||
.get_field_rev(¶ms.field_id)
|
||||
.await
|
||||
.unwrap_or(Arc::new(editor.next_field_rev(¶ms.field_type).await?));
|
||||
|
||||
let type_option_data = get_type_option_data(&field_rev, ¶ms.field_type).await?;
|
||||
let data = FieldTypeOptionDataPB {
|
||||
grid_id: params.grid_id,
|
||||
field: field_rev.into(),
|
||||
type_option_data,
|
||||
};
|
||||
let _ = editor
|
||||
.update_field_type_option(¶ms.grid_id, &field_rev.id, type_option_data)
|
||||
.await?;
|
||||
|
||||
data_result(data)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "trace", skip(data, manager), err)]
|
||||
@ -227,7 +214,7 @@ pub(crate) async fn move_field_handler(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The FieldMeta contains multiple data, each of them belongs to a specific FieldType.
|
||||
/// The [FieldRevision] contains multiple data, each of them belongs to a specific FieldType.
|
||||
async fn get_type_option_data(field_rev: &FieldRevision, field_type: &FieldType) -> FlowyResult<Vec<u8>> {
|
||||
let s = field_rev.get_type_option_str(field_type).unwrap_or_else(|| {
|
||||
default_type_option_builder_from_type(field_type)
|
||||
|
@ -15,7 +15,6 @@ pub fn create(grid_manager: Arc<GridManager>) -> Module {
|
||||
// Field
|
||||
.event(GridEvent::GetFields, get_fields_handler)
|
||||
.event(GridEvent::UpdateField, update_field_handler)
|
||||
.event(GridEvent::InsertField, insert_field_handler)
|
||||
.event(GridEvent::UpdateFieldTypeOption, update_field_type_option_handler)
|
||||
.event(GridEvent::DeleteField, delete_field_handler)
|
||||
.event(GridEvent::SwitchToField, switch_to_field_handler)
|
||||
@ -106,11 +105,6 @@ pub enum GridEvent {
|
||||
#[event(input = "UpdateFieldTypeOptionPayloadPB")]
|
||||
UpdateFieldTypeOption = 12,
|
||||
|
||||
/// [InsertField] event is used to insert a new Field. If the Field already exists, the event
|
||||
/// handler will replace the value with the new Field value.
|
||||
#[event(input = "InsertFieldPayloadPB")]
|
||||
InsertField = 13,
|
||||
|
||||
/// [DeleteField] event is used to delete a Field. [DeleteFieldPayloadPB] is the context that
|
||||
/// is used to delete the field from the Grid.
|
||||
#[event(input = "DeleteFieldPayloadPB")]
|
||||
@ -119,7 +113,7 @@ pub enum GridEvent {
|
||||
/// [SwitchToField] event is used to update the current Field's type.
|
||||
/// It will insert a new FieldTypeOptionData if the new FieldType doesn't exist before, otherwise
|
||||
/// reuse the existing FieldTypeOptionData. You could check the [GridRevisionPad] for more details.
|
||||
#[event(input = "EditFieldPayloadPB", output = "FieldTypeOptionDataPB")]
|
||||
#[event(input = "EditFieldPayloadPB")]
|
||||
SwitchToField = 20,
|
||||
|
||||
/// [DuplicateField] event is used to duplicate a Field. The duplicated field data is kind of
|
||||
|
@ -105,7 +105,7 @@ impl GridRevisionEditor {
|
||||
frozen: Some(field.frozen),
|
||||
visibility: Some(field.visibility),
|
||||
width: Some(field.width),
|
||||
type_option_data: Some(type_option_data),
|
||||
type_option_data: None,
|
||||
};
|
||||
|
||||
let _ = self.update_field_rev(changeset, field.field_type).await?;
|
||||
|
Loading…
Reference in New Issue
Block a user