orc-shared 5.10.0-dev.2 → 5.10.0-dev.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -116,6 +116,16 @@ var useStyles = exports.useStyles = (0, _styles.makeStyles)(function (theme) {
116
116
  "& + $track": {
117
117
  backgroundColor: "".concat(theme.palette.grey.borders, " !important")
118
118
  }
119
+ },
120
+ container: {
121
+ display: "flex",
122
+ flexDirection: "column"
123
+ },
124
+ errorText: {
125
+ marginTop: theme.spacing(0.5),
126
+ color: theme.palette.error.main,
127
+ fontSize: theme.typography.fieldLabelSize,
128
+ float: "left"
119
129
  }
120
130
  };
121
131
  });
@@ -130,6 +140,7 @@ var Switch = function Switch(_ref) {
130
140
  var value = (switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.value)) || false;
131
141
  var onCaption = switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.onCaption);
132
142
  var offCaption = switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.offCaption);
143
+ var error = switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.error);
133
144
  var disabled = (switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.disabled)) || false;
134
145
  var readOnly = switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.readOnly);
135
146
  var className = (switchProps == null ? void 0 : switchProps.get(_SwitchProps.default.propNames.className)) || "";
@@ -149,7 +160,7 @@ var Switch = function Switch(_ref) {
149
160
  checked: classes.checked,
150
161
  disabled: classes.disabled
151
162
  }, className);
