@knovator/pagecreator-admin 0.5.12 → 0.7.0

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.
package/index.js CHANGED
@@ -6,8 +6,8 @@ import { useFieldArray, useForm, Controller } from 'react-hook-form';
6
6
  import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
7
7
  import CSSTransition from 'react-transition-group/CSSTransition';
8
8
  import { useDropzone } from 'react-dropzone';
9
- import { Tabs as Tabs$1, TabList, Tab, TabPanel } from 'react-tabs';
10
9
  import { Popover } from 'react-tiny-popover';
10
+ import { Tabs as Tabs$1, TabList, Tab, TabPanel } from 'react-tabs';
11
11
 
12
12
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
13
13
 
@@ -1634,6 +1634,18 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1634
1634
  PERFORMANCE OF THIS SOFTWARE.
1635
1635
  ***************************************************************************** */
1636
1636
 
1637
+ function __rest(s, e) {
1638
+ var t = {};
1639
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
1640
+ t[p] = s[p];
1641
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
1642
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
1643
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
1644
+ t[p[i]] = s[p[i]];
1645
+ }
1646
+ return t;
1647
+ }
1648
+
1637
1649
  function __awaiter(thisArg, _arguments, P, generator) {
1638
1650
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1639
1651
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -1662,6 +1674,11 @@ var CALLBACK_CODES;
1662
1674
  CALLBACK_CODES["IMAGE_REMOVE"] = "IMAGE_REMOVE";
1663
1675
  CALLBACK_CODES["INTERNAL"] = "INTERNAL";
1664
1676
  })(CALLBACK_CODES || (CALLBACK_CODES = {}));
