@webiny/app 0.0.0-mt-1

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.
Files changed (86) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/apollo-client/InMemoryCache.d.ts +7 -0
  4. package/apollo-client/InMemoryCache.js +59 -0
  5. package/components/Image.d.ts +3 -0
  6. package/components/Image.js +28 -0
  7. package/components/Routes.d.ts +2 -0
  8. package/components/Routes.js +35 -0
  9. package/components/View.d.ts +8 -0
  10. package/components/View.js +23 -0
  11. package/components/index.d.ts +1 -0
  12. package/components/index.js +1 -0
  13. package/contexts/Ui/index.d.ts +27 -0
  14. package/contexts/Ui/index.js +69 -0
  15. package/hooks/useAutocomplete/index.d.ts +1 -0
  16. package/hooks/useAutocomplete/index.js +1 -0
  17. package/hooks/useAutocomplete/useAutocomplete.d.ts +6 -0
  18. package/hooks/useAutocomplete/useAutocomplete.js +27 -0
  19. package/hooks/useDataList/functions/getData.d.ts +2 -0
  20. package/hooks/useDataList/functions/getData.js +4 -0
  21. package/hooks/useDataList/functions/getError.d.ts +2 -0
  22. package/hooks/useDataList/functions/getError.js +4 -0
  23. package/hooks/useDataList/functions/getMeta.d.ts +2 -0
  24. package/hooks/useDataList/functions/getMeta.js +4 -0
  25. package/hooks/useDataList/functions/index.d.ts +3 -0
  26. package/hooks/useDataList/functions/index.js +3 -0
  27. package/hooks/useDataList/functions/searchDataByKey.d.ts +2 -0
  28. package/hooks/useDataList/functions/searchDataByKey.js +21 -0
  29. package/hooks/useDataList/index.d.ts +1 -0
  30. package/hooks/useDataList/index.js +1 -0
  31. package/hooks/useDataList/useDataList.d.ts +36 -0
  32. package/hooks/useDataList/useDataList.js +193 -0
  33. package/hooks/useDataList/utils/index.d.ts +2 -0
  34. package/hooks/useDataList/utils/index.js +2 -0
  35. package/hooks/useDataList/utils/prepareLoadListParams.d.ts +4 -0
  36. package/hooks/useDataList/utils/prepareLoadListParams.js +34 -0
  37. package/hooks/useDataList/utils/redirectToRouteWithQueryParams.d.ts +5 -0
  38. package/hooks/useDataList/utils/redirectToRouteWithQueryParams.js +22 -0
  39. package/hooks/useDataList/utils/types.d.ts +34 -0
  40. package/hooks/useDataList/utils/types.js +1 -0
  41. package/hooks/useHandler.d.ts +1 -0
  42. package/hooks/useHandler.js +12 -0
  43. package/hooks/useHandlers.d.ts +11 -0
  44. package/hooks/useHandlers.js +19 -0
  45. package/hooks/useUi.d.ts +2 -0
  46. package/hooks/useUi.js +5 -0
  47. package/i18n/i18n.d.ts +2 -0
  48. package/i18n/i18n.js +5 -0
  49. package/i18n/index.d.ts +1 -0
  50. package/i18n/index.js +1 -0
  51. package/package.json +70 -0
  52. package/plugins/AddQuerySelectionPlugin.d.ts +15 -0
  53. package/plugins/AddQuerySelectionPlugin.js +101 -0
  54. package/plugins/ApolloCacheObjectIdPlugin.d.ts +15 -0
  55. package/plugins/ApolloCacheObjectIdPlugin.js +40 -0
  56. package/plugins/ApolloDynamicLink.d.ts +5 -0
  57. package/plugins/ApolloDynamicLink.js +62 -0
  58. package/plugins/ApolloLinkPlugin.d.ts +13 -0
  59. package/plugins/ApolloLinkPlugin.js +43 -0
  60. package/plugins/ConsoleLinkPlugin.d.ts +8 -0
  61. package/plugins/ConsoleLinkPlugin.js +47 -0
  62. package/plugins/LocaleHeaderLinkPlugin.d.ts +14 -0
  63. package/plugins/LocaleHeaderLinkPlugin.js +61 -0
  64. package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.d.ts +6 -0
  65. package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js +76 -0
  66. package/plugins/NetworkErrorLinkPlugin/StyledComponents.d.ts +3 -0
  67. package/plugins/NetworkErrorLinkPlugin/StyledComponents.js +9 -0
  68. package/plugins/NetworkErrorLinkPlugin/assets/close_24px.svg +1 -0
  69. package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.d.ts +5 -0
  70. package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js +27 -0
  71. package/plugins/NetworkErrorLinkPlugin.d.ts +7 -0
  72. package/plugins/NetworkErrorLinkPlugin.js +45 -0
  73. package/plugins/OmitTypenameLinkPlugin.d.ts +8 -0
  74. package/plugins/OmitTypenameLinkPlugin.js +41 -0
  75. package/plugins/RoutePlugin.d.ts +12 -0
  76. package/plugins/RoutePlugin.js +36 -0
  77. package/plugins/TenantHeaderLinkPlugin.d.ts +14 -0
  78. package/plugins/TenantHeaderLinkPlugin.js +71 -0
  79. package/plugins/ViewPlugin.d.ts +14 -0
  80. package/plugins/ViewPlugin.js +41 -0
  81. package/plugins/image.d.ts +3 -0
  82. package/plugins/image.js +165 -0
  83. package/plugins/index.d.ts +20 -0
  84. package/plugins/index.js +73 -0
  85. package/types.d.ts +62 -0
  86. package/types.js +2 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @webiny/app
