Skip to content

Commit

Permalink
refactor: optimize mei validation
Browse files Browse the repository at this point in the history
- Move `validateMei` to `MeiTools.ts`
- Remove `setProcessStatus` and `setResultStatus`
- Refactor validation hooks

Resolves: #110
  • Loading branch information
yinanazhou committed Jul 12, 2024
1 parent 2d2001a commit e902b70
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
42 changes: 8 additions & 34 deletions src/Editor/CressTable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Handsontable from 'handsontable';
import { ImageTools } from './ImageTools';
import { MeiTools } from './MeiTools';
import { ValidationTools } from './ValidationTools';
import { ExportTools } from './ExportTools';
import { ColumnTools } from './ColumnTools';
import { updateAttachment } from '../Dashboard/Storage';
Expand All @@ -27,7 +26,6 @@ export class CressTable {
private images: any[] = []; // Array to store images
private imageTools: ImageTools;
private meiTools: MeiTools;
private validationTools: ValidationTools;
private exportTools: ExportTools;
private columnTools: ColumnTools;

Expand All @@ -37,7 +35,6 @@ export class CressTable {
// Initialize Toolss
this.imageTools = new ImageTools(this.images);
this.meiTools = new MeiTools();
this.validationTools = new ValidationTools();
this.exportTools = new ExportTools();
this.columnTools = new ColumnTools(inputHeader);

Expand Down Expand Up @@ -94,7 +91,9 @@ export class CressTable {
dropdownMenu: true,
className: 'table-menu-btn',
licenseKey: 'non-commercial-and-evaluation',
afterChange: (changes, source) => this.validateMei(changes, source),
afterLoadData: (_, initialLoad) => {
if (initialLoad) setTimeout(this.initValidationListener.bind(this), 0);
},
});

this.initFileListener(id, inputHeader, body, headers);
Expand Down Expand Up @@ -156,35 +155,10 @@ export class CressTable {
});
}

private validateMei(changes, source) {
if (source == 'loadData') {
// Validate mei data and update the validation status
this.meiTools.getMeiData().forEach((mei) => {
this.meiTools.setProcessStatus(mei);
this.validationTools
.meiValidator(mei.mei)
.then(([isValid, errorMsg]) => {
this.meiTools.updateMeiData(mei.row, mei.mei, isValid, errorMsg);
this.table.render();
this.meiTools.setResultStatus();
});
});
} else {
changes?.forEach(([row, prop, oldValue, newValue]) => {
if (prop === 'mei' && oldValue !== newValue) {
// validate the new edited mei data and update the validation status
this.meiTools.setProcessStatus(newValue);
this.meiTools.updateMeiData(row, newValue, undefined, undefined);
this.table.render();
this.validationTools
.meiValidator(newValue)
.then(([isValid, errorMsg]) => {
this.meiTools.updateMeiData(row, undefined, isValid, errorMsg);
this.table.render();
this.meiTools.setResultStatus();
});
}
});
}
private initValidationListener() {
this.meiTools.validateMei(this.table, 'afterLoadData');
this.table.addHook('afterChange', (changes, _) => {
this.meiTools.validateMei(this.table, 'afterChange', changes);
});
}
}
52 changes: 39 additions & 13 deletions src/Editor/MeiTools.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Handsontable from 'handsontable';
import { updateStatus } from './ValidationTools';
import { updateStatus, ValidationTools } from './ValidationTools';
import * as Notification from '../utils/Notification';
import { MeiData } from '../Types';

export class MeiTools {
private meiData: MeiData;
public validationInProgress = false;
public pendingValidations = 0;
private validationTools: ValidationTools;

constructor() {
this.validationTools = new ValidationTools();
this.meiData = [];
}

Expand Down Expand Up @@ -60,24 +62,48 @@ export class MeiTools {
}
}

public setProcessStatus(value: any) {
if (!this.validationInProgress) {
this.validationInProgress = true;
updateStatus('processing');
public validateMei(cressTable, hook, changes?) {
updateStatus('processing');

let validationPromises = [];

switch (hook) {
case 'afterLoadData':
this.getMeiData().forEach((mei) => {
if (mei) {
const validationPromise = this.validationTools
.meiValidator(mei.mei)
.then(([isValid, errorMsg]) => {
this.updateMeiData(mei.row, mei.mei, isValid, errorMsg);
});
validationPromises.push(validationPromise);
}
});
break;

case 'afterChange':
changes?.forEach(([row, prop, oldValue, newValue]) => {
if (prop === 'mei' && oldValue !== newValue && newValue) {
// validate the new edited mei data and update the validation status
this.updateMeiData(row, newValue, undefined, undefined);
const validationPromise = this.validationTools
.meiValidator(newValue)
.then(([isValid, errorMsg]) => {
this.updateMeiData(row, undefined, isValid, errorMsg);
});
validationPromises.push(validationPromise);
}
});
}
// Update `pendingValidations` if value is not empty
if (value) this.pendingValidations++;
}

public setResultStatus() {
this.pendingValidations--;
if (this.pendingValidations === 0) {
this.validationInProgress = false;
Promise.all(validationPromises).then(() => {
cressTable.render();

const hasInvalid = this.meiData.some(
(element) => element.isValid === false,
);
updateStatus('done', hasInvalid);
}
});
}

// Mei Renderer Functions
Expand Down

0 comments on commit e902b70

Please sign in to comment.