152
- return /*#__PURE__*/_react.default.createElement(_Switch.default, {
163
+ var switchComponent = /*#__PURE__*/_react.default.createElement(_Switch.default, {
153
164
  disabled: disabled,
154
165
  classes: switchClasses,
155
166
  checked: value,
@@ -158,6 +169,11 @@ var Switch = function Switch(_ref) {
158
169
  },
159
170
  color: "primary"
160
171
  });
172
+ return error && /*#__PURE__*/_react.default.createElement("div", {
173
+ className: classes.container
174
+ }, switchComponent, /*#__PURE__*/_react.default.createElement("div", {
175
+ className: (0, _classnames.default)(classes.errorText)
176
+ }, error)) || switchComponent;
161
177
  };
162
178
  __signature__(Switch, "useIntl{{ formatMessage }}\nuseStyles{classes}", function () {
163
179
  return [_reactIntl.useIntl, useStyles];
@@ -35,6 +35,7 @@ var SwitchProps = /*#__PURE__*/function (_ComponentProps) {
35
35
  _this.componentProps.set(_this.constructor.propNames.readOnly, null);
36
36
  _this.componentProps.set(_this.constructor.propNames.className, null);
37
37
  _this.componentProps.set(_this.constructor.propNames.id, null);
38
+ _this.componentProps.set(_this.constructor.propNames.error, null);
38
39
  _this.componentProps.set(_this.constructor.propNames.metadata, null);
39
40
  _this._isSwitchProps = true;
40
41
  return _this;
@@ -57,6 +58,7 @@ _defineProperty(SwitchProps, "propNames", {
57
58
  readOnly: "readOnly",
58
59
  className: "className",
59
60
  id: "id",
61
+ error: "error",
60
62
  metadata: "metadata"
61
63
  });
62
64
  var isSwitchProps = exports.isSwitchProps = function isSwitchProps(value) {
@@ -49,11 +49,14 @@ var useMultipleFieldEditState = function useMultipleFieldEditState(entityId, sec
49
49
  var dispatch = (0, _useDispatchWithModulesData.useDispatchWithModulesData)();
50
50
  var mergedValidationRules = _objectSpread(_objectSpread({}, _modelValidationHelper.validationRules), extendedValidationRules);
51
51
  var modifiedStates = (0, _reactRedux.useSelector)((0, _view.getModifiedModels)(entityId))[sectionName] || {};
52
- var useDynamicFieldState = function useDynamicFieldState(id, fieldName, errorTypes) {
52
+ var useDynamicFieldState = function useDynamicFieldState(id, fieldName, errorTypes, fieldDependencies) {
53
53
  var _initialValues$id$fie, _initialValues$id, _modifiedStates$id$fi, _modifiedStates$id, _editState$value;
54
54
  if (errorTypes === void 0) {
55
55
  errorTypes = [];
56
56
  }
57
+ if (fieldDependencies === void 0) {
58
+ fieldDependencies = {};
59
+ }
57
60
  var keys = [id, fieldName];
58
61
  var initialValue = (_initialValues$id$fie = (_initialValues$id = initialValues[id]) == null ? void 0 : _initialValues$id[fieldName]) != null ? _initialValues$id$fie : "";
59
62
  var editState = (_modifiedStates$id$fi = (_modifiedStates$id = modifiedStates[id]) == null ? void 0 : _modifiedStates$id[fieldName]) != null ? _modifiedStates$id$fi : {};
@@ -65,17 +68,23 @@ var useMultipleFieldEditState = function useMultipleFieldEditState(entityId, sec
65
68
  var resetEditState = function resetEditState() {
66
69
  dispatch(_view2.setEditModelField, [keys, initialValue, initialValue, entityId, sectionName]);
67
70
  };
68
- var isEditStateValid = function isEditStateValid(value) {
71
+ var isEditStateValid = function isEditStateValid(value, dependencies) {
72
+ if (dependencies === void 0) {
73
+ dependencies = {};
74
+ }
69
75
  var valueToValidate = value != null ? value : editState.value;
70
76
  var hasAnyValidationErrors = false;
71
77
  errorTypes.forEach(function (errorType) {
72
- var isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName);
78
+ var isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName, _objectSpread(_objectSpread({}, fieldDependencies), dependencies));
73
79
  if (isValid === false) {
74
80
  dispatch(_view2.setEditModelFieldError, [keys, errorType, entityId, sectionName]);
75
81
  hasAnyValidationErrors = true;
76
82
  return;
77
83
  }
78
84
  });
85
+ if (!hasAnyValidationErrors) {
86
+ dispatch(_view2.removeEditModelFieldError, [keys, entityId, sectionName]);
87
+ }
79
88
  return !hasAnyValidationErrors;
80
89
  };
81
90
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orc-shared",
3
- "version": "5.10.0-dev.2",
3
+ "version": "5.10.0-dev.3",
4
4
  "description": "Shared code for Orckestra applications",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
@@ -87,6 +87,16 @@ export const useStyles = makeStyles(theme => ({
87
87
  backgroundColor: `${theme.palette.grey.borders} !important`,
88
88
  },
89
89
  },
90
+ container: {
91
+ display: "flex",
92
+ flexDirection: "column",
93
+ },
94
+ errorText: {
95
+ marginTop: theme.spacing(0.5),
96
+ color: theme.palette.error.main,
97
+ fontSize: theme.typography.fieldLabelSize,
98
+ float: "left",
99
+ },
90
100
  }));
91
101
 
92
102
  const Switch = ({ switchProps }) => {
@@ -100,6 +110,7 @@ const Switch = ({ switchProps }) => {
100
110
  const value = switchProps?.get(SwitchProps.propNames.value) || false;
101
111
  const onCaption = switchProps?.get(SwitchProps.propNames.onCaption);
102
112
  const offCaption = switchProps?.get(SwitchProps.propNames.offCaption);
113
+ const error = switchProps?.get(SwitchProps.propNames.error);
103
114
  const disabled = switchProps?.get(SwitchProps.propNames.disabled) || false;
104
115
  const readOnly = switchProps?.get(SwitchProps.propNames.readOnly);
105
116
  const className = switchProps?.get(SwitchProps.propNames.className) || "";
@@ -120,7 +131,7 @@ const Switch = ({ switchProps }) => {
120
131
  ...className,
121
132
  };
122
133
 
123
- return (
134
+ const switchComponent = (
124
135
  <SwitchMui
125
136
  disabled={disabled}
126
137
  classes={switchClasses}
@@ -129,6 +140,16 @@ const Switch = ({ switchProps }) => {
129
140
  color={"primary"}
130
141
  />
131
142
  );
143
+
144
+ return (
145
+ (error && (
146
+ <div className={classes.container}>
147
+ {switchComponent}
148
+ <div className={classNames(classes.errorText)}>{error}</div>
149
+ </div>
150
+ )) ||
151
+ switchComponent
152
+ );
132
153
  };
133
154
 
134
155
  export default Switch;
@@ -60,6 +60,29 @@ describe("Switch Component", () => {
60
60
  expect(mountedComponent.containsMatchingElement(expected), "to be truthy");
61
61
  });
62
62
 
63
+ it("Renders Switch component with an error", () => {
64
+ const switchProps = new SwitchProps();
65
+
66
+ switchProps.set(SwitchProps.propNames.update, update);
67
+ switchProps.set(SwitchProps.propNames.value, true);
68
+ switchProps.set(SwitchProps.propNames.error, "an error");
69
+ switchProps.set(SwitchProps.propNames.onCaption, { id: "captionOn" });
70
+ switchProps.set(SwitchProps.propNames.offCaption, { id: "captionOff" });
71
+
72
+ const component = (
73
+ <IntlProvider messages={messages} locale="en-US">
74
+ <Switch switchProps={switchProps} />
75
+ </IntlProvider>
76
+ );
77
+
78
+ const mountedComponent = mount(component);
79
+ const expectedSwitch = <SwitchMUI checked={true} />;
80
+ const expectedError = <div>an error</div>;
81
+
82
+ expect(mountedComponent.containsMatchingElement(expectedSwitch), "to be truthy");
83
+ expect(mountedComponent.containsMatchingElement(expectedError), "to be truthy");
84
+ });
85
+
63
86
  it("Checkbox component handles check", () => {
64
87
  const switchProps = new SwitchProps();
65
88
 
@@ -10,6 +10,7 @@ class SwitchProps extends ComponentProps {
10
10
  readOnly: "readOnly",
11
11
  className: "className",
12
12
  id: "id",
13
+ error: "error",
13
14
  metadata: "metadata",
14
15
  };
15
16
 
@@ -23,6 +24,7 @@ class SwitchProps extends ComponentProps {
23
24
  this.componentProps.set(this.constructor.propNames.readOnly, null);
24
25
  this.componentProps.set(this.constructor.propNames.className, null);
25
26
  this.componentProps.set(this.constructor.propNames.id, null);
27
+ this.componentProps.set(this.constructor.propNames.error, null);
26
28
  this.componentProps.set(this.constructor.propNames.metadata, null);
27
29
 
28
30
  this._isSwitchProps = true;
@@ -11,6 +11,7 @@ describe("Switch Props", () => {
11
11
  "readOnly",
12
12
  "className",
13
13
  "id",
14
+ "error",
14
15
  "metadata",
15
16
  ];
16
17
 
@@ -27,6 +28,7 @@ describe("Switch Props", () => {
27
28
  "readOnly",
28
29
  "className",
29
30
  "id",
31
+ "error",
30
32
  "metadata",
31
33
  ];
32
34
 
@@ -1,7 +1,7 @@
1
1
  import { useSelector } from "react-redux";
2
2
  import { validationRules } from "../utils/modelValidationHelper";
3
3
  import { getModifiedModels } from "../selectors/view";
4
- import { setEditModelField, setEditModelFieldError } from "../actions/view";
4
+ import { setEditModelField, setEditModelFieldError, removeEditModelFieldError } from "../actions/view";
5
5
  import { useDispatchWithModulesData } from "./../hooks/useDispatchWithModulesData";
6
6
 
7
7
  /* This hook is used when a component has a dynamic number of fields that can be edited (e.g.: Orders' Custom Configuration Parameters).
@@ -25,7 +25,7 @@ const useMultipleFieldEditState = (entityId, sectionName, initialValues, extende
25
25
  const mergedValidationRules = { ...validationRules, ...extendedValidationRules };
26
26
  const modifiedStates = useSelector(getModifiedModels(entityId))[sectionName] || {};
27
27
 
28
- const useDynamicFieldState = (id, fieldName, errorTypes = []) => {
28
+ const useDynamicFieldState = (id, fieldName, errorTypes = [], fieldDependencies = {}) => {
29
29
  const keys = [id, fieldName];
30
30
 
31
31
  const initialValue = initialValues[id]?.[fieldName] ?? "";
@@ -42,12 +42,15 @@ const useMultipleFieldEditState = (entityId, sectionName, initialValues, extende
42
42
  dispatch(setEditModelField, [keys, initialValue, initialValue, entityId, sectionName]);
43
43
  };
44
44
 
45
- const isEditStateValid = value => {
45
+ const isEditStateValid = (value, dependencies = {}) => {
46
46
  const valueToValidate = value ?? editState.value;
47
47
 
48
48
  let hasAnyValidationErrors = false;
49
49
  errorTypes.forEach(errorType => {
50
- const isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName);
50
+ const isValid = mergedValidationRules[errorType](valueToValidate, id, fieldName, {
51
+ ...fieldDependencies,
52
+ ...dependencies,
53
+ });
51
54
 
52
55
  if (isValid === false) {
53
56
  dispatch(setEditModelFieldError, [keys, errorType, entityId, sectionName]);
@@ -57,6 +60,10 @@ const useMultipleFieldEditState = (entityId, sectionName, initialValues, extende
57
60
  }
58
61
  });
59
62
 
63
+ if (!hasAnyValidationErrors) {
64
+ dispatch(removeEditModelFieldError, [keys, entityId, sectionName]);
65
+ }
66
+
60
67
  return !hasAnyValidationErrors;
61
68
  };
62
69
 
@@ -5,7 +5,7 @@ import Immutable from "immutable";
5
5
  import sinon from "sinon";
6
6
  import { mount } from "enzyme";
7
7
  import * as useDispatchWithModulesDataMock from "./useDispatchWithModulesData";
8
- import { setEditModelField, setEditModelFieldError } from "./../actions/view";
8
+ import { removeEditModelFieldError, setEditModelField, setEditModelFieldError } from "./../actions/view";
9
9
  import { validationErrorTypes } from "./../constants";
10
10
  import _ from "lodash";
11
11
 
@@ -404,6 +404,54 @@ describe("useMultipleFieldEditState", () => {
404
404
  }
405
405
  });
406
406
 
407
+ it("Updates edit view value and reset error correctly with custom validation rules when validation was passed", () => {
408
+ const useDispatchWithModulesDataSpy = sinon.spy();
409
+ const useDispatchWithModulesDataStub = sinon
410
+ .stub(useDispatchWithModulesDataMock, "useDispatchWithModulesData")
411
+ .returns(useDispatchWithModulesDataSpy);
412
+
413
+ try {
414
+ // TODOJOC
415
+ const mountedComponent = mountComponent();
416
+
417
+ const fieldComponent = mountedComponent.find(`#id1-prop1-update`);
418
+
419
+ const event = {
420
+ target: {
421
+ value: "anotherValue",
422
+ },
423
+ };
424
+
425
+ fieldComponent.invoke("onClick")(event);
426
+
427
+ const resetEvent = {
428
+ target: {
429
+ value: "custom",
430
+ },
431
+ };
432
+
433
+ fieldComponent.invoke("onClick")(resetEvent);
434
+
435
+ const id = "id1";
436
+ const fieldName = "prop1";
437
+ const initialFieldValue = fieldInitialValues[id][fieldName];
438
+
439
+ expect(useDispatchWithModulesDataSpy, "to have a call satisfying", {
440
+ args: [setEditModelField, [[id, fieldName], "anotherValue", initialFieldValue, entityId, sectionName]],
441
+ });
442
+
443
+ expect(useDispatchWithModulesDataSpy, "to have a call satisfying", {
444
+ args: [setEditModelFieldError, [[id, fieldName], "customRule", entityId, sectionName]],
445
+ });
446
+
447
+ expect(useDispatchWithModulesDataSpy, "to have a call satisfying", {
448
+ args: [removeEditModelFieldError, [[id, fieldName], entityId, sectionName]],
449
+ });
450
+ } finally {
451
+ useDispatchWithModulesDataStub.restore();
452
+ }
453
+ });
454
+
407
455
  it.each([
408
456
  ["id1", "prop1"],
409
457
  ["id1", "prop2"],