2
+ [![](https://img.shields.io/npm/dw/@webiny/app.svg)](https://www.npmjs.com/package/@webiny/app)
3
+ [![](https://img.shields.io/npm/v/@webiny/app.svg)](https://www.npmjs.com/package/@webiny/app)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ The base package for building Webiny and React powered web apps.
8
+
9
+ For more information, please visit [the official docs](https://docs.webiny.com/docs/webiny/introduction).
10
+
11
+ ## Install
12
+ ```
13
+ npm install --save @webiny/app
14
+ ```
15
+
16
+ Or if you prefer yarn:
17
+ ```
18
+ yarn add @webiny/app
19
+ ```
@@ -0,0 +1,7 @@
1
+ import { InMemoryCache as BaseInMemoryCache, InMemoryCacheConfig } from "apollo-cache-inmemory";
2
+ import { DocumentNode } from "graphql";
3
+ export declare class InMemoryCache extends BaseInMemoryCache {
4
+ private transformPlugins;
5
+ constructor(config?: InMemoryCacheConfig);
6
+ transformDocument(document: DocumentNode): DocumentNode;
7
+ }
@@ -0,0 +1,59 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/createForOfIteratorHelper";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
5
+ import _get from "@babel/runtime/helpers/get";
6
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7
+ import _inherits from "@babel/runtime/helpers/inherits";
8
+ import _createSuper from "@babel/runtime/helpers/createSuper";
9
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
10
+ import { InMemoryCache as BaseInMemoryCache } from "apollo-cache-inmemory";
11
+ import { plugins } from "@webiny/plugins";
12
+ import { AddQuerySelectionPlugin } from "../plugins/AddQuerySelectionPlugin";
13
+ import { ApolloLinkPlugin } from "../plugins/ApolloLinkPlugin";
14
+ export var InMemoryCache = /*#__PURE__*/function (_BaseInMemoryCache) {
15
+ _inherits(InMemoryCache, _BaseInMemoryCache);
16
+
17
+ var _super = _createSuper(InMemoryCache);
18
+
19
+ function InMemoryCache(config) {
20
+ var _this;
21
+
22
+ _classCallCheck(this, InMemoryCache);
23
+
24
+ _this = _super.call(this, config);
25
+
26
+ _defineProperty(_assertThisInitialized(_this), "transformPlugins", void 0);
27
+
28
+ _this.transformPlugins = plugins.byType(ApolloLinkPlugin.type).filter(function (pl) {
29
+ return pl instanceof AddQuerySelectionPlugin;
30
+ });
31
+ return _this;
32
+ }
33
+
34
+ _createClass(InMemoryCache, [{
35
+ key: "transformDocument",
36
+ value: function transformDocument(document) {
37
+ // @ts-ignore
38
+ var operationName = document.definitions[0].name.value;
39
+
40
+ var _iterator = _createForOfIteratorHelper(this.transformPlugins),
41
+ _step;
42
+
43
+ try {
44
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
45
+ var pl = _step.value;
46
+ pl.addSelectionToQuery(operationName, document);
47
+ }
48
+ } catch (err) {
49
+ _iterator.e(err);
50
+ } finally {
51
+ _iterator.f();
52
+ }
53
+
54
+ return _get(_getPrototypeOf(InMemoryCache.prototype), "transformDocument", this).call(this, document);
55
+ }
56
+ }]);
57
+
58
+ return InMemoryCache;
59
+ }(BaseInMemoryCache);
@@ -0,0 +1,3 @@
1
+ import * as React from "react";
2
+ import { ImageProps } from "../types";
3
+ export declare const Image: React.FC<ImageProps>;
@@ -0,0 +1,28 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
+ import _get from "lodash/get";
3
+ var _excluded = ["preset"];
4
+ import invariant from "invariant";
5
+ import { plugins } from "@webiny/plugins";
6
+ export var Image = function Image(_ref) {
7
+ var presetName = _ref.preset,
8
+ props = _objectWithoutProperties(_ref, _excluded);
9
+
10
+ var plugin = plugins.byName("image-component");
11
+
12
+ if (!plugin) {
13
+ throw new Error("Image component plugin (type \"image-component\") not defined.");
14
+ }
15
+
16
+ if (presetName) {
17
+ var preset = _get(plugin, "presets.".concat(presetName));
18
+
19
+ invariant(preset, "Transform preset \"".concat(presetName, "\" not found."));
20
+ props.transform = preset;
21
+ }
22
+
23
+ if (props.transform) {
24
+ props.src = plugin.getImageSrc(props);
25
+ }
26
+
27
+ return plugin.render(props);
28
+ };
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export declare const Routes: () => JSX.Element;
@@ -0,0 +1,35 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import React from "react";
3
+ import { plugins } from "@webiny/plugins";
4
+ import { Switch } from "@webiny/react-router";
5
+ export var Routes = function Routes() {
6
+ // We cannot call `sort` on the array returned by the `plugins.byType` call - it is read-only.
7
+ var routes = _toConsumableArray(plugins.byType("route")).sort(function (a, b) {
8
+ var pathA = a.route.props.path || "*";
9
+ var pathB = b.route.props.path || "*"; // This will sort paths at the very bottom of the list
10
+
11
+ if (pathA === "/" && pathB === "*") {
12
+ return -1;
13
+ } // This will push * and / to the bottom of the list
14
+
15
+
16
+ if (pathA === "*" || pathA === "/") {
17
+ return 1;
18
+ } // This will push * and / to the bottom of the list
19
+
20
+
21
+ if (["*", "/"].includes(pathB)) {
22
+ return -1;
23
+ }
24
+
25
+ return 0;
26
+ });
27
+
28
+ return /*#__PURE__*/React.createElement(Switch, null, routes.map(function (_ref) {
29
+ var route = _ref.route,
30
+ name = _ref.name;
31
+ return /*#__PURE__*/React.cloneElement(route, {
32
+ key: name
33
+ });
34
+ }));
35
+ };
@@ -0,0 +1,8 @@
1
+ import React from "react";
2
+ interface Props {
3
+ name: string;
4
+ props?: Record<string, any>;
5
+ children?: React.ReactNode;
6
+ }
7
+ export declare const View: ({ name, children, props }: Props) => JSX.Element;
8
+ export {};
@@ -0,0 +1,23 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import React, { Fragment } from "react";
3
+ import { plugins } from "@webiny/plugins";
4
+ import { ViewPlugin } from "../plugins/ViewPlugin";
5
+ export var View = function View(_ref) {
6
+ var name = _ref.name,
7
+ children = _ref.children,
8
+ _ref$props = _ref.props,
9
+ props = _ref$props === void 0 ? {} : _ref$props;
10
+ var viewPlugins = plugins.byType(ViewPlugin.type).filter(function (pl) {
11
+ return pl.key === name;
12
+ });
13
+
14
+ if (viewPlugins.length) {
15
+ children = viewPlugins.reduce(function (el, pl) {
16
+ return pl.render(_objectSpread({
17
+ children: el
18
+ }, props));
19
+ }, children);
20
+ }
21
+
22
+ return /*#__PURE__*/React.createElement(Fragment, null, children || null);
23
+ };
@@ -0,0 +1 @@
1
+ export { Image } from "./Image";
@@ -0,0 +1 @@
1
+ export { Image } from "./Image";
@@ -0,0 +1,27 @@
1
+ import React from "react";
2
+ export declare const UiContext: React.Context<{}>;
3
+ declare type Props = {};
4
+ declare type State = {
5
+ ui: {
6
+ [key: string]: any;
7
+ };
8
+ };
9
+ interface UiData {
10
+ [key: string]: any;
11
+ }
12
+ interface UiDataSetter {
13
+ (ui: UiData): UiData;
14
+ }
15
+ export interface UiContextValue {
16
+ setState: (setter: UiDataSetter) => void;
17
+ [key: string]: any;
18
+ }
19
+ export declare class UiProvider extends React.Component<Props, State> {
20
+ state: State;
21
+ setData: (setter: Function) => void;
22
+ render(): JSX.Element;
23
+ }
24
+ export declare const UiConsumer: ({ children }: {
25
+ children: any;
26
+ }) => JSX.Element;
27
+ export {};
@@ -0,0 +1,69 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
5
+ import _inherits from "@babel/runtime/helpers/inherits";
6
+ import _createSuper from "@babel/runtime/helpers/createSuper";
7
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
8
+ import React from "react";
9
+ import { plugins } from "@webiny/plugins";
10
+ export var UiContext = /*#__PURE__*/React.createContext({});
11
+ export var UiProvider = /*#__PURE__*/function (_React$Component) {
12
+ _inherits(UiProvider, _React$Component);
13
+
14
+ var _super = _createSuper(UiProvider);
15
+
16
+ function UiProvider() {
17
+ var _this;
18
+
19
+ _classCallCheck(this, UiProvider);
20
+
21
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
22
+ args[_key] = arguments[_key];
23
+ }
24
+
25
+ _this = _super.call.apply(_super, [this].concat(args));
26
+
27
+ _defineProperty(_assertThisInitialized(_this), "state", {
28
+ ui: {}
29
+ });
30
+
31
+ _defineProperty(_assertThisInitialized(_this), "setData", function (setter) {
32
+ return _this.setState(function (state) {
33
+ return {
34
+ ui: _objectSpread(_objectSpread({}, state.ui), setter(state.ui))
35
+ };
36
+ });
37
+ });
38
+
39
+ return _this;
40
+ }
41
+
42
+ _createClass(UiProvider, [{
43
+ key: "render",
44
+ value: function render() {
45
+ var value = _objectSpread(_objectSpread({}, this.state.ui), {}, {
46
+ setState: this.setData
47
+ });
48
+
49
+ var uiStatePlugins = plugins.byType("ui-state");
50
+ return /*#__PURE__*/React.createElement(UiContext.Provider, {
51
+ value: value
52
+ }, uiStatePlugins.map(function (pl) {
53
+ return /*#__PURE__*/React.cloneElement(pl.render(), {
54
+ key: pl.name
55
+ });
56
+ }), this.props.children);
57
+ }
58
+ }]);
59
+
60
+ return UiProvider;
61
+ }(React.Component);
62
+ export var UiConsumer = function UiConsumer(_ref) {
63
+ var children = _ref.children;
64
+ return /*#__PURE__*/React.createElement(UiContext.Consumer, null, function (ui) {
65
+ return /*#__PURE__*/React.cloneElement(children, {
66
+ ui: ui
67
+ });
68
+ });
69
+ };
@@ -0,0 +1 @@
1
+ export { useAutocomplete } from "./useAutocomplete";
@@ -0,0 +1 @@
1
+ export { useAutocomplete } from "./useAutocomplete";
@@ -0,0 +1,6 @@
1
+ interface UseAutocompleteHook {
2
+ options: any[];
3
+ onInput(value: string): void;
4
+ }
5
+ export declare const useAutocomplete: (props: any) => UseAutocompleteHook;
6
+ export {};
@@ -0,0 +1,27 @@
1
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import _debounce from "lodash/debounce";
3
+ import { useDataList } from "../useDataList";
4
+ export var useAutocomplete = function useAutocomplete(props) {
5
+ var useDataListProps = props.query ? props : {
6
+ query: props
7
+ };
8
+ var dataList = useDataList(_objectSpread({
9
+ useRouter: false
10
+ }, useDataListProps));
11
+ return {
12
+ options: dataList.data || [],
13
+ onInput: _debounce(function (query) {
14
+ if (!query) {
15
+ return;
16
+ }
17
+
18
+ var search = props.search || query;
19
+
20
+ if (typeof search === "function") {
21
+ search = search(query);
22
+ }
23
+
24
+ dataList.setSearch(search);
25
+ }, 250)
26
+ };
27
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: (response: any) => any;
2
+ export default _default;
@@ -0,0 +1,4 @@
1
+ import searchDataByKey from "./searchDataByKey";
2
+ export default (function (response) {
3
+ return searchDataByKey("data", response);
4
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: (response: any) => any;
2
+ export default _default;
@@ -0,0 +1,4 @@
1
+ import searchDataByKey from "./searchDataByKey";
2
+ export default (function (response) {
3
+ return searchDataByKey("error", response);
4
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: (response: any) => any;
2
+ export default _default;
@@ -0,0 +1,4 @@
1
+ import searchDataByKey from "./searchDataByKey";
2
+ export default (function (response) {
3
+ return searchDataByKey("meta", response);
4
+ });
@@ -0,0 +1,3 @@
1
+ export { default as getData } from "./getData";
2
+ export { default as getError } from "./getError";
3
+ export { default as getMeta } from "./getMeta";
@@ -0,0 +1,3 @@
1
+ export { default as getData } from "./getData";
2
+ export { default as getError } from "./getError";
3
+ export { default as getMeta } from "./getMeta";
@@ -0,0 +1,2 @@
1
+ declare const searchDataByKey: (searchKey: any, object: any) => any;
2
+ export default searchDataByKey;
@@ -0,0 +1,21 @@
1
+ var searchDataByKey = function searchDataByKey(searchKey, object) {
2
+ if (!object || typeof object !== "object") {
3
+ return null;
4
+ }
5
+
6
+ if (object[searchKey]) {
7
+ return object[searchKey];
8
+ }
9
+
10
+ for (var key in object) {
11
+ var value = searchDataByKey(searchKey, object[key]);
12
+
13
+ if (value) {
14
+ return value;
15
+ }
16
+ }
17
+
18
+ return null;
19
+ };
20
+
21
+ export default searchDataByKey;
@@ -0,0 +1 @@
1
+ export { useDataList } from "./useDataList";
@@ -0,0 +1 @@
1
+ export { useDataList } from "./useDataList";
@@ -0,0 +1,36 @@
1
+ import { DocumentNode } from "graphql";
2
+ import { ApolloClient } from "apollo-client";
3
+ export declare type UseDataListParams = {
4
+ useRouter?: boolean;
5
+ variables?: ((params: UseDataListParams) => any) | object;
6
+ client?: ApolloClient<any>;
7
+ query: DocumentNode;
8
+ getData?: (data: any) => any;
9
+ getMeta?: (data: any) => any;
10
+ getError?: (data: any) => any;
11
+ };
12
+ export declare type DataListProps = {
13
+ __loadParams: any;
14
+ refresh: (params?: any) => void;
15
+ data: any[];
16
+ meta: any;
17
+ error: any;
18
+ loading: boolean;
19
+ isSelected: (item: any) => boolean;
20
+ select: (item: any) => void;
21
+ isMultiSelected: (item: any) => boolean;
22
+ isNoneMultiSelected: () => boolean;
23
+ isAllMultiSelected: () => boolean;
24
+ multiSelectAll: (value: boolean) => void;
25
+ getMultiSelected: () => any[];
26
+ setSearch: (search: any) => void;
27
+ setWhere: (where: any) => void;
28
+ setSorters: (sort: any) => void;
29
+ setPerPage: (perPage: number) => void;
30
+ setPreviousPage: (cursor: string) => void;
31
+ setNextPage: (cursor: string) => void;
32
+ multiSelect: (items: string | string[], value?: boolean) => void;
33
+ init: () => void;
34
+ };
35
+ declare const useDataList: (params: UseDataListParams) => DataListProps;
36
+ export { useDataList };
@@ -0,0 +1,193 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _objectSpread from "@babel/runtime/helpers/objectSpread2";
3
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
4
+ import _isEqual from "lodash/isEqual";
5
+ import _get from "lodash/get";
6
+ import { useState, useEffect, useRef, useCallback } from "react";
7
+ import { useQuery } from "@apollo/react-hooks";
8
+ import { useRouter } from "@webiny/react-router";
9
+ import { prepareLoadListParams } from "./utils";
10
+ import { getData, getError, getMeta } from "./functions";
11
+
12
+ var useDataList = function useDataList(params) {
13
+ var _useState = useState([]),
14
+ _useState2 = _slicedToArray(_useState, 2),
15
+ multiSelectedItems = _useState2[0],
16
+ _multiSelect = _useState2[1];
17
+
18
+ var history = null;
19
+ var location = null;
20
+ var routerHook = useRouter();
21
+
22
+ if (params.useRouter !== false) {
23
+ history = routerHook.history;
24
+ location = routerHook.location;
25
+ }
26
+
27
+ var getQueryOptions = useCallback(function () {
28
+ var variables = params.variables;
29
+
30
+ if (typeof variables === "function") {
31
+ variables = variables(params);
32
+ }
33
+
34
+ return {
35
+ client: params.client,
36
+ variables: _objectSpread(_objectSpread({}, variables), prepareLoadListParams(location))
37
+ };
38
+ }, undefined);
39
+ var queryData = useQuery(params.query, getQueryOptions());
40
+ var prevLoadParamsRef = useRef({});
41
+ var dataListProps = {
42
+ data: _get(params, "getData", getData)(queryData.data),
43
+ meta: _get(params, "getMeta", getMeta)(queryData.data),
44
+ error: _get(params, "getError", getError)(queryData.data),
45
+ loading: queryData.loading,
46
+ init: function init() {
47
+ this.refresh();
48
+ },
49
+ refresh: function refresh() {
50
+ var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
51
+
52
+ // Refresh multi select first.
53
+ _multiSelect([]);
54
+
55
+ if (!params) {
56
+ queryData.refetch(dataListProps.__loadParams);
57
+ return;
58
+ } // if (history) {
59
+ // redirectToRouteWithQueryParams(params, { history, location });
60
+ // return;
61
+ // }
62
+
63
+
64
+ queryData.refetch(params);
65
+ },
66
+ setPerPage: function setPerPage(perPage) {
67
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
68
+ limit: parseInt("" + perPage),
69
+ after: undefined,
70
+ before: undefined
71
+ });
72
+
73
+ this.refresh(preparedParams);
74
+ },
75
+ setNextPage: function setNextPage(cursor) {
76
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
77
+ after: cursor,
78
+ before: undefined
79
+ });
80
+
81
+ this.refresh(preparedParams);
82
+ },
83
+ setPreviousPage: function setPreviousPage(cursor) {
84
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
85
+ after: undefined,
86
+ before: cursor
87
+ });
88
+
89
+ this.refresh(preparedParams);
90
+ },
91
+ setSearch: function setSearch(search) {
92
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
93
+ search: search,
94
+ after: undefined,
95
+ before: undefined
96
+ });
97
+
98
+ this.refresh(preparedParams);
99
+ },
100
+ setWhere: function setWhere(where) {
101
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
102
+ where: where,
103
+ after: undefined,
104
+ before: undefined
105
+ });
106
+
107
+ this.refresh(preparedParams);
108
+ },
109
+ setSorters: function setSorters(sort) {
110
+ var preparedParams = _objectSpread(_objectSpread({}, dataListProps.__loadParams), {}, {
111
+ sort: sort,
112
+ after: undefined,
113
+ before: undefined
114
+ });
115
+
116
+ this.refresh(preparedParams);
117
+ },
118
+ multiSelect: function multiSelect(items, value) {
119
+ if (!Array.isArray(items)) {
120
+ items = [items];
121
+ }
122
+
123
+ var returnItems = _toConsumableArray(multiSelectedItems);
124
+
125
+ items.forEach(function (item) {
126
+ if (value === undefined) {
127
+ returnItems.includes(item) ? returnItems.splice(returnItems.indexOf(item), 1) : returnItems.push(item);
128
+ } else {
129
+ if (value === true) {
130
+ !returnItems.includes(item) && returnItems.push(item);
131
+ } else {
132
+ returnItems.includes(item) && returnItems.splice(returnItems.indexOf(item), 1);
133
+ }
134
+ }
135
+ });
136
+
137
+ _multiSelect(returnItems);
138
+ },
139
+ isSelected: function isSelected(item) {
140
+ var query = new URLSearchParams(location.search);
141
+ return query.get("id") === item.id;
142
+ },
143
+ select: function select(item) {
144
+ var query = new URLSearchParams(location.search);
145
+ query.set("id", item.id);
146
+ history.push({
147
+ search: query.toString()
148
+ });
149
+ },
150
+ isMultiSelected: function isMultiSelected(item) {
151
+ if (!Array.isArray(multiSelectedItems)) {
152
+ return false;
153
+ }
154
+
155
+ return multiSelectedItems.includes(item);
156
+ },
157
+ isNoneMultiSelected: function isNoneMultiSelected() {
158
+ return multiSelectedItems.length === 0;
159
+ },
160
+ getMultiSelected: function getMultiSelected() {
161
+ return multiSelectedItems;
162
+ },
163
+ multiSelectAll: function multiSelectAll(value) {
164
+ var data = dataListProps.data;
165
+
166
+ if (Array.isArray(data)) {
167
+ dataListProps.multiSelect(data, value);
168
+ } else {
169
+ dataListProps.multiSelect([], value);
170
+ }
171
+ },
172
+ isAllMultiSelected: function isAllMultiSelected() {
173
+ var data = dataListProps.data;
174
+ return Array.isArray(data) && multiSelectedItems.length === data.length;
175
+ },
176
+ __loadParams: prepareLoadListParams(location)
177
+ };
178
+ useEffect(function () {
179
+ var params = {
180
+ prev: prevLoadParamsRef.current,
181
+ current: dataListProps.__loadParams
182
+ };
183
+
184
+ if (!_isEqual(params.prev, params.current)) {
185
+ dataListProps.init();
186
+ }
187
+
188
+ prevLoadParamsRef.current = params.current;
189
+ });
190
+ return dataListProps;
191
+ };
192
+
193
+ export { useDataList };