Skip to content

Commit

Permalink
#18
Browse files Browse the repository at this point in the history
  • Loading branch information
mavarazy committed Jul 6, 2017
1 parent a5281fb commit ddec74f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
4 changes: 1 addition & 3 deletions src/actions/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export default class Executor {
executor(params, schema, uiSchema);
});

return new Promise(function(resolve) {
resolve({ schema, uiSchema });
});
return Promise.resolve({ schema, uiSchema });
};
}
9 changes: 5 additions & 4 deletions src/engine/PredicatesRuleEngine.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import validate from "./predicate/validation";
import applicableActions from "./predicate/applicableActions";
import { isDevelopment } from "../utils";

const engine = {
validate: (rules, schema) => {
validate(rules.map(({ conditions }) => conditions), schema);
},
run: (formData, rules, schema) => {
engine.validate(rules, schema);
return new Promise(function(resolve) {
resolve(applicableActions(rules, formData));
});
if (isDevelopment()) {
engine.validate(rules, schema);
}
return Promise.resolve(applicableActions(rules, formData));
},
};

Expand Down
76 changes: 38 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ export default function applyRules(FormComponent) {
constructor(props) {
super(props);

let { schema, rules, uiSchema, formData, extraActions } = this.props;

this.rulesExecutor = new Actions(rules, schema, uiSchema, extraActions);
let { schema, uiSchema, formData } = this.props;
this.state = { schema, uiSchema, formData };

let self = this;
this.props.rulesEngine
.run(formData, rules, schema)
.then(this.rulesExecutor.run)
.then(newState => {
self.setState(newState);
});
this.runRulesOnRender = true;
}

componentWillReceiveProps(nextProps) {
let { schema, formData, uiSchema } = nextProps;
this.setState({ schema, formData, uiSchema });
this.runRulesOnRender =
this.runRulesOnRender || !deepEqual(nextProps, this.props);
}

shouldComponentUpdate(nextProps, nextState) {
Expand All @@ -39,18 +33,25 @@ export default function applyRules(FormComponent) {
return !deepEqual(nextProps, this.props);
}

runRules = formData => {
let { rulesEngine, rules, schema, uiSchema, extraActions } = this.props;
let rulesExecutor = new Actions(rules, schema, uiSchema, extraActions);
console.log("Running rules");
return rulesEngine.run(formData, rules, schema).then(actions => {
console.log(`Received actions ${JSON.stringify(actions)}`);
return rulesExecutor.run(actions);
});
};

ruleTracker = state => {
let { formData } = state;
this.props.rulesEngine
.run(formData, this.props.rules, this.props.schema)
.then(this.rulesExecutor.run)
.then(newSchemaConf => {
this.notifySchemaUpdate(newSchemaConf, this.state);
this.setState(Object.assign(newSchemaConf, { formData }));
if (this.props.onChange) {
this.props.onChange(Object.assign({}, state, newSchemaConf));
}
});
this.runRules(formData).then(newSchemaConf => {
this.notifySchemaUpdate(newSchemaConf, this.state);
this.setState(Object.assign({}, newSchemaConf, { formData }));
if (this.props.onChange) {
this.props.onChange(Object.assign({}, state, newSchemaConf));
}
});
};

notifySchemaUpdate = (nextSchemaConf, schemaConf) => {
Expand All @@ -68,24 +69,23 @@ export default function applyRules(FormComponent) {
};

render() {
let configs = Object.assign({}, this.props);

delete configs.schema;
delete configs.formData;
delete configs.onChange;
delete configs.uiSchema;

this.props.rulesEngine.validate(this.props.rules, this.props.schema);

return (
<FormComponent
{...configs}
schema={this.state.schema}
uiSchema={this.state.uiSchema}
formData={this.state.formData}
onChange={this.ruleTracker}
/>
);
let { schema, uiSchema, formData } = this.state;
let configs = Object.assign({}, this.props, {
schema,
uiSchema,
formData,
onChange: this.ruleTracker,
});

if (this.runRulesOnRender) {
this.runRulesOnRender = false;
let self = this;
this.runRules(this.state.formData).then(newState =>
self.setState(newState)
);
}

return <FormComponent {...configs} />;
}
}

Expand Down

0 comments on commit ddec74f

Please sign in to comment.