Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState, useEffect, useMemo } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { Button, InputGroup, MenuItem, ModalActions, ModalContent, ModalTitle, SelectGroup, TooltipV2 } from '@scaleflex/ui/core';
import { useTheme } from '@scaleflex/ui/theme/hooks';
import { LoaderView, PC, Typography } from '@filerobot/common';
import { COLLECTION_TYPES_OPTIONS, COLLECTION_TYPE_IDS, COLLECTION_VISIBILITY_OPTIONS } from '@filerobot/utils/lib/constants';
import cuid from 'cuid';

// import Filters from './Filters'
import Groups from './Groups';
import Styled from './CollectionModal.styled';
import { InfoOutline } from '@scaleflex/icons';
import { useExplorerI18n } from '../../../../hooks';
import { createCollection, selectCollectionsSearchTerm, updateCollectionByUuid } from '../../../../slices/collections.slice';
import { collectionModalOpened, selectCollectionToEdit } from '../../../../slices/panels.slice';
import { selectSearchSuggestionsAttributesKeys, selectSearchSuggestionsAttributesKeysLoading } from '../../../../slices/search.slice';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var generateNewGroup = function generateNewGroup() {
  var attributeSysKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  return {
    attribute_sys_key: attributeSysKey,
    uuid: cuid()
  };
};
var CollectionModal = function CollectionModal() {
  var _form$data5, _form$data6, _form$data6$filters, _form$data7, _form$data7$groupby;
  var i18n = useExplorerI18n();
  var theme = useTheme();
  var dispatch = useDispatch();
  var iconPrimaryColor = theme.palette[PC.IconsPrimary];
  var collectionsSearchTerm = useSelector(selectCollectionsSearchTerm);
  var collectionToEdit = useSelector(selectCollectionToEdit);
  var searchSuggestionsAttributesKeys = useSelector(selectSearchSuggestionsAttributesKeys);
  var searchSuggestionsAttributesKeysLoading = useSelector(selectSearchSuggestionsAttributesKeysLoading);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    submitted = _useState4[0],
    setSubmitted = _useState4[1];
  var _useState5 = useState({
      title: collectionsSearchTerm || i18n('collectionDialogNewCollectionTitle', 'My new collection'),
      visibility: 'company',
      type: COLLECTION_TYPE_IDS.QUERY,
      data: {
        filters: '',
        groupby: [generateNewGroup()]
      }
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    form = _useState6[0],
    setForm = _useState6[1];
  var editCollectionUuid = collectionToEdit === null || collectionToEdit === void 0 ? void 0 : collectionToEdit.uuid;
  var isNew = !editCollectionUuid;

  // TODO: DON'T REMOVE will be uncommented later
  // const attrKeys = useMemo( // Available to be used as filters
  //   () => (searchSuggestionsAttributesKeys || []).filter(attrKey => attrKey?.stats?.top_values?.length && attrKey.stats.top_values.some(topVal => topVal?.label && topVal?.sys_key)),
  //   [searchSuggestionsAttributesKeys]
  // )

  var groupableAttrKeys = useMemo(function () {
    return searchSuggestionsAttributesKeys.filter(function (attrKey) {
      return !!(attrKey !== null && attrKey !== void 0 && attrKey.is_groupable);
    });
  }, [searchSuggestionsAttributesKeys]);
  var handleCreateCollection = function handleCreateCollection() {
    var _form$data = form.data,
      groupby = _form$data.groupby,
      filters = _form$data.filters;
    var groups = groupby.filter(function (_ref) {
      var attrSysKey = _ref.attribute_sys_key;
      return Boolean(attrSysKey);
    });
    if (!form.title || groups.length === 0 && !filters) {
      return;
    }
    setSubmitted(true);
    setIsLoading(true);
    var dispatchedReq;
    if (editCollectionUuid) {
      dispatchedReq = dispatch(updateCollectionByUuid({
        collection: _objectSpread({
          uuid: editCollectionUuid
        }, form)
      }));
    } else {
      dispatchedReq = dispatch(createCollection({
        collection: _objectSpread(_objectSpread({}, form), {}, {
          data: _objectSpread(_objectSpread({}, form.data), {}, {
            groupby: groups
          })
        })
      }));
    }
    dispatchedReq["finally"](function () {
      setIsLoading(false);
    });
  };
  useEffect(function () {
    if (collectionToEdit) {
      setForm(function (formState) {
        var _ref2 = collectionToEdit || {},
          title = _ref2.title,
          _ref2$visibility = _ref2.visibility,
          visibility = _ref2$visibility === void 0 ? 'company' : _ref2$visibility,
          _ref2$type = _ref2.type,
          type = _ref2$type === void 0 ? COLLECTION_TYPE_IDS.QUERY : _ref2$type,
          data = _ref2.data;
        return _objectSpread(_objectSpread({}, formState), {}, {
          title: title,
          visibility: visibility,
          type: typeof (data === null || data === void 0 ? void 0 : data.filters) === 'string' ? COLLECTION_TYPE_IDS.QUERY : type,
          data: _objectSpread(_objectSpread({}, formState.data), {}, {
            filters: (data === null || data === void 0 ? void 0 : data.filters) || '',
            groupby: (data === null || data === void 0 ? void 0 : data.groupby) || []
          })
        });
      });
    }
  }, [collectionToEdit]);
  var handleCancel = function handleCancel() {
    dispatch(collectionModalOpened({
      show: false,
      collectionToEdit: null
    }));
  };
  var handleAddGroup = function handleAddGroup() {
    var attributeSysKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    setForm(function (oldForm) {
      return _objectSpread(_objectSpread({}, oldForm), {}, {
        data: _objectSpread(_objectSpread({}, oldForm.data), {}, {
          groupby: [].concat(_toConsumableArray(oldForm.data.groupby), [generateNewGroup(attributeSysKey)])
        })
      });
    });
  };
  var changeFilters = function changeFilters() {
    var newFilters = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
    setForm(function (formState) {
      return _objectSpread(_objectSpread({}, formState), {}, {
        data: _objectSpread(_objectSpread({}, (formState === null || formState === void 0 ? void 0 : formState.data) || {}), {}, {
          filters: newFilters
        })
      });
    });
  };
  var renderContent = function renderContent() {
    var _form$data3, _COLLECTION_TYPES_OPT4, _form$data4;
    return searchSuggestionsAttributesKeysLoading ? /*#__PURE__*/_jsx(LoaderView, {
      loaderLabel: i18n('loaderLoadingLabel')
    }) : /*#__PURE__*/_jsxs(Styled.Container, {
      children: [/*#__PURE__*/_jsxs(Styled.FirstRow, {
        children: [/*#__PURE__*/_jsx(InputGroup, {
          label: i18n('mutualizedNameFieldLabel'),
          value: form.title,
          onChange: function onChange(_ref3) {
            var value = _ref3.target.value;
            return setForm(function (oldForm) {
              return _objectSpread(_objectSpread({}, oldForm), {}, {
                title: value
              });
            });
          },
          fullWidth: true,
          focusOnMount: true,
          error: submitted && !form.title,
          placeholder: i18n('mutualizedNameFieldPlaceholder')
        }), /*#__PURE__*/_jsx(SelectGroup, {
          label: i18n('mutualizedAccessFieldLabel'),
          value: form.visibility,
          onChange: function onChange(newValue) {
            return setForm(function (oldForm) {
              return _objectSpread(_objectSpread({}, oldForm), {}, {
                visibility: newValue
              });
            });
          },
          fullWidth: true,
          placeholder: i18n('mutualizedAccessFieldPlaceholder'),
          children: Object.keys(COLLECTION_VISIBILITY_OPTIONS).map(function (visibilityId) {
            var _COLLECTION_VISIBILIT;
            return /*#__PURE__*/_jsx(MenuItem, {
              value: visibilityId,
              children: i18n((_COLLECTION_VISIBILIT = COLLECTION_VISIBILITY_OPTIONS[visibilityId]) === null || _COLLECTION_VISIBILIT === void 0 ? void 0 : _COLLECTION_VISIBILIT.i18nStr)
            }, visibilityId);
          })
        }), /*#__PURE__*/_jsx(SelectGroup, {
          fullWidth: true,
          placeholder: i18n('mutualizedFilterTypePlaceholder'),
          label: i18n('mutualizedFilterTypeLabel'),
          value: form.type,
          onChange: function onChange(newValue) {
            var _form$data2;
            setForm(function (oldForm) {
              return _objectSpread(_objectSpread({}, oldForm), {}, {
                type: newValue
              });
            });
            if (newValue === COLLECTION_TYPE_IDS.QUERY && typeof ((_form$data2 = form.data) === null || _form$data2 === void 0 ? void 0 : _form$data2.filters) !== 'string') {
              changeFilters('');
            }
          },
          children: Object.keys(COLLECTION_TYPES_OPTIONS).map(function (typeId) {
            var _COLLECTION_TYPES_OPT, _COLLECTION_TYPES_OPT2, _COLLECTION_TYPES_OPT3;
            return /*#__PURE__*/_jsx(MenuItem, {
              value: typeId,
              disabled: (_COLLECTION_TYPES_OPT = (_COLLECTION_TYPES_OPT2 = COLLECTION_TYPES_OPTIONS[typeId]) === null || _COLLECTION_TYPES_OPT2 === void 0 ? void 0 : _COLLECTION_TYPES_OPT2.disabled) !== null && _COLLECTION_TYPES_OPT !== void 0 ? _COLLECTION_TYPES_OPT : false,
              children: i18n((_COLLECTION_TYPES_OPT3 = COLLECTION_TYPES_OPTIONS[typeId]) === null || _COLLECTION_TYPES_OPT3 === void 0 ? void 0 : _COLLECTION_TYPES_OPT3.i18nStr)
            }, typeId);
          })
        })]
      }), /*#__PURE__*/_jsx("div", {
        children: _typeof((_form$data3 = form.data) === null || _form$data3 === void 0 ? void 0 : _form$data3.filters) === 'object' ? /*#__PURE__*/_jsx(_Fragment, {
          children: /*#__PURE__*/_jsx(Typography, {
            colorVariant: PC.Warning,
            children: i18n('collectionDialogFiltersTypeNotSupported')
          })
        }) : /*#__PURE__*/_jsx(InputGroup, {
          label: /*#__PURE__*/_jsxs(Styled.Label, {
            children: [i18n((_COLLECTION_TYPES_OPT4 = COLLECTION_TYPES_OPTIONS[form.type]) === null || _COLLECTION_TYPES_OPT4 === void 0 ? void 0 : _COLLECTION_TYPES_OPT4.i18nStr), form.type === COLLECTION_TYPE_IDS.QUERY && /*#__PURE__*/_jsx(TooltipV2, {
              arrow: true,
              position: "right",
              size: "md",
              title: /*#__PURE__*/_jsx("div", {
                dangerouslySetInnerHTML: {
                  __html: i18n('collectionDialogFilterTypeQueryTooltip', {
                    link: "<a href=\"https://docs.filerobot.com\" target=\"_blank\" rel=\"noopener noreferrer\">".concat(i18n('mutualizedFilerobotQueryLanguageDocLink'), "</a>")
                  })
                }
              }),
              children: /*#__PURE__*/_jsx(InfoOutline, {
                size: 12,
                color: iconPrimaryColor
              })
            })]
          }),
          placeholder: "season=summer&size>1000&status=approved",
          fullWidth: true,
          type: "textarea",
          value: ((_form$data4 = form.data) === null || _form$data4 === void 0 ? void 0 : _form$data4.filters) || '',
          onChange: function onChange(_ref4) {
            var value = _ref4.target.value;
            return changeFilters(value);
          }
        })
      }), /*#__PURE__*/_jsx(Groups, {
        form: form,
        setForm: setForm,
        attrKeys: groupableAttrKeys,
        submitted: submitted,
        addGroup: handleAddGroup
      })]
    });
  };
  return /*#__PURE__*/_jsxs(Styled.Modal, {
    onClose: handleCancel,
    open: true,
    style: {
      maxWidth: 1100
    },
    fullWidth: true,
    children: [/*#__PURE__*/_jsx(ModalTitle, {
      onClose: handleCancel,
      primary: i18n(isNew ? 'collectionDialogCreateCollectionLabel' : 'collectionDialogEditCollectionLabel')
    }), /*#__PURE__*/_jsx(ModalContent, {
      children: renderContent()
    }), /*#__PURE__*/_jsxs(ModalActions, {
      align: "right",
      children: [/*#__PURE__*/_jsx(Button, {
        size: "lg",
        color: "basic",
        onClick: handleCancel,
        children: i18n('mutualizedCancelButtonLabel')
      }), /*#__PURE__*/_jsx(Button, {
        color: "primary",
        size: "lg",
        onClick: handleCreateCollection,
        loading: isLoading,
        disabled: searchSuggestionsAttributesKeysLoading || isNew && (!(form !== null && form !== void 0 && (_form$data5 = form.data) !== null && _form$data5 !== void 0 && _form$data5.filters) || !(form !== null && form !== void 0 && (_form$data6 = form.data) !== null && _form$data6 !== void 0 && (_form$data6$filters = _form$data6.filters) !== null && _form$data6$filters !== void 0 && _form$data6$filters.length)) && !(form !== null && form !== void 0 && (_form$data7 = form.data) !== null && _form$data7 !== void 0 && (_form$data7$groupby = _form$data7.groupby) !== null && _form$data7$groupby !== void 0 && _form$data7$groupby.length),
        children: i18n(isNew ? 'collectionDialogCreateCollectionButton' : 'collectionDialogEditCollectionButton')
      })]
    })]
  });
};
export default CollectionModal;