Skip to content

Commit

Permalink
feat(metadata-table): update operations (#6862)
Browse files Browse the repository at this point in the history
  • Loading branch information
renjie-run authored Oct 8, 2024
1 parent b3b75fe commit 7756f19
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
22 changes: 16 additions & 6 deletions frontend/src/metadata/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { EVENT_BUS_TYPE, PER_LOAD_NUMBER, PRIVATE_COLUMN_KEY } from '../constants';
import DataProcessor from './data-processor';
import ServerOperator from './server-operator';
import LocalOperator from './local-operator';
import Metadata from '../model/metadata';
import { checkIsDir } from '../utils/row';
import { Utils } from '../../utils/utils';
Expand All @@ -27,6 +28,7 @@ class Store {
this.isSendingOperation = false;
this.isReadonly = false;
this.serverOperator = new ServerOperator();
this.localOperator = new LocalOperator();
this.collaborators = props.collaborators || [];
}

Expand Down Expand Up @@ -108,17 +110,18 @@ class Store {

applyOperation(operation, undoRedoHandler = { handleUndo: true }) {
const { op_type } = operation;

if (LOCAL_APPLY_OPERATION_TYPE.includes(op_type)) {
this.localOperator.applyOperation(operation);
}

if (!NEED_APPLY_AFTER_SERVER_OPERATION.includes(op_type)) {
this.handleUndoRedos(undoRedoHandler, operation);
this.data = deepCopy(operation.apply(this.data));
this.syncOperationOnData(operation);
this.context.eventBus.dispatch(EVENT_BUS_TYPE.LOCAL_TABLE_CHANGED);
}

if (LOCAL_APPLY_OPERATION_TYPE.includes(op_type)) {
return;
}

this.addPendingOperations(operation, undoRedoHandler);
}

Expand Down Expand Up @@ -153,7 +156,9 @@ class Store {
this.sendNextOperation(undoRedoHandler);
return;
}
if (NEED_APPLY_AFTER_SERVER_OPERATION.includes(operation.op_type)) {

const isAfterServerOperation = NEED_APPLY_AFTER_SERVER_OPERATION.includes(operation.op_type);
if (isAfterServerOperation) {
this.handleUndoRedos(undoRedoHandler, operation);
this.data = deepCopy(operation.apply(this.data));
this.syncOperationOnData(operation);
Expand All @@ -163,7 +168,9 @@ class Store {
window.sfMetadataContext.eventBus.dispatch(EVENT_BUS_TYPE.VIEW_CHANGED, this.data.view);
}

this.context.eventBus.dispatch(EVENT_BUS_TYPE.SERVER_TABLE_CHANGED);
if (isAfterServerOperation) {
this.context.eventBus.dispatch(EVENT_BUS_TYPE.SERVER_TABLE_CHANGED);
}
operation.success_callback && operation.success_callback();

// need reload records if has related formula columns
Expand Down Expand Up @@ -335,10 +342,13 @@ class Store {
return;
}

const deleted_rows = valid_rows_ids.map((rowId) => getRowById(this.data, rowId));

const operation = this.createOperation({
type,
repo_id: this.repoId,
rows_ids: valid_rows_ids,
deleted_rows,
fail_callback,
success_callback,
});
Expand Down
27 changes: 27 additions & 0 deletions frontend/src/metadata/store/local-operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { OPERATION_TYPE } from './operations';

class LocalOperator {

applyOperation(operation) {
const { op_type } = operation;

switch (op_type) {
case OPERATION_TYPE.MODIFY_COLUMN_WIDTH: {
const { column_key, new_width } = operation;
try {
const oldValue = window.sfMetadataContext.localStorage.getItem('columns_width') || {};
window.sfMetadataContext.localStorage.setItem('columns_width', { ...oldValue, [column_key]: new_width });
} catch (err) {
break;
}
break;
}
default: {
break;
}
}
}

}

export default LocalOperator;
15 changes: 3 additions & 12 deletions frontend/src/metadata/store/operations/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const COLUMN_DATA_OPERATION_TYPE = {

export const OPERATION_ATTRIBUTES = {
[OPERATION_TYPE.MODIFY_RECORDS]: ['repo_id', 'row_ids', 'id_row_updates', 'id_original_row_updates', 'id_old_row_data', 'id_original_old_row_data', 'is_copy_paste', 'is_rename', 'id_obj_id'],
[OPERATION_TYPE.DELETE_RECORDS]: ['repo_id', 'rows_ids'],
[OPERATION_TYPE.DELETE_RECORDS]: ['repo_id', 'rows_ids', 'deleted_rows'],
[OPERATION_TYPE.RESTORE_RECORDS]: ['repo_id', 'rows_data', 'original_rows', 'link_infos', 'upper_row_ids'],
[OPERATION_TYPE.RELOAD_RECORDS]: ['repo_id', 'row_ids'],
[OPERATION_TYPE.MODIFY_FILTERS]: ['repo_id', 'view_id', 'filter_conjunction', 'filters', 'basic_filters'],
Expand All @@ -54,23 +54,14 @@ export const UNDO_OPERATION_TYPE = [

// only apply operation on the local
export const LOCAL_APPLY_OPERATION_TYPE = [

OPERATION_TYPE.MODIFY_COLUMN_WIDTH,
];

// apply operation after exec operation on the server
export const NEED_APPLY_AFTER_SERVER_OPERATION = [
OPERATION_TYPE.MODIFY_RECORDS,
OPERATION_TYPE.DELETE_RECORDS,
OPERATION_TYPE.INSERT_COLUMN,
OPERATION_TYPE.MODIFY_FILTERS,
OPERATION_TYPE.MODIFY_SORTS,
OPERATION_TYPE.MODIFY_GROUPBYS,
OPERATION_TYPE.MODIFY_HIDDEN_COLUMNS,
OPERATION_TYPE.INSERT_COLUMN,
OPERATION_TYPE.DELETE_COLUMN,
OPERATION_TYPE.RENAME_COLUMN,
OPERATION_TYPE.MODIFY_COLUMN_DATA,
OPERATION_TYPE.MODIFY_COLUMN_WIDTH,
OPERATION_TYPE.MODIFY_COLUMN_ORDER,
];

export const VIEW_OPERATION = [
Expand Down
33 changes: 12 additions & 21 deletions frontend/src/metadata/store/server-operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class ServerOperator {

switch (op_type) {
case OPERATION_TYPE.MODIFY_RECORDS: {
const { repo_id, row_ids, id_row_updates, id_original_row_updates, is_copy_paste, is_rename, id_obj_id } = operation;
const { repo_id, row_ids, id_row_updates, id_original_row_updates, id_original_old_row_data, is_copy_paste, is_rename, id_obj_id } = operation;
if (is_rename) {
const rowId = row_ids[0];
const oldRowData = id_original_old_row_data[rowId];
const rowUpdates = id_original_row_updates[rowId];
const oldName = getFileNameFromRecord(oldRowData);
const newName = getFileNameFromRecord(rowUpdates);
this.renameFile(newName, repo_id, rowId, data, {
this.renameFile(newName, oldName, repo_id, rowId, data, {
fail_callback: (error) => {
callback({ error });
},
Expand All @@ -42,9 +44,8 @@ class ServerOperator {
break;
}
case OPERATION_TYPE.DELETE_RECORDS: {
const { repo_id, rows_ids } = operation;
const file_names = rows_ids.map((rowId) => {
const row = getRowById(data, rowId);
const { repo_id, deleted_rows } = operation;
const file_names = deleted_rows.map((row) => {
const { _parent_dir, _name } = row || {};
if (_parent_dir && _name) {
return Utils.joinPath(_parent_dir, _name);
Expand Down Expand Up @@ -112,17 +113,6 @@ class ServerOperator {
});
break;
}
case OPERATION_TYPE.MODIFY_COLUMN_WIDTH: {
const { column_key, new_width } = operation;
try {
const oldValue = window.sfMetadataContext.localStorage.getItem('columns_width') || {};
window.sfMetadataContext.localStorage.setItem('columns_width', { ...oldValue, [column_key]: new_width });
callback({ operation });
} catch {
callback({ error: gettext('Failed to modify property width') });
}
break;
}
case OPERATION_TYPE.MODIFY_COLUMN_ORDER: {
const { repo_id, view_id, new_columns_keys } = operation;
window.sfMetadataContext.modifyView(repo_id, view_id, { columns_keys: new_columns_keys }).then(res => {
Expand Down Expand Up @@ -334,14 +324,15 @@ class ServerOperator {
}, []);
}

renameFile = (newName, repo_id, rowId, data, { fail_callback, success_callback }) => {
renameFile = (newName, oldName, repo_id, rowId, data, { fail_callback, success_callback }) => {
const row = getRowById(data, rowId);
if (!row) {
fail_callback();
return;
}

const { _parent_dir, _name } = row;
const path = Utils.joinPath(_parent_dir, _name);
const { _parent_dir } = row;
const path = Utils.joinPath(_parent_dir, oldName);

// rename folder
if (checkIsDir(row)) {
Expand All @@ -350,7 +341,7 @@ class ServerOperator {
}).catch((error) => {
let errMessage = Utils.getErrorMsg(error);
if (errMessage === gettext('Error')) {
errMessage = gettext('Renaming {name} failed').replace('{name}', _name);
errMessage = gettext('Renaming {name} failed').replace('{name}', oldName);
}
fail_callback(errMessage);
});
Expand All @@ -368,7 +359,7 @@ class ServerOperator {
errMessage = Utils.getErrorMsg(error);
}
if (errMessage === gettext('Error')) {
errMessage = gettext('Renaming {name} failed').replace('{name}', _name);
errMessage = gettext('Renaming {name} failed').replace('{name}', oldName);
}
fail_callback(errMessage);
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/metadata/views/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const Table = () => {
}
store.modifyRecords(rowIds, idRowUpdates, idOriginalRowUpdates, idOldRowData, idOriginalOldRowData, isCopyPaste, isRename, {
fail_callback: (error) => {
toaster.danger(error);
error && toaster.danger(error);
},
success_callback: () => {
if (isRename) {
Expand Down

0 comments on commit 7756f19

Please sign in to comment.