UNPKG

@drieam/common

Version:

Default Drieam api wrapper

81 lines (65 loc) 3.37 kB
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import * as api from "@drieam/api"; import Notification from "antd/lib/notification"; import { get } from "../../utils"; import * as types from "../../types"; var defaultNotify = Notification; var ENTITY_ACTIONS = [api.SAVE_ENTITY_SUCCESS, api.SAVE_ENTITY_FAILED, api.SAVE_LIST_SUCCESS, api.DELETE_ENTITY_SUCCESS, api.DELETE_ENTITY_FAILED]; function errorHandlingMiddleware(parentOptions) { var notificationApi = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultNotify; return function (store) { function dispatchOnline() { store.dispatch({ type: types.APP_ONLINE, payload: window.navigator.onLine }); } window.addEventListener("online", dispatchOnline); window.addEventListener("offline", dispatchOnline); window.addEventListener("load", dispatchOnline); return function (next) { return function (action) { var opts = _objectSpread(_objectSpread({}, parentOptions), action.options); if (action.type === types.APP_ONLINE) { var onlineAction = action; if (onlineAction.payload) { notificationApi.close("offline"); } else { notificationApi.error(_objectSpread(_objectSpread({}, get(parentOptions, "toasts.offline")), {}, { key: "offline" })); } } else if (ENTITY_ACTIONS.includes(action.type)) { var toasts = opts.toasts; switch (action.type) { case api.SAVE_ENTITY_SUCCESS: case api.SAVE_LIST_SUCCESS: if (!get(toasts, "save")) { break; } notificationApi.success(toasts.save); break; case api.DELETE_ENTITY_SUCCESS: if (!get(toasts, "delete")) { break; } notificationApi.success(toasts.delete); break; case api.DELETE_ENTITY_FAILED: case api.SAVE_ENTITY_FAILED: if (!toasts.failed) { break; } notificationApi.error(toasts.failed); break; default: } } return next(action); }; }; }; } export { errorHandlingMiddleware };