woocommerce-paypal-payments/modules/ppcp-wc-gateway/resources/js/common/display-manager/Rule.js
Emili Castells Guasch 11105d913b Run eslint autofix
2024-07-12 12:58:34 +02:00

69 lines
1.5 KiB
JavaScript

import ConditionFactory from './ConditionFactory';
import ActionFactory from './ActionFactory';
class Rule {
constructor( config, triggerUpdate ) {
this.config = config;
this.conditions = {};
this.actions = {};
this.triggerUpdate = triggerUpdate;
this.status = null;
const updateStatus = this.updateStatus.bind( this );
for ( const conditionConfig of this.config.conditions ) {
const condition = ConditionFactory.make(
conditionConfig,
updateStatus
);
this.conditions[ condition.key ] = condition;
//console.log('Condition', condition);
}
for ( const actionConfig of this.config.actions ) {
const action = ActionFactory.make( actionConfig );
this.actions[ action.key ] = action;
//console.log('Action', action);
}
}
get key() {
return this.config.key;
}
updateStatus( forceRunActions = false ) {
let status = true;
for ( const [ key, condition ] of Object.entries( this.conditions ) ) {
status &= condition.status;
}
if ( status !== this.status ) {
this.status = status;
this.triggerUpdate();
this.runActions();
} else if ( forceRunActions ) {
this.runActions();
}
}
runActions() {
for ( const [ key, action ] of Object.entries( this.actions ) ) {
action.run( this.status );
}
}
register() {
for ( const [ key, condition ] of Object.entries( this.conditions ) ) {
condition.register( this.updateStatus.bind( this ) );
}
for ( const [ key, action ] of Object.entries( this.actions ) ) {
action.register();
}
this.updateStatus( true );
}
}
export default Rule;