UNPKG

@commercetools-frontend/actions-global

Version:
217 lines (195 loc) • 10.4 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var history = require('@commercetools-frontend/browser-history'); var constants = require('@commercetools-frontend/constants'); var sentry = require('@commercetools-frontend/sentry'); var _Array$isArray = require('@babel/runtime-corejs3/core-js-stable/array/is-array'); var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty'); require('@babel/runtime-corejs3/core-js-stable/instance/includes'); require('@babel/runtime-corejs3/core-js-stable/object/values'); var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys'); var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols'); var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter'); var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor'); var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each'); var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors'); var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties'); var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property'); var isNumber = require('lodash/isNumber'); var notifications = require('@commercetools-frontend/notifications'); var react = require('react'); var reactRedux = require('react-redux'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var history__default = /*#__PURE__*/_interopDefault(history); var _Array$isArray__default = /*#__PURE__*/_interopDefault(_Array$isArray); var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys); var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols); var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty); var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor); var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty); var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors); var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties); var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty); var isNumber__default = /*#__PURE__*/_interopDefault(isNumber); // NOTE: This string will be replaced on build time with the package version. var version = "24.2.0"; function ownKeys$1(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var _context2, _context3; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context2 = ownKeys$1(Object(t), !0)).call(_context2, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context3 = ownKeys$1(Object(t))).call(_context3, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; } function showNotification(notification) { let meta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; let dismissAfter = meta.dismissAfter; if (!isNumber__default["default"](dismissAfter)) dismissAfter = notification.kind === constants.NOTIFICATION_KINDS_SIDE.success ? 5000 : 0; return notifications.addNotification(notification, _objectSpread$1(_objectSpread$1({}, meta), {}, { dismissAfter })); } function showApiErrorNotification(_ref) { let errors = _ref.errors; return showNotification({ id: 0, domain: constants.DOMAINS.PAGE, kind: 'api-error', values: { // NOTE: Some sources or errors (e.g. GraphQL) return an array or object. // The cast into an array happens here so that consumers can pass both types. errors: _Array$isArray__default["default"](errors) ? errors : [errors] } }, { dismissAfter: 0 }); } function showUnexpectedErrorNotification() { let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, errorId = _ref.errorId; return showNotification({ id: 0, domain: constants.DOMAINS.PAGE, kind: 'unexpected-error', values: { errorId } }, { dismissAfter: 0 }); } function isApiError(error) { return error.body !== undefined; } function handleActionError(error) { return dispatch => { // On production we send the errors to Sentry. // eslint-disable-next-line no-console if (window.app.env !== 'production') console.error(error, error instanceof Error && error.stack); if (!isApiError(error)) { const errorId = sentry.reportErrorToSentry(error); return dispatch(showUnexpectedErrorNotification({ errorId })); } // When unauthorized, log the user out. if (error.statusCode === constants.STATUS_CODES.UNAUTHORIZED) { history__default["default"].push(`/logout?reason=${constants.LOGOUT_REASONS.UNAUTHORIZED}`); } // We need to do the return, because if not we see an error notification // The error is handled with the handleUnavailableResource HoC to show the PageNotFound component // when the api returns 404 if (error.statusCode === constants.STATUS_CODES.NOT_FOUND) return null; return dispatch(showApiErrorNotification({ errors: error.body.errors || [ // Pass a fallback error so that our error components can handle it { message: error.body.message }] })); }; } function hideAllPageNotifications() { return { type: constants.HIDE_ALL_PAGE_NOTIFICATIONS }; } function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context, _context2; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context = ownKeys(Object(t), !0)).call(_context, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context2 = ownKeys(Object(t))).call(_context2, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; } /** * Dispatch a notification. * * @example * const showSuccessNotification = useShowNotification(); * showSuccessNotification({ * domain: NOTIFICATION_DOMAINS.SIDE, * kind: NOTIFICATION_KINDS_SIDE.success, * text: "All good!", * }); */ /** * Dispatch a notification. * * @deprecated: Avoid passing the notification options here. * Instead define them in the notification function itself. * * @example * Bad: * const showSuccessNotification = useShowNotification({ * domain: NOTIFICATION_DOMAINS.SIDE, * kind: NOTIFICATION_KINDS_SIDE.success, * }); * showSuccessNotification({ * text: "All good!", * }); * * Good: * const showSuccessNotification = useShowNotification(); * showSuccessNotification({ * domain: NOTIFICATION_DOMAINS.SIDE, * kind: NOTIFICATION_KINDS_SIDE.success, * text: "All good!", * }); */ function useShowNotification(notificationFragment) { const dispatch = reactRedux.useDispatch(); return react.useCallback((content, meta) => { const notification = showNotification(_objectSpread(_objectSpread({}, notificationFragment), content), meta); return dispatch(notification); }, [dispatch, notificationFragment]); } /** * Dispatch an API error notification. * * @example * const showApiErrorNotification = useShowApiErrorNotification(); * showApiErrorNotification({ errors }); */ function useShowApiErrorNotification() { const dispatch = reactRedux.useDispatch(); return react.useCallback(options => dispatch(showApiErrorNotification(options)), [dispatch]); } /** * Dispatch an unexpected error notification. * * @example * const showUnexpectedErrorNotification = useShowUnexpectedErrorNotification(); * showUnexpectedErrorNotification({ errors }); */ function useShowUnexpectedErrorNotification() { const dispatch = reactRedux.useDispatch(); return react.useCallback(options => dispatch(showUnexpectedErrorNotification(options)), [dispatch]); } function useHideAllPageNotifications() { const dispatch = reactRedux.useDispatch(); return react.useCallback(() => dispatch(hideAllPageNotifications()), [dispatch]); } function useOnActionError() { const dispatch = reactRedux.useDispatch(); return react.useCallback(error => dispatch(handleActionError(error)), [dispatch]); } exports.handleActionError = handleActionError; exports.hideAllPageNotifications = hideAllPageNotifications; exports.showApiErrorNotification = showApiErrorNotification; exports.showNotification = showNotification; exports.showUnexpectedErrorNotification = showUnexpectedErrorNotification; exports.useHideAllPageNotifications = useHideAllPageNotifications; exports.useOnActionError = useOnActionError; exports.useShowApiErrorNotification = useShowApiErrorNotification; exports.useShowNotification = useShowNotification; exports.useShowUnexpectedErrorNotification = useShowUnexpectedErrorNotification; exports.version = version;