1677
+ const CONSTANTS = {
1678
+ EMPTY_REGEX: / /g,
1679
+ SLUG_REGEX: /^[\da-z-]*$/,
1680
+ SLUG_REPLACE_REGEX: /[^\da-z-]/gi
1681
+ };
1665
1682
  const DEFAULT_PERMISSIONS = {
1666
1683
  list: true,
1667
1684
  add: true,
@@ -1743,7 +1760,15 @@ const TRANSLATION_PAIRS_ITEM = {
1743
1760
  'item.uploadFile': 'Upload File',
1744
1761
  'item.dragDrop': 'or drag and drop',
1745
1762
  'item.allowedFormat': 'PNG, JPG, SVG up to 2 MB',
1746
- 'item.srcset': 'Srcset'
1763
+ 'item.srcset': 'Srcset',
1764
+ 'item.screenSizeRequired': 'ScreenSize is required',
1765
+ 'item.widthRequired': 'Width is required',
1766
+ 'item.heightRequired': 'Height is required',
1767
+ 'item.minScreenSize': 'ScreenSize should be greater than 0',
1768
+ 'item.minWidth': 'Width should be greater than 0',
1769
+ 'item.minHeight': 'Height should be greater than 0',
1770
+ 'item.titleRequired': 'Title is required',
1771
+ 'item.deleteTitle': 'Are you sure you want to delete this item?'
1747
1772
  };
1748
1773
  const TRANSLATION_PAIRS_PAGE = {
1749
1774
  'page.name': 'Name',
@@ -1752,6 +1777,9 @@ const TRANSLATION_PAIRS_PAGE = {
1752
1777
  'page.code': 'Code',
1753
1778
  'page.codePlaceholder': 'Enter code',
1754
1779
  'page.codeRequired': 'Code is required',
1780
+ 'page.slug': 'Slug',
1781
+ 'page.slugPlaceholder': 'Enter Slug',
1782
+ 'page.slugRequired': 'Slug is required',
1755
1783
  'page.addPageTitle': 'Add Page',
1756
1784
  'page.updatePageTitle': 'Update Page',
1757
1785
  'page.searchPages': 'Search Pages...',
@@ -1771,7 +1799,6 @@ const Provider = ({
1771
1799
  onSuccess: _onSuccess = () => {},
1772
1800
  onLogout: _onLogout = () => {},
1773
1801
  switchClass: _switchClass = 'khb_switch',
1774
- itemsRoutesPrefix: _itemsRoutesPrefix = 'items',
1775
1802
  widgetRoutesPrefix: _widgetRoutesPrefix = 'widgets',
1776
1803
  pageRoutesPrefix: _pageRoutesPrefix = 'pages'
1777
1804
  }) => {
@@ -1783,7 +1810,6 @@ const Provider = ({
1783
1810
  onSuccess: _onSuccess,
1784
1811
  onLogout: _onLogout,
1785
1812
  switchClass: _switchClass,
1786
- itemsRoutesPrefix: _itemsRoutesPrefix,
1787
1813
  widgetRoutesPrefix: _widgetRoutesPrefix,
1788
1814
  pageRoutesPrefix: _pageRoutesPrefix
1789
1815
  }
@@ -2482,7 +2508,11 @@ const capitalizeFirstLetter = (string = '') => {
2482
2508
  };
2483
2509
  const changeToCode = (string = '') => {
2484
2510
  var _a, _b;
2485
- return (_b = (_a = string.replace(/[^\s\w]/gi, '')) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === null || _b === void 0 ? void 0 : _b.replace(/ /g, '_');
2511
+ return (_b = (_a = string.replace(/[^\s\w]/gi, '')) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === null || _b === void 0 ? void 0 : _b.replace(CONSTANTS.EMPTY_REGEX, '_');
2512
+ };
2513
+ const changeToSlug = (string = '') => {
2514
+ var _a;
2515
+ return (_a = string === null || string === void 0 ? void 0 : string.toLowerCase()) === null || _a === void 0 ? void 0 : _a.replace(CONSTANTS.EMPTY_REGEX, '-');
2486
2516
  };
2487
2517
  const isObject$3 = data => {
2488
2518
  var _a;
@@ -2584,6 +2614,13 @@ const usePagination = ({
2584
2614
  };
2585
2615
 
2586
2616
  const apiList = {
2617
+ GET_ONE: ({
2618
+ prefix,
2619
+ id
2620
+ }) => ({
2621
+ url: `${prefix}/${id}`,
2622
+ method: 'GET'
2623
+ }),
2587
2624
  LIST: ({
2588
2625
  prefix
2589
2626
  }) => ({
@@ -3327,6 +3364,45 @@ const CustomReactSelect = ({
3327
3364
  }, error));
3328
3365
  };
3329
3366
 
3367
+ var call$7 = functionCall;
3368
+ var hasOwn$1 = hasOwnProperty_1;
3369
+ var isPrototypeOf$2 = objectIsPrototypeOf;
3370
+ var regExpFlags = regexpFlags$1;
3371
+
3372
+ var RegExpPrototype$1 = RegExp.prototype;
3373
+
3374
+ var regexpGetFlags = function (R) {
3375
+ var flags = R.flags;
3376
+ return flags === undefined && !('flags' in RegExpPrototype$1) && !hasOwn$1(R, 'flags') && isPrototypeOf$2(RegExpPrototype$1, R)
3377
+ ? call$7(regExpFlags, R) : flags;
3378
+ };
3379
+
3380
+ var PROPER_FUNCTION_NAME = functionName.PROPER;
3381
+ var defineBuiltIn$2 = defineBuiltIn$7;
3382
+ var anObject$5 = anObject$e;
3383
+ var $toString = toString$6;
3384
+ var fails$3 = fails$n;
3385
+ var getRegExpFlags = regexpGetFlags;
3386
+
3387
+ var TO_STRING = 'toString';
3388
+ var RegExpPrototype = RegExp.prototype;
3389
+ var nativeToString = RegExpPrototype[TO_STRING];
3390
+
3391
+ var NOT_GENERIC = fails$3(function () { return nativeToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
3392
+ // FF44- RegExp#toString has a wrong name
3393
+ var INCORRECT_NAME = PROPER_FUNCTION_NAME && nativeToString.name != TO_STRING;
3394
+
3395
+ // `RegExp.prototype.toString` method
3396
+ // https://tc39.es/ecma262/#sec-regexp.prototype.tostring
3397
+ if (NOT_GENERIC || INCORRECT_NAME) {
3398
+ defineBuiltIn$2(RegExp.prototype, TO_STRING, function toString() {
3399
+ var R = anObject$5(this);
3400
+ var pattern = $toString(R.source);
3401
+ var flags = $toString(getRegExpFlags(R));
3402
+ return '/' + pattern + '/' + flags;
3403
+ }, { unsafe: true });
3404
+ }
3405
+
3330
3406
  const Close = ({
3331
3407
  srText,
3332
3408
  className
@@ -3368,27 +3444,19 @@ const Plus = ({
3368
3444
  const SrcSetInput = ({
3369
3445
  size,
3370
3446
  className,
3371
- index,
3372
- register,
3373
- errors,
3374
- name,
3375
- inputKey,
3447
+ rest,
3448
+ error,
3376
3449
  placeholder,
3377
3450
  disabled: _disabled = false
3378
3451
  }) => {
3379
- var _a, _b;
3380
3452
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("input", Object.assign({
3381
3453
  className: classNames('khb_input', `khb_input-${size}`, className),
3382
3454
  type: 'number',
3383
3455
  placeholder: placeholder,
3384
- name: `srcset[${index}].${inputKey}`,
3385
3456
  disabled: _disabled
3386
- }, register(`srcset[${index}].${inputKey}`, {
3387
- required: `${name} Size is required`,
3388
- validate: value => Number(value) <= 0 ? `${inputKey} should be greater than 0` : true
3389
- }))), Array.isArray(errors) && errors[index] && /*#__PURE__*/React.createElement("p", {
3457
+ }, rest || {})), error && /*#__PURE__*/React.createElement("p", {
3390
3458
  className: "khb_input-error"
3391
- }, (_b = (_a = errors[index]) === null || _a === void 0 ? void 0 : _a[inputKey]) === null || _b === void 0 ? void 0 : _b['message']));
3459
+ }, error));
3392
3460
  };
3393
3461
  const SrcSet = ({
3394
3462
  label,
@@ -3400,7 +3468,9 @@ const SrcSet = ({
3400
3468
  wrapperClassName,
3401
3469
  control,
3402
3470
  errors,
3403
- disabled: _disabled2 = false
3471
+ name,
3472
+ disabled: _disabled2 = false,
3473
+ t
3404
3474
  }) => {
3405
3475
  const {
3406
3476
  fields,
@@ -3408,7 +3478,7 @@ const SrcSet = ({
3408
3478
  remove
3409
3479
  } = useFieldArray({
3410
3480
  control,
3411
- name: 'srcset'
3481
+ name: name
3412
3482
  });
3413
3483
  return /*#__PURE__*/React.createElement("div", {
3414
3484
  className: classNames('khb_input-wrapper', wrapperClassName)
@@ -3418,49 +3488,55 @@ const SrcSet = ({
3418
3488
  className: "khb_input-label-required"
3419
3489
  }, "*") : null), /*#__PURE__*/React.createElement("div", {
3420
3490
  className: "khb_input-srcset-items"
3421
- }, fields === null || fields === void 0 ? void 0 : fields.map((item, index) => /*#__PURE__*/React.createElement("div", {
3422
- key: index,
3423
- className: "khb_input-srcset"
3424
- }, /*#__PURE__*/React.createElement(SrcSetInput, {
3425
- errors: errors,
3426
- index: index,
3427
- className: className,
3428
- inputKey: "screenSize",
3429
- name: "Screen Size",
3430
- size: _size,
3431
- placeholder: "Screen Size",
3432
- register: register,
3433
- disabled: _disabled2
3434
- }), /*#__PURE__*/React.createElement("span", {
3435
- className: "p-2"
3436
- }, "="), /*#__PURE__*/React.createElement(SrcSetInput, {
3437
- errors: errors,
3438
- index: index,
3439
- register: register,
3440
- className: className,
3441
- inputKey: "width",
3442
- name: "Width",
3443
- size: _size,
3444
- placeholder: "Width",
3445
- disabled: _disabled2
3446
- }), /*#__PURE__*/React.createElement("span", {
3447
- className: "p-2"
3448
- }, "x"), /*#__PURE__*/React.createElement(SrcSetInput, {
3449
- errors: errors,
3450
- index: index,
3451
- register: register,
3452
- className: className,
3453
- inputKey: "height",
3454
- name: "Height",
3455
- size: _size,
3456
- placeholder: "Height",
3457
- disabled: _disabled2
3491
+ }, fields === null || fields === void 0 ? void 0 : fields.map((item, index) => {
3492
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3493
+ return /*#__PURE__*/React.createElement("div", {
3494
+ key: index,
3495
+ className: "khb_input-srcset"
3496
+ }, /*#__PURE__*/React.createElement(SrcSetInput, {
3497
+ error: (_c = (_b = (_a = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _a === void 0 ? void 0 : _a['screenSize']) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.toString(),
3498
+ index: index,
3499
+ className: className,
3500
+ size: _size,
3501
+ placeholder: "Screen Size",
3502
+ rest: register(`${name}.${index}.screenSize`, {
3503
+ required: t('item.screenSizeRequired'),
3504
+ validate: value => Number(value) <= 0 ? t('item.minScreenSize') : true
3505
+ }),
3506
+ disabled: _disabled2
3507
+ }), /*#__PURE__*/React.createElement("span", {
3508
+ className: "p-2"
3509
+ }, "="), /*#__PURE__*/React.createElement(SrcSetInput, {
3510
+ error: (_f = (_e = (_d = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _d === void 0 ? void 0 : _d['width']) === null || _e === void 0 ? void 0 : _e.message) === null || _f === void 0 ? void 0 : _f.toString(),
3511
+ index: index,
3512
+ rest: register(`${name}.${index}.width`, {
3513
+ required: t('item.widthRequired'),
3514
+ validate: value => Number(value) <= 0 ? t('item.minWidth') : true
3515
+ }),
3516
+ className: className,
3517
+ size: _size,
3518
+ placeholder: "Width",
3519
+ disabled: _disabled2
3520
+ }), /*#__PURE__*/React.createElement("span", {
3521
+ className: "p-2"
3522
+ }, "x"), /*#__PURE__*/React.createElement(SrcSetInput, {
3523
+ error: (_j = (_h = (_g = errors === null || errors === void 0 ? void 0 : errors[index]) === null || _g === void 0 ? void 0 : _g['height']) === null || _h === void 0 ? void 0 : _h.message) === null || _j === void 0 ? void 0 : _j.toString(),
3524
+ index: index,
3525
+ rest: register(`${name}.${index}.height`, {
3526
+ required: t('item.heightRequired'),
3527
+ validate: value => Number(value) <= 0 ? t('item.minHeight') : true
3528
+ }),
3529
+ className: className,
3530
+ size: _size,
3531
+ placeholder: "Height",
3532
+ disabled: _disabled2
3533
+ }), /*#__PURE__*/React.createElement("button", {
3534
+ type: "button",
3535
+ className: "khb_btn khb_btn-danger khb_btn-xs",
3536
+ onClick: () => remove(index),
3537
+ disabled: _disabled2
3538
+ }, /*#__PURE__*/React.createElement(Close, null)));
3458
3539
  }), /*#__PURE__*/React.createElement("button", {
3459
- type: "button",
3460
- className: "khb_btn khb_btn-danger khb_btn-xs",
3461
- onClick: () => remove(index),
3462
- disabled: _disabled2
3463
- }, /*#__PURE__*/React.createElement(Close, null)))), /*#__PURE__*/React.createElement("button", {
3464
3540
  type: "button",
3465
3541
  disabled: _disabled2,
3466
3542
  onClick: () => append({
@@ -3470,7 +3546,7 @@ const SrcSet = ({
3470
3546
  }),
3471
3547
  className: "khb_btn khb_btn-primary khb_btn-xs"
3472
3548
  }, /*#__PURE__*/React.createElement(Plus, {
3473
- className: "w-7 h-7"
3549
+ className: "khb_srcset-remove"
3474
3550
  }))), error && /*#__PURE__*/React.createElement("p", {
3475
3551
  className: "khb_input-error "
3476
3552
  }, error));
@@ -3509,45 +3585,6 @@ const PageSearch = () => {
3509
3585
  });
3510
3586
  };
3511
3587
 
3512
- var call$7 = functionCall;
3513
- var hasOwn$1 = hasOwnProperty_1;
3514
- var isPrototypeOf$2 = objectIsPrototypeOf;
3515
- var regExpFlags = regexpFlags$1;
3516
-
3517
- var RegExpPrototype$1 = RegExp.prototype;
3518
-
3519
- var regexpGetFlags = function (R) {
3520
- var flags = R.flags;
3521
- return flags === undefined && !('flags' in RegExpPrototype$1) && !hasOwn$1(R, 'flags') && isPrototypeOf$2(RegExpPrototype$1, R)
3522
- ? call$7(regExpFlags, R) : flags;
3523
- };
3524
-
3525
- var PROPER_FUNCTION_NAME = functionName.PROPER;
3526
- var defineBuiltIn$2 = defineBuiltIn$7;
3527
- var anObject$5 = anObject$e;
3528
- var $toString = toString$6;
3529
- var fails$3 = fails$n;
3530
- var getRegExpFlags = regexpGetFlags;
3531
-
3532
- var TO_STRING = 'toString';
3533
- var RegExpPrototype = RegExp.prototype;
3534
- var nativeToString = RegExpPrototype[TO_STRING];
3535
-
3536
- var NOT_GENERIC = fails$3(function () { return nativeToString.call({ source: 'a', flags: 'b' }) != '/a/b'; });
3537
- // FF44- RegExp#toString has a wrong name
3538
- var INCORRECT_NAME = PROPER_FUNCTION_NAME && nativeToString.name != TO_STRING;
3539
-
3540
- // `RegExp.prototype.toString` method
3541
- // https://tc39.es/ecma262/#sec-regexp.prototype.tostring
3542
- if (NOT_GENERIC || INCORRECT_NAME) {
3543
- defineBuiltIn$2(RegExp.prototype, TO_STRING, function toString() {
3544
- var R = anObject$5(this);
3545
- var pattern = $toString(R.source);
3546
- var flags = $toString(getRegExpFlags(R));
3547
- return '/' + pattern + '/' + flags;
3548
- }, { unsafe: true });
3549
- }
3550
-
3551
3588
  var aCallable$6 = aCallable$8;
3552
3589
  var toObject = toObject$5;
3553
3590
  var IndexedObject = indexedObject;
@@ -4023,6 +4060,16 @@ const PageForm = ({
4023
4060
  event.target.value = changeToCode(event.target.value);
4024
4061
  return event;
4025
4062
  }
4063
+ function handleSlug(event) {
4064
+ let slugValue = changeToSlug(event.target.value);
4065
+ if (!slugValue || !CONSTANTS.SLUG_REGEX.test(slugValue)) {
4066
+ slugValue = '';
4067
+ } else {
4068
+ slugValue = slugValue.replace(CONSTANTS.SLUG_REPLACE_REGEX, '');
4069
+ }
4070
+ event.target.value = slugValue;
4071
+ return event;
4072
+ }
4026
4073
  function loadOptions(value, callback) {
4027
4074
  getWidgets(widgetsData => {
4028
4075
  if (callback) callback(widgetsData);
@@ -4059,6 +4106,17 @@ const PageForm = ({
4059
4106
  validations: {
4060
4107
  required: t('page.codeRequired')
4061
4108
  }
4109
+ }, {
4110
+ label: `${t('page.slug')}`,
4111
+ accessor: 'slug',
4112
+ required: true,
4113
+ type: 'text',
4114
+ onInput: handleSlug,
4115
+ editable: false,
4116
+ placeholder: t('page.slugPlaceholder'),
4117
+ validations: {
4118
+ required: t('page.slugRequired')
4119
+ }
4062
4120
  }, {
4063
4121
  label: t('page.widgets'),
4064
4122
  accessor: 'widgets',
@@ -4093,13 +4151,23 @@ const Button = ({
4093
4151
  onClick,
4094
4152
  className,
4095
4153
  children,
4096
- disabled
4154
+ disabled,
4155
+ loading
4097
4156
  }) => {
4098
4157
  return /*#__PURE__*/React.createElement("button", {
4099
4158
  className: classNames('khb_btn', `khb_btn-${_type}`, `khb_btn-${_size}`, className),
4100
4159
  onClick: onClick,
4101
- disabled: disabled
4102
- }, children);
4160
+ disabled: disabled || loading
4161
+ }, loading && /*#__PURE__*/React.createElement("svg", {
4162
+ className: "khb_btn-loader",
4163
+ xmlns: "http://www.w3.org/2000/svg",
4164
+ width: "12",
4165
+ height: "13",
4166
+ viewBox: "0 0 12 13"
4167
+ }, /*#__PURE__*/React.createElement("path", {
4168
+ // style={{ fill: kind }}
4169
+ d: "M5.5,12a1,1,0,1,1,1,1A1,1,0,0,1,5.5,12ZM1.75,10.5A1.25,1.25,0,1,1,3,11.75,1.25,1.25,0,0,1,1.75,10.5Zm7.531.031a.75.75,0,1,1,.75.75A.75.75,0,0,1,9.281,10.531ZM0,7A1.5,1.5,0,1,1,1.5,8.5,1.5,1.5,0,0,1,0,7ZM11,7a.5.5,0,1,1,.5.5A.5.5,0,0,1,11,7ZM1.875,4.637a1.62,1.62,0,0,1,0-2.275,1.582,1.582,0,0,1,2.253,0,1.62,1.62,0,0,1,0,2.275,1.582,1.582,0,0,1-2.253,0ZM4.5,2a2,2,0,1,1,2,2A2,2,0,0,1,4.5,2ZM9.75,3.5a.25.25,0,1,1,.25.25A.25.25,0,0,1,9.75,3.5Z"
4170
+ })), children);
4103
4171
  };
4104
4172
 
4105
4173
  const AddButton$1 = () => {
@@ -4402,7 +4470,7 @@ const FormActions = ({
4402
4470
  onClick: onSecondaryButtonClick
4403
4471
  }, _secondaryLabel), /*#__PURE__*/React.createElement(Button, {
4404
4472
  onClick: onPrimaryButtonClick,
4405
- disabled: _loading
4473
+ loading: _loading
4406
4474
  }, _primaryLabel));
4407
4475
  };
4408
4476
 
@@ -4566,11 +4634,6 @@ const useWidget = ({
4566
4634
  imageBaseUrl
4567
4635
  }) => {
4568
4636
  const [list, setList] = useState([]);
4569
- const [itemsList, setItemsList] = useState({
4570
- web: [],
4571
- mobile: []
4572
- });
4573
- const [itemsLoading, setItemsLoading] = useState(false);
4574
4637
  const [loading, setLoading] = useState(false);
4575
4638
  const [totalPages, setTotalPages] = useState(0);
4576
4639
  const [totalRecords, setTotalRecords] = useState(0);
@@ -4586,8 +4649,7 @@ const useWidget = ({
4586
4649
  onError,
4587
4650
  onSuccess,
4588
4651
  onLogout,
4589
- widgetRoutesPrefix,
4590
- itemsRoutesPrefix
4652
+ widgetRoutesPrefix
4591
4653
  } = useProviderState();
4592
4654
  const {
4593
4655
  changeSearch,
@@ -4645,67 +4707,6 @@ const useWidget = ({
4645
4707
  setLoading(false);
4646
4708
  }
4647
4709
  }), [baseUrl, currentPageRef, limitRef, offsetRef, handleError, routes, token, widgetRoutesPrefix]);
4648
- const getItems = useCallback(id => __awaiter(void 0, void 0, void 0, function* () {
4649
- try {
4650
- setItemsLoading(true);
4651
- const api = getApiType({
4652
- routes,
4653
- action: 'ITEM',
4654
- prefix: itemsRoutesPrefix,
4655
- id
4656
- });
4657
- const response = yield commonApi({
4658
- baseUrl,
4659
- token,
4660
- method: api.method,
4661
- url: api.url,
4662
- onError: handleError(CALLBACK_CODES.GET_ALL)
4663
- });
4664
- if ((response === null || response === void 0 ? void 0 : response.code) === 'SUCCESS') {
4665
- setItemsLoading(false);
4666
- const itemsResponse = dataGatter(response).reduce((acc, itemItem) => {
4667
- if (itemItem['itemType'] === 'Web') acc.web.push(itemItem);else acc.mobile.push(itemItem);
4668
- return acc;
4669
- }, {
4670
- web: [],
4671
- mobile: []
4672
- });
4673
- return setItemsList(itemsResponse);
4674
- }
4675
- setItemsLoading(false);
4676
- } catch (error) {
4677
- setItemsLoading(false);
4678
- }
4679
- }), [baseUrl, handleError, routes, itemsRoutesPrefix, token]);
4680
- const onDeleteItem = id => __awaiter(void 0, void 0, void 0, function* () {
4681
- try {
4682
- setItemsLoading(true);
4683
- const api = getApiType({
4684
- routes,
4685
- action: 'DELETE',
4686
- prefix: itemsRoutesPrefix,
4687
- id
4688
- });
4689
- const response = yield commonApi({
4690
- baseUrl,
4691
- token,
4692
- method: api.method,
4693
- url: api.url,
4694
- onError: handleError(CALLBACK_CODES.DELETE)
4695
- });
4696
- if ((response === null || response === void 0 ? void 0 : response.code) === 'SUCCESS') {
4697
- setItemsLoading(false);
4698
- onSuccess(CALLBACK_CODES.DELETE, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4699
- if (itemData) getItems(itemData['_id']);
4700
- return;
4701
- }
4702
- setItemsLoading(false);
4703
- onError(CALLBACK_CODES.DELETE, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4704
- } catch (error) {
4705
- setItemsLoading(false);
4706
- onError(CALLBACK_CODES.DELETE, INTERNAL_ERROR_CODE, error.message);
4707
- }
4708
- });
4709
4710
  const onCofirmDeleteWidget = () => __awaiter(void 0, void 0, void 0, function* () {
4710
4711
  try {
4711
4712
  let proceed = true;
@@ -4768,6 +4769,7 @@ const useWidget = ({
4768
4769
  });
4769
4770
  if ((response === null || response === void 0 ? void 0 : response.code) === 'SUCCESS') {
4770
4771
  setList(oldListData => oldListData.map(item => item['_id'] === id ? response.data : item));
4772
+ if (response.message) onSuccess(CALLBACK_CODES.PARTIAL_UPDATE, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4771
4773
  } else {
4772
4774
  onError(CALLBACK_CODES.PARTIAL_UPDATE, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4773
4775
  }
@@ -4878,7 +4880,39 @@ const useWidget = ({
4878
4880
  setFormState(undefined);
4879
4881
  setItemData(null);
4880
4882
  };
4881
- const onChangeFormState = (state, data) => {
4883
+ const getAndSetWidget = id => __awaiter(void 0, void 0, void 0, function* () {
4884
+ try {
4885
+ setLoading(true);
4886
+ const api = getApiType({
4887
+ routes,
4888
+ action: 'GET_ONE',
4889
+ prefix: widgetRoutesPrefix,
4890
+ id
4891
+ });
4892
+ const response = yield commonApi({
4893
+ baseUrl,
4894
+ token,
4895
+ url: api.url,
4896
+ method: api.method,
4897
+ onError: handleError(CALLBACK_CODES.GET_SINGLE)
4898
+ });
4899
+ if ((response === null || response === void 0 ? void 0 : response.code) === 'SUCCESS') {
4900
+ setLoading(false);
4901
+ const data = response === null || response === void 0 ? void 0 : response.data;
4902
+ if (Array.isArray(data.items)) {
4903
+ const items = JSON.parse(JSON.stringify(data.items));
4904
+ data.webItems = items.filter(item => item.itemType === 'Web');
4905
+ data.mobileItems = items.filter(item => item.itemType === 'Mobile');
4906
+ delete data.items;
4907
+ }
4908
+ setItemData(data);
4909
+ }
4910
+ } catch (error) {
4911
+ setLoading(false);
4912
+ onError(CALLBACK_CODES.UPDATE, '', error.message);
4913
+ }
4914
+ });
4915
+ const onChangeFormState = (state, data) => __awaiter(void 0, void 0, void 0, function* () {
4882
4916
  setFormState(state);
4883
4917
  // fetch ItemsTypes & WidgetTypes if needed
4884
4918
  if (state === 'ADD' || state === 'UPDATE') {
@@ -4887,52 +4921,14 @@ const useWidget = ({
4887
4921
  }
4888
4922
  // get Item data if widget is updating
4889
4923
  if (state === 'UPDATE' && data) {
4890
- if (data['itemsType'] === 'Image') {
4891
- getItems(data['_id']);
4892
- }
4893
- setItemData(data);
4924
+ getAndSetWidget(data['_id']);
4894
4925
  } else if (state === 'ADD') {
4895
4926
  // reset Item data if widget is adding
4896
- setItemsList({
4897
- web: [],
4898
- mobile: []
4899
- });
4900
4927
  setItemData(null);
4901
4928
  } else if (state === 'DELETE' && data) {
4902
4929
  setItemData(data);
4903
4930
  setFormState(state);
4904
4931
  }
4905
- };
4906
- const onItemFormSubmit = (state, data, updateId) => __awaiter(void 0, void 0, void 0, function* () {
4907
- setItemsLoading(true);
4908
- const code = state === 'ADD' ? CALLBACK_CODES.CREATE : CALLBACK_CODES.UPDATE;
4909
- try {
4910
- const api = getApiType({
4911
- routes,
4912
- action: state === 'ADD' ? 'CREATE' : 'UPDATE',
4913
- prefix: itemsRoutesPrefix,
4914
- id: updateId
4915
- });
4916
- const response = yield commonApi({
4917
- baseUrl,
4918
- token,
4919
- data,
4920
- url: api.url,
4921
- method: api.method,
4922
- onError: handleError(code)
4923
- });
4924
- if ((response === null || response === void 0 ? void 0 : response.code) === 'SUCCESS') {
4925
- setItemsLoading(false);
4926
- onSuccess(code, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4927
- if (itemData) getItems(itemData['_id']);
4928
- } else {
4929
- setItemsLoading(false);
4930
- onError(code, response === null || response === void 0 ? void 0 : response.code, response === null || response === void 0 ? void 0 : response.message);
4931
- }
4932
- } catch (error) {
4933
- setItemsLoading(false);
4934
- onError(code, INTERNAL_ERROR_CODE, error.message);
4935
- }
4936
4932
  });
4937
4933
  // Image Upload operations
4938
4934
  const onImageUpload = file => __awaiter(void 0, void 0, void 0, function* () {
@@ -5018,7 +5014,6 @@ const useWidget = ({
5018
5014
  itemData,
5019
5015
  onChangeFormState,
5020
5016
  onCloseForm,
5021
- onDeleteItem,
5022
5017
  onWidgetFormSubmit,
5023
5018
  onCofirmDeleteWidget,
5024
5019
  onPartialUpdateWidget,
@@ -5029,11 +5024,7 @@ const useWidget = ({
5029
5024
  changeSearch,
5030
5025
  collectionDataLoading,
5031
5026
  getCollectionData,
5032
- collectionData,
5033
- // Items
5034
- itemsList,
5035
- itemsLoading,
5036
- onItemFormSubmit
5027
+ collectionData
5037
5028
  };
5038
5029
  };
5039
5030
 
@@ -6151,11 +6142,6 @@ const WidgetContextProvider = ({
6151
6142
  canDelete: _canDelete = false,
6152
6143
  loader: _loader = /*#__PURE__*/React.createElement("span", null),
6153
6144
  onPartialUpdateWidget: _onPartialUpdateWidget = () => Promise.resolve(),
6154
- // Item
6155
- webItems: _webItems = [],
6156
- mobileItems: _mobileItems = [],
6157
- itemsLoading: _itemsLoading = false,
6158
- onItemFormSubmit: _onItemFormSubmit = () => {},
6159
6145
  reactSelectStyles: _reactSelectStyles = {},
6160
6146
  // other
6161
6147
  children
@@ -6203,12 +6189,7 @@ const WidgetContextProvider = ({
6203
6189
  columns: _columns,
6204
6190
  data: _data,
6205
6191
  canDelete: _canDelete,
6206
- loader: _loader,
6207
- // Item
6208
- webItems: _webItems,
6209
- mobileItems: _mobileItems,
6210
- itemsLoading: _itemsLoading,
6211
- onItemFormSubmit: _onItemFormSubmit
6192
+ loader: _loader
6212
6193
  }
6213
6194
  }, children);
6214
6195
  };
@@ -6356,6 +6337,72 @@ $({ target: 'String', proto: true, forced: !correctIsRegExpLogic('includes') },
6356
6337
  }
6357
6338
  });
6358
6339
 
6340
+ const ChevronDown = ({
6341
+ srText,
6342
+ className
6343
+ }) => {
6344
+ return /*#__PURE__*/React.createElement(React.Fragment, null, srText ? /*#__PURE__*/React.createElement("span", {
6345
+ className: "khb_sr-only"
6346
+ }, srText) : null, /*#__PURE__*/React.createElement("svg", {
6347
+ xmlns: "http://www.w3.org/2000/svg",
6348
+ viewBox: "-5 -8 24 24",
6349
+ width: "24",
6350
+ fill: "currentColor",
6351
+ className: className
6352
+ }, /*#__PURE__*/React.createElement("path", {
6353
+ d: "M7.071 5.314l4.95-4.95a1 1 0 1 1 1.414 1.414L7.778 7.435a1 1 0 0 1-1.414 0L.707 1.778A1 1 0 1 1 2.121.364l4.95 4.95z"
6354
+ })));
6355
+ };
6356
+
6357
+ const ChevronUp = ({
6358
+ srText,
6359
+ className
6360
+ }) => {
6361
+ return /*#__PURE__*/React.createElement(React.Fragment, null, srText ? /*#__PURE__*/React.createElement("span", {
6362
+ className: "khb_sr-only"
6363
+ }, srText) : null, /*#__PURE__*/React.createElement("svg", {
6364
+ xmlns: "http://www.w3.org/2000/svg",
6365
+ viewBox: "-5 -7.5 24 24",
6366
+ width: "24",
6367
+ fill: "currentColor",
6368
+ className: className
6369
+ }, /*#__PURE__*/React.createElement("path", {
6370
+ d: "M7.071 2.828l-4.95 4.95A1 1 0 0 1 .707 6.364L6.364.707a1 1 0 0 1 1.414 0l5.657 5.657a1 1 0 0 1-1.414 1.414l-4.95-4.95z"
6371
+ })));
6372
+ };
6373
+
6374
+ const Accordian = ({
6375
+ id,
6376
+ className,
6377
+ children,
6378
+ open,
6379
+ onToggle,
6380
+ title,
6381
+ collapseId,
6382
+ footerContent
6383
+ }) => {
6384
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("h2", {
6385
+ id: id
6386
+ }, /*#__PURE__*/React.createElement("button", {
6387
+ type: "button",
6388
+ className: "khb_accordian-header",
6389
+ "data-accordion-target": `#${collapseId}`,
6390
+ "aria-expanded": "true",
6391
+ "aria-controls": collapseId,
6392
+ onClick: () => onToggle && onToggle(!open)
6393
+ }, /*#__PURE__*/React.createElement("span", null, title), open ? /*#__PURE__*/React.createElement(ChevronUp, null) : /*#__PURE__*/React.createElement(ChevronDown, null))), /*#__PURE__*/React.createElement("div", {
6394
+ id: collapseId,
6395
+ className: classNames('khb_accordian-body', {
6396
+ hidden: !open
6397
+ }),
6398
+ "aria-labelledby": id
6399
+ }, /*#__PURE__*/React.createElement("div", {
6400
+ className: "khb_accordian-content"
6401
+ }, children), footerContent && /*#__PURE__*/React.createElement("div", {
6402
+ className: "khb_accordian-footer"
6403
+ }, footerContent)));
6404
+ };
6405
+
6359
6406
  const ImageUpload = ({
6360
6407
  className,
6361
6408
  text,
@@ -6366,6 +6413,7 @@ const ImageUpload = ({
6366
6413
  imgId: _imgId = '',
6367
6414
  onImageUpload,
6368
6415
  onImageRemove,
6416
+ clearError,
6369
6417
  baseUrl,
6370
6418
  disabled: _disabled = false
6371
6419
  }) => {
@@ -6384,7 +6432,7 @@ const ImageUpload = ({
6384
6432
  onDrop: (acceptedFiles, rejectedFiles) => __awaiter(void 0, void 0, void 0, function* () {
6385
6433
  var _a, _b, _c;
6386
6434
  try {
6387
- onError('');
6435
+ if (typeof clearError === 'function') clearError();
6388
6436
  if ((acceptedFiles === null || acceptedFiles === void 0 ? void 0 : acceptedFiles.length) > 0) {
6389
6437
  const regex = /\.(png|jpeg|jpg|webp|svg)$/gi;
6390
6438
  const files = acceptedFiles.filter(file => regex.test(file.name));
@@ -6430,174 +6478,127 @@ const ImageUpload = ({
6430
6478
  className: "khb_img-wrapper-del"
6431
6479
  }, /*#__PURE__*/React.createElement(Close, null)));
6432
6480
  };
6433
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
6481
+ if (!isEmpty(img) && isString(img) && img) {
6482
+ return /*#__PURE__*/React.createElement("div", {
6483
+ className: "khb_img-upload-wrapper-1"
6484
+ }, /*#__PURE__*/React.createElement("div", {
6485
+ className: "khb_img-upload-wrapper-2"
6486
+ }, /*#__PURE__*/React.createElement("div", null, showImage(img))));
6487
+ }
6488
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", Object.assign({}, getRootProps(), {
6434
6489
  className: "khb_img-upload-wrapper-1"
6435
- }, /*#__PURE__*/React.createElement("div", {
6436
- className: "khb_img-upload-wrapper-2"
6437
- }, !isEmpty(img) && isString(img) && img ? showImage(img) : /*#__PURE__*/React.createElement("div", Object.assign({}, getRootProps({
6438
- className
6439
- })), /*#__PURE__*/React.createElement("input", Object.assign({
6490
+ }), /*#__PURE__*/React.createElement("input", Object.assign({
6440
6491
  disabled: _disabled
6441
6492
  }, getInputProps(), {
6442
6493
  id: "file-upload"
6443
- })), text))), error && /*#__PURE__*/React.createElement("p", {
6494
+ })), /*#__PURE__*/React.createElement("label", {
6495
+ htmlFor: "file-upload",
6496
+ className: "khb_img-upload-wrapper-2"
6497
+ }, /*#__PURE__*/React.createElement("div", {
6498
+ className: className
6499
+ }, text))), error && /*#__PURE__*/React.createElement("p", {
6444
6500
  className: "khb_input-error"
6445
6501
  }, error));
6446
6502
  };
6447
6503
  var ImageUpload$1 = /*#__PURE__*/React.memo(ImageUpload);
6448
6504
 
6449
- const ChevronDown = ({
6450
- srText,
6451
- className
6452
- }) => {
6453
- return /*#__PURE__*/React.createElement(React.Fragment, null, srText ? /*#__PURE__*/React.createElement("span", {
6454
- className: "khb_sr-only"
6455
- }, srText) : null, /*#__PURE__*/React.createElement("svg", {
6456
- xmlns: "http://www.w3.org/2000/svg",
6457
- viewBox: "-5 -8 24 24",
6458
- width: "24",
6459
- fill: "currentColor",
6460
- className: className
6461
- }, /*#__PURE__*/React.createElement("path", {
6462
- d: "M7.071 5.314l4.95-4.95a1 1 0 1 1 1.414 1.414L7.778 7.435a1 1 0 0 1-1.414 0L.707 1.778A1 1 0 1 1 2.121.364l4.95 4.95z"
6463
- })));
6464
- };
6465
-
6466
- const ChevronUp = ({
6467
- srText,
6468
- className
6469
- }) => {
6470
- return /*#__PURE__*/React.createElement(React.Fragment, null, srText ? /*#__PURE__*/React.createElement("span", {
6471
- className: "khb_sr-only"
6472
- }, srText) : null, /*#__PURE__*/React.createElement("svg", {
6473
- xmlns: "http://www.w3.org/2000/svg",
6474
- viewBox: "-5 -7.5 24 24",
6475
- width: "24",
6476
- fill: "currentColor",
6477
- className: className
6478
- }, /*#__PURE__*/React.createElement("path", {
6479
- d: "M7.071 2.828l-4.95 4.95A1 1 0 0 1 .707 6.364L6.364.707a1 1 0 0 1 1.414 0l5.657 5.657a1 1 0 0 1-1.414 1.414l-4.95-4.95z"
6480
- })));
6481
- };
6482
-
6483
- const Accordian = ({
6484
- id,
6485
- className,
6505
+ const ConfirmPopOver = ({
6486
6506
  children,
6487
- open,
6488
- onToggle,
6507
+ onConfirm,
6489
6508
  title,
6490
- collapseId,
6491
- footerContent
6509
+ confirmText: _confirmText = 'Yes',
6510
+ cancelText: _cancelText = 'No'
6492
6511
  }) => {
6493
- return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("h2", {
6494
- id: id
6495
- }, /*#__PURE__*/React.createElement("button", {
6496
- type: "button",
6497
- className: "khb_accordian-header",
6498
- "data-accordion-target": `#${collapseId}`,
6499
- "aria-expanded": "true",
6500
- "aria-controls": collapseId,
6501
- onClick: () => onToggle && onToggle(!open)
6502
- }, /*#__PURE__*/React.createElement("span", null, title), open ? /*#__PURE__*/React.createElement(ChevronUp, null) : /*#__PURE__*/React.createElement(ChevronDown, null))), /*#__PURE__*/React.createElement("div", {
6503
- id: collapseId,
6504
- className: classNames('khb_accordian-body', {
6505
- hidden: !open
6506
- }),
6507
- "aria-labelledby": id
6512
+ const [isPopoverOpen, setIsPopoverOpen] = useState(false);
6513
+ const onConfirmClick = () => {
6514
+ setIsPopoverOpen(false);
6515
+ onConfirm();
6516
+ };
6517
+ return /*#__PURE__*/React.createElement(Popover, {
6518
+ containerClassName: "khb_confirm-popover",
6519
+ isOpen: isPopoverOpen,
6520
+ padding: 10,
6521
+ onClickOutside: () => setIsPopoverOpen(false),
6522
+ content: () => /*#__PURE__*/React.createElement("div", {
6523
+ className: "khb_confirm-popover-content"
6524
+ }, /*#__PURE__*/React.createElement("div", {
6525
+ className: "khb_confirm-popover-title"
6526
+ }, title), /*#__PURE__*/React.createElement("div", {
6527
+ className: "khb_confirm-popover-footer"
6528
+ }, /*#__PURE__*/React.createElement(Button, {
6529
+ type: "secondary",
6530
+ onClick: () => setIsPopoverOpen(false)
6531
+ }, _cancelText), /*#__PURE__*/React.createElement(Button, {
6532
+ type: "primary",
6533
+ onClick: onConfirmClick
6534
+ }, _confirmText)))
6508
6535
  }, /*#__PURE__*/React.createElement("div", {
6509
- className: "khb_accordian-content"
6510
- }, children), footerContent && /*#__PURE__*/React.createElement("div", {
6511
- className: "khb_accordian-footer"
6512
- }, footerContent)));
6536
+ onClick: () => setIsPopoverOpen(true)
6537
+ }, children));
6513
6538
  };
6514
6539
 
6515
6540
  const ItemsAccordian = ({
6516
- schema,
6517
- onDataSubmit,
6518
6541
  show,
6519
6542
  title,
6520
6543
  id,
6521
- itemsData,
6522
6544
  collapseId,
6523
6545
  toggleShow,
6546
+ loading,
6547
+ name,
6548
+ errors,
6549
+ control,
6550
+ register,
6551
+ setError,
6524
6552
  itemType,
6525
- widgetId,
6526
- onDelete,
6553
+ clearError,
6527
6554
  addText: _addText = 'Add',
6528
- saveText: _saveText = 'Save',
6529
- cancelText: _cancelText = 'Cancel',
6530
- deleteText: _deleteText = 'Delete',
6531
- editText: _editText = 'Edit'
6555
+ deleteText: _deleteText = 'Delete'
6532
6556
  }) => {
6533
- const [data, setData] = useState([]);
6534
- const formRefs = useRef([]);
6557
+ const {
6558
+ baseUrl
6559
+ } = useProviderState();
6560
+ const {
6561
+ onImageUpload,
6562
+ onImageRemove,
6563
+ t,
6564
+ imageBaseUrl
6565
+ } = useWidgetState();
6535
6566
  const [itemsShow, setItemsShow] = useState([]);
6536
- const [editingItemIndex, setEditingItemIndex] = useState();
6537
- useEffect(() => {
6538
- if (Array.isArray(itemsData)) {
6539
- setData(itemsData);
6540
- formRefs.current = itemsData.map(() => null);
6541
- setItemsShow(itemsData.map(() => false));
6542
- }
6543
- }, [itemsData]);
6544
- const onItemFormSubmitClick = index => {
6545
- var _a;
6546
- (_a = formRefs.current[index]) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new Event('submit', {
6547
- cancelable: true,
6548
- bubbles: true
6549
- }));
6550
- };
6551
- const onItemFormSubmit = (index, formData) => {
6552
- var _a;
6553
- const state = index === editingItemIndex && data[index] ? 'UPDATE' : 'ADD';
6554
- const finalData = Object.assign(Object.assign({}, formData), {
6555
- widgetId,
6556
- itemType,
6557
- sequence: index
6558
- });
6559
- if (finalData['img'] && finalData['img']['_id']) {
6560
- const id = finalData['img']['_id'];
6561
- finalData['img'] = id;
6562
- }
6563
- onDataSubmit(state, finalData, state === 'UPDATE' ? (_a = data[index]) === null || _a === void 0 ? void 0 : _a['_id'] : undefined);
6564
- setEditingItemIndex(undefined);
6565
- };
6567
+ const {
6568
+ fields: items,
6569
+ append: appendItem,
6570
+ remove: removeItem
6571
+ } = useFieldArray({
6572
+ name,
6573
+ control
6574
+ });
6566
6575
  const onItemsToggleClick = (index, status) => {
6576
+ var _a;
6567
6577
  const newItemsShow = [...itemsShow];
6568
- const newStatus = typeof status === 'undefined' ? !newItemsShow[index] : status;
6569
- newItemsShow.fill(false);
6578
+ const newStatus = ((_a = errors === null || errors === void 0 ? void 0 : errors[name]) === null || _a === void 0 ? void 0 : _a[index]) ? true : typeof status === 'undefined' ? !newItemsShow[index] : status;
6570
6579
  newItemsShow[index] = newStatus;
6571
6580
  setItemsShow(newItemsShow);
6572
6581
  };
6573
- const onItemRemoveClick = index => {
6574
- const newItemsShow = [...itemsShow];
6575
- newItemsShow.splice(index, 1);
6576
- setItemsShow(newItemsShow);
6577
- formRefs.current.splice(index, 1);
6578
- };
6579
- const onItemAddClick = () => {
6580
- const newItemsShow = [...itemsShow];
6581
- newItemsShow.push(false);
6582
- setItemsShow(newItemsShow);
6583
- formRefs.current.push(null);
6584
- onItemsToggleClick(newItemsShow.length - 1);
6585
- };
6586
- const onItemEditClick = index => {
6587
- setEditingItemIndex(index);
6588
- };
6589
- const onItemCancelClick = index => {
6590
- if (!data[index]) {
6591
- onItemRemoveClick(index);
6592
- } else {
6593
- setEditingItemIndex(undefined);
6582
+ useEffect(() => {
6583
+ var _a, _b;
6584
+ if (errors && ((_a = errors === null || errors === void 0 ? void 0 : errors[name]) === null || _a === void 0 ? void 0 : _a.length) > 0) {
6585
+ (_b = errors === null || errors === void 0 ? void 0 : errors[name]) === null || _b === void 0 ? void 0 : _b.forEach((errorItem, index) => {
6586
+ if (errorItem) onItemsToggleClick(index, true);
6587
+ });
6594
6588
  }
6595
- onItemsToggleClick(index, false);
6596
- };
6597
- const onDeleteClick = index => {
6598
- var _a;
6599
- onDelete((_a = data[index]) === null || _a === void 0 ? void 0 : _a['_id']);
6600
- onItemRemoveClick(index);
6589
+ // eslint-disable-next-line react-hooks/exhaustive-deps
6590
+ }, [errors, name, errors === null || errors === void 0 ? void 0 : errors[name]]);
6591
+ const addTab = index => {
6592
+ appendItem({
6593
+ title: '',
6594
+ subtitle: '',
6595
+ altText: '',
6596
+ link: '',
6597
+ img: '',
6598
+ srcset: [],
6599
+ itemType,
6600
+ sequence: index
6601
+ });
6601
6602
  };
6602
6603
  return /*#__PURE__*/React.createElement(Accordian, {
6603
6604
  open: show,
@@ -6607,76 +6608,107 @@ const ItemsAccordian = ({
6607
6608
  id: id,
6608
6609
  footerContent: /*#__PURE__*/React.createElement(Button, {
6609
6610
  size: "sm",
6610
- onClick: onItemAddClick,
6611
- disabled: !widgetId
6611
+ disabled: loading,
6612
+ onClick: () => addTab(items.length)
6612
6613
  }, _addText)
6613
6614
  }, /*#__PURE__*/React.createElement("div", {
6614
6615
  className: "khb_item-items"
6615
- }, itemsShow.map((status, index) => /*#__PURE__*/React.createElement(Accordian, {
6616
- key: index,
6617
- open: status,
6618
- onToggle: () => onItemsToggleClick(index),
6619
- title: `Item ${index + 1}`,
6620
- collapseId: `${id}-item-content-${index}`,
6621
- id: `${id}-item-${index}`,
6622
- footerContent: editingItemIndex === index || !data[index] ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
6623
- size: "sm",
6624
- onClick: () => onItemFormSubmitClick(index)
6625
- }, _saveText), /*#__PURE__*/React.createElement(Button, {
6626
- type: "secondary",
6627
- size: "sm",
6628
- onClick: () => onItemCancelClick(index)
6629
- }, _cancelText)) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
6630
- size: "sm",
6631
- onClick: () => onItemEditClick(index)
6632
- }, _editText), /*#__PURE__*/React.createElement(Button, {
6633
- type: "danger",
6634
- size: "sm",
6635
- onClick: () => onDeleteClick(index)
6636
- }, _deleteText))
6637
- }, /*#__PURE__*/React.createElement(Form, {
6638
- schema: schema,
6639
- data: data[index],
6640
- onSubmit: data => onItemFormSubmit(index, data),
6641
- ref: el => formRefs.current[index] = el,
6642
- enable: editingItemIndex === index || !data[index],
6643
- isUpdating: editingItemIndex === index
6644
- })))));
6645
- };
6646
-
6647
- const ConfirmPopOver = ({
6648
- children,
6649
- onConfirm,
6650
- title,
6651
- confirmText: _confirmText = 'Yes',
6652
- cancelText: _cancelText = 'No'
6653
- }) => {
6654
- const [isPopoverOpen, setIsPopoverOpen] = useState(false);
6655
- const onConfirmClick = () => {
6656
- setIsPopoverOpen(false);
6657
- onConfirm();
6658
- };
6659
- return /*#__PURE__*/React.createElement(Popover, {
6660
- containerClassName: "khb_confirm-popover",
6661
- isOpen: isPopoverOpen,
6662
- padding: 10,
6663
- onClickOutside: () => setIsPopoverOpen(false),
6664
- content: () => /*#__PURE__*/React.createElement("div", {
6665
- className: "khb_confirm-popover-content"
6616
+ }, items === null || items === void 0 ? void 0 : items.map((field, index) => {
6617
+ var _a, _b, _c, _d, _e, _f;
6618
+ return /*#__PURE__*/React.createElement(Accordian, {
6619
+ key: index,
6620
+ open: itemsShow[index],
6621
+ onToggle: () => onItemsToggleClick(index),
6622
+ title: `Item ${index + 1}`,
6623
+ collapseId: `${id}-item-content-${index}`,
6624
+ id: `${id}-item-${index}`,
6625
+ footerContent: /*#__PURE__*/React.createElement(ConfirmPopOver, {
6626
+ onConfirm: () => removeItem(index),
6627
+ title: t('item.deleteTitle'),
6628
+ confirmText: t('yesButtonText'),
6629
+ cancelText: t('cancelButtonText')
6630
+ }, /*#__PURE__*/React.createElement(Button, {
6631
+ type: "danger",
6632
+ size: "sm",
6633
+ disabled: loading
6634
+ }, _deleteText))
6666
6635
  }, /*#__PURE__*/React.createElement("div", {
6667
- className: "khb_confirm-popover-title"
6668
- }, title), /*#__PURE__*/React.createElement("div", {
6669
- className: "khb_confirm-popover-footer"
6670
- }, /*#__PURE__*/React.createElement(Button, {
6671
- type: "secondary",
6672
- onClick: () => setIsPopoverOpen(false)
6673
- }, _cancelText), /*#__PURE__*/React.createElement(Button, {
6674
- type: "primary",
6675
- onClick: onConfirmClick
6676
- }, _confirmText)))
6677
- }, /*#__PURE__*/React.createElement("div", {
6678
- onClick: () => setIsPopoverOpen(true)
6679
- }, children));
6636
+ className: "khb-form-items"
6637
+ }, /*#__PURE__*/React.createElement(Input, {
6638
+ rest: register(`${name}.${index}.title`, {
6639
+ required: t('item.titleRequired')
6640
+ }),
6641
+ label: t('item.title'),
6642
+ error: (_d = (_c = (_b = (_a = errors[name]) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b['title']) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.toString(),
6643
+ type: 'text',
6644
+ className: "w-full p-2",
6645
+ placeholder: t('item.titlePlaceholder'),
6646
+ required: true
6647
+ }), /*#__PURE__*/React.createElement(Input, {
6648
+ rest: register(`${name}.${index}.subtitle`),
6649
+ label: t('item.subtitle'),
6650
+ type: 'text',
6651
+ className: "w-full p-2",
6652
+ placeholder: t('item.subTitlePlaceholder')
6653
+ }), /*#__PURE__*/React.createElement(Input, {
6654
+ rest: register(`${name}.${index}.altText`),
6655
+ label: t('item.altText'),
6656
+ type: 'text',
6657
+ className: "w-full p-2",
6658
+ placeholder: t('item.altTextPlaceholder')
6659
+ }), /*#__PURE__*/React.createElement(Input, {
6660
+ rest: register(`${name}.${index}.link`),
6661
+ label: t('item.link'),
6662
+ type: 'url',
6663
+ className: "w-full p-2",
6664
+ placeholder: t('item.linkPlaceholder')
6665
+ }), /*#__PURE__*/React.createElement(Input.SrcSet, {
6666
+ control: control,
6667
+ register: register,
6668
+ label: t(`item.srcset`),
6669
+ name: `${name}.${index}.srcset`,
6670
+ errors: (_f = (_e = errors[name]) === null || _e === void 0 ? void 0 : _e[index]) === null || _f === void 0 ? void 0 : _f['srcset'],
6671
+ t: t
6672
+ }), /*#__PURE__*/React.createElement("div", {
6673
+ className: "kms_input-wrapper"
6674
+ }, /*#__PURE__*/React.createElement("label", {
6675
+ className: "kms_input-label"
6676
+ }, t('item.image')), /*#__PURE__*/React.createElement(Controller, {
6677
+ control: control,
6678
+ name: `${name}.${index}.img`,
6679
+ render: ({
6680
+ field
6681
+ }) => {
6682
+ var _a, _b, _c, _d;
6683
+ return /*#__PURE__*/React.createElement(ImageUpload$1, {
6684
+ imgId: field.value,
6685
+ clearError: () => clearError(`${name}.${index}.img`),
6686
+ maxSize: 10485760,
6687
+ onError: msg => setError(`${name}.${index}.img`, {
6688
+ type: 'custom',
6689
+ message: msg
6690
+ }),
6691
+ error: (_d = (_c = (_b = (_a = errors[name]) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b['img']) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.toString(),
6692
+ setImgId: value => {
6693
+ field.onChange(value);
6694
+ },
6695
+ baseUrl: imageBaseUrl ? imageBaseUrl : baseUrl,
6696
+ text: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
6697
+ className: "khb_img-text-wrapper"
6698
+ }, /*#__PURE__*/React.createElement("div", {
6699
+ className: "khb_img-text-label"
6700
+ }, /*#__PURE__*/React.createElement("span", null, t('item.uploadFile'))), /*#__PURE__*/React.createElement("p", {
6701
+ className: "khb_img-text-1"
6702
+ }, t('item.dragDrop'))), /*#__PURE__*/React.createElement("p", {
6703
+ className: "khb_img-text-2"
6704
+ }, t('item.allowedFormat'))),
6705
+ onImageUpload: onImageUpload,
6706
+ onImageRemove: onImageRemove,
6707
+ className: "khb_img-upload-wrapper-3"
6708
+ });
6709
+ }
6710
+ }))));
6711
+ })));
6680
6712
  };
6681
6713
 
6682
6714
  const TabItem = ({
@@ -6815,6 +6847,8 @@ const constants = {
6815
6847
  carouselWidgetTypeValue: 'Carousel',
6816
6848
  imageItemsTypeValue: 'Image',
6817
6849
  tabsAccessor: 'tabs',
6850
+ webItems: 'webItems',
6851
+ mobileItems: 'mobileItems',
6818
6852
  tabCollectionItemsAccessor: 'collectionItems'
6819
6853
  };
6820
6854
  const WidgetForm = ({
@@ -6831,13 +6865,13 @@ const WidgetForm = ({
6831
6865
  setValue,
6832
6866
  control,
6833
6867
  watch,
6868
+ clearErrors,
6834
6869
  setError,
6835
6870
  getValues
6836
6871
  } = useForm({
6837
6872
  shouldUnregister: false
6838
6873
  });
6839
6874
  const {
6840
- baseUrl,
6841
6875
  switchClass
6842
6876
  } = useProviderState();
6843
6877
  const {
@@ -6845,23 +6879,17 @@ const WidgetForm = ({
6845
6879
  data,
6846
6880
  canAdd,
6847
6881
  canUpdate,
6848
- webItems,
6849
- mobileItems,
6850
6882
  formState,
6851
6883
  itemsTypes,
6852
6884
  widgetTypes,
6853
- onItemFormSubmit,
6885
+ loading,
6854
6886
  onWidgetFormSubmit,
6855
- onDeleteItem,
6856
- onImageRemove,
6857
- onImageUpload,
6858
6887
  getCollectionData,
6859
6888
  collectionData,
6860
6889
  collectionDataLoading,
6861
6890
  formatListItem,
6862
6891
  formatOptionLabel,
6863
- reactSelectStyles,
6864
- imageBaseUrl
6892
+ reactSelectStyles
6865
6893
  } = useWidgetState();
6866
6894
  const callerRef = useRef(null);
6867
6895
  const [activeTab, setActiveTab] = useState(0);
@@ -6902,7 +6930,7 @@ const WidgetForm = ({
6902
6930
  let valueToSet = '';
6903
6931
  if (formState === 'UPDATE') {
6904
6932
  if (data[constants.widgetTypeAccessor] === constants.tabsWidgetTypeValue) {
6905
- collectionItems = data[constants.tabsAccessor][activeTab]['collectionItems'];
6933
+ collectionItems = data[constants.tabsAccessor][activeTab] ? data[constants.tabsAccessor][activeTab]['collectionItems'] : [];
6906
6934
  valueToSet = `${constants.tabsAccessor}.${activeTab}.${constants.tabCollectionItemsAccessor}`;
6907
6935
  } else if (Array.isArray(data[constants.collectionItemsAccessor]) && data[constants.collectionItemsAccessor].length > 0) {
6908
6936
  collectionItems = data[constants.collectionItemsAccessor];
@@ -7002,7 +7030,19 @@ const WidgetForm = ({
7002
7030
  if (Array.isArray(selectedCollectionItems) && selectedCollectionItems.length > 0) {
7003
7031
  formData['collectionItems'] = selectedCollectionItems.map(item => item.value);
7004
7032
  }
7005
- onWidgetFormSubmit(formData);
7033
+ let items = [...(getValues(constants.webItems) || []), ...(getValues(constants.mobileItems) || [])];
7034
+ items = items.map(_a => {
7035
+ var item = __rest(_a, ["_id", "__v", "widgetId"]);
7036
+ if (item['img'] && item['img']['_id']) {
7037
+ item['img'] = item['img']['_id'];
7038
+ } else if (typeof item['img'] !== 'string' || !item['img']) {
7039
+ delete item['img'];
7040
+ }
7041
+ return item;
7042
+ });
7043
+ onWidgetFormSubmit(Object.assign(Object.assign({}, formData), {
7044
+ items
7045
+ }));
7006
7046
  };
7007
7047
  const onCollectionIndexChange = result => {
7008
7048
  const {
@@ -7165,65 +7205,6 @@ const WidgetForm = ({
7165
7205
  customStyles: reactSelectStyles || {},
7166
7206
  selectKey: selectedCollectionType === null || selectedCollectionType === void 0 ? void 0 : selectedCollectionType.value
7167
7207
  }];
7168
- const itemFormSchema = [{
7169
- label: `${t('item.title')}`,
7170
- required: true,
7171
- accessor: 'title',
7172
- type: 'text',
7173
- placeholder: t('item.titlePlaceholder')
7174
- }, {
7175
- label: `${t('item.subtitle')}`,
7176
- accessor: 'subtitle',
7177
- type: 'text',
7178
- placeholder: t('item.subTitlePlaceholder')
7179
- }, {
7180
- label: `${t('item.altText')}`,
7181
- accessor: 'altText',
7182
- type: 'text',
7183
- placeholder: t('item.altTextPlaceholder')
7184
- }, {
7185
- label: `${t('item.link')}`,
7186
- required: true,
7187
- accessor: 'link',
7188
- type: 'url',
7189
- placeholder: t('item.linkPlaceholder')
7190
- }, {
7191
- label: `${t('item.srcset')}`,
7192
- accessor: 'srcset',
7193
- type: 'srcset'
7194
- }, {
7195
- label: t('item.image'),
7196
- accessor: 'img',
7197
- Input: ({
7198
- field,
7199
- error,
7200
- setError,
7201
- disabled
7202
- }) => /*#__PURE__*/React.createElement(ImageUpload$1, {
7203
- imgId: field.value,
7204
- maxSize: 10485760,
7205
- onError: setError,
7206
- error: error,
7207
- setImgId: value => {
7208
- field.onChange(value);
7209
- },
7210
- baseUrl: imageBaseUrl ? imageBaseUrl : baseUrl,
7211
- disabled: disabled,
7212
- text: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
7213
- className: "khb_img-text-wrapper"
7214
- }, /*#__PURE__*/React.createElement("label", {
7215
- htmlFor: "file-upload",
7216
- className: "khb_img-text-label"
7217
- }, /*#__PURE__*/React.createElement("span", null, t('item.uploadFile'))), /*#__PURE__*/React.createElement("p", {
7218
- className: "khb_img-text-1"
7219
- }, t('item.dragDrop'))), /*#__PURE__*/React.createElement("p", {
7220
- className: "khb_img-text-2"
7221
- }, t('item.allowedFormat'))),
7222
- onImageUpload: onImageUpload,
7223
- onImageRemove: onImageRemove,
7224
- className: "khb_img-upload-wrapper-3"
7225
- })
7226
- }];
7227
7208
  if (!canAdd || !canUpdate) return null;
7228
7209
  return /*#__PURE__*/React.createElement("div", {
7229
7210
  className: "khb_form"
@@ -7261,38 +7242,36 @@ const WidgetForm = ({
7261
7242
  formatItem: formatListItem,
7262
7243
  listCode: selectedCollectionType === null || selectedCollectionType === void 0 ? void 0 : selectedCollectionType.value
7263
7244
  }), itemsEnabled && selectedWidgetType !== 'Tabs' && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ItemsAccordian, {
7264
- collapseId: "webItems",
7245
+ clearError: clearErrors,
7246
+ collapseId: constants.webItems,
7265
7247
  title: t('widget.webItems'),
7266
- id: "webItems",
7267
- schema: itemFormSchema,
7268
- show: webItemsVisible,
7269
- itemsData: webItems,
7248
+ id: constants.webItems,
7249
+ setError: setError,
7250
+ show: webItemsVisible || !!(errors && (errors === null || errors === void 0 ? void 0 : errors[constants.webItems])),
7270
7251
  toggleShow: setWebItemsVisible,
7271
- onDataSubmit: onItemFormSubmit,
7272
7252
  itemType: "Web",
7273
- widgetId: data === null || data === void 0 ? void 0 : data._id,
7274
- onDelete: onDeleteItem,
7253
+ name: constants.webItems,
7254
+ errors: errors,
7255
+ control: control,
7256
+ register: register,
7257
+ loading: loading,
7275
7258
  addText: t('addButtonText'),
7276
- cancelText: t('cancelButtonText'),
7277
- saveText: t('saveButtonText'),
7278
- editText: t('editButtonText'),
7279
7259
  deleteText: t('deleteButtonText')
7280
7260
  }), /*#__PURE__*/React.createElement(ItemsAccordian, {
7281
- collapseId: "mobileItems",
7261
+ clearError: clearErrors,
7262
+ collapseId: constants.mobileItems,
7282
7263
  title: t('widget.mobileItems'),
7283
- id: "mobileItems",
7284
- schema: itemFormSchema,
7285
- show: mobileItemsVisible,
7286
- itemsData: mobileItems,
7264
+ id: constants.mobileItems,
7265
+ name: constants.mobileItems,
7266
+ setError: setError,
7267
+ loading: loading,
7268
+ show: mobileItemsVisible || !!(errors && (errors === null || errors === void 0 ? void 0 : errors[constants.mobileItems])),
7287
7269
  toggleShow: setMobileItemsVisible,
7288
- onDataSubmit: onItemFormSubmit,
7289
7270
  itemType: "Mobile",
7290
- widgetId: data === null || data === void 0 ? void 0 : data._id,
7291
- onDelete: onDeleteItem,
7271
+ errors: errors,
7272
+ control: control,
7273
+ register: register,
7292
7274
  addText: t('addButtonText'),
7293
- cancelText: t('cancelButtonText'),
7294
- saveText: t('saveButtonText'),
7295
- editText: t('editButtonText'),
7296
7275
  deleteText: t('deleteButtonText')
7297
7276
  })));
7298
7277
  };
@@ -7438,7 +7417,6 @@ const Widget = ({
7438
7417
  itemData,
7439
7418
  getWidgets,
7440
7419
  onCofirmDeleteWidget,
7441
- onDeleteItem,
7442
7420
  onImageRemove,
7443
7421
  onImageUpload,
7444
7422
  onPartialUpdateWidget,
@@ -7452,11 +7430,7 @@ const Widget = ({
7452
7430
  totalRecords,
7453
7431
  currentPage,
7454
7432
  pageSize,
7455
- setCurrentPage,
7456
- // Item
7457
- itemsList,
7458
- itemsLoading,
7459
- onItemFormSubmit
7433
+ setCurrentPage
7460
7434
  } = useWidget({
7461
7435
  canList: derivedPermissions.list,
7462
7436
  routes,
@@ -7475,7 +7449,6 @@ const Widget = ({
7475
7449
  loader: loader,
7476
7450
  onWidgetFormSubmit: onWidgetFormSubmit,
7477
7451
  data: itemData,
7478
- onDeleteItem: onDeleteItem,
7479
7452
  getWidgets: getWidgets,
7480
7453
  onImageRemove: onImageRemove,
7481
7454
  onImageUpload: onImageUpload,
@@ -7493,11 +7466,6 @@ const Widget = ({
7493
7466
  currentPage: currentPage,
7494
7467
  pageSize: pageSize,
7495
7468
  setCurrentPage: setCurrentPage,
7496
- // Item
7497
- webItems: itemsList.web,
7498
- mobileItems: itemsList.mobile,
7499
- itemsLoading: itemsLoading,
7500
- onItemFormSubmit: onItemFormSubmit,
7501
7469
  // Permissions
7502
7470
  canAdd: derivedPermissions.add,
7503
7471
  canDelete: derivedPermissions.delete,