@commercetools-frontend/notifications 0.0.0-CRAFT-1791-20251006162610
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/LICENSE +21 -0
- package/README.md +205 -0
- package/dist/commercetools-frontend-notifications.cjs.d.ts +2 -0
- package/dist/commercetools-frontend-notifications.cjs.dev.js +126 -0
- package/dist/commercetools-frontend-notifications.cjs.js +7 -0
- package/dist/commercetools-frontend-notifications.cjs.prod.js +126 -0
- package/dist/commercetools-frontend-notifications.esm.js +102 -0
- package/dist/declarations/src/action-creators.d.ts +3 -0
- package/dist/declarations/src/action-types.d.ts +2 -0
- package/dist/declarations/src/index.d.ts +6 -0
- package/dist/declarations/src/middleware.d.ts +11 -0
- package/dist/declarations/src/reducer.d.ts +2 -0
- package/dist/declarations/src/types.d.ts +22 -0
- package/dist/declarations/src/version.d.ts +2 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) commercetools GmbH
|
|
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,205 @@
|
|
|
1
|
+
# @commercetools-frontend/notifications
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@commercetools-frontend/notifications"><img src="https://badgen.net/npm/v/@commercetools-frontend/notifications" alt="Latest release (latest dist-tag)" /></a> <a href="https://www.npmjs.com/package/@commercetools-frontend/notifications"><img src="https://badgen.net/npm/v/@commercetools-frontend/notifications/next" alt="Latest release (next dist-tag)" /></a> <a href="https://bundlephobia.com/result?p=@commercetools-frontend/notifications"><img src="https://badgen.net/bundlephobia/minzip/@commercetools-frontend/notifications" alt="Minified + GZipped size" /></a> <a href="https://github.com/commercetools/merchant-center-application-kit/blob/main/LICENSE"><img src="https://badgen.net/github/license/commercetools/merchant-center-application-kit" alt="GitHub license" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
A general-purpose notification system built on top of redux.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ npm install --save @commercetools-frontend/notifications
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Action Creator
|
|
16
|
+
|
|
17
|
+
### `addNotification(notification, [options])`
|
|
18
|
+
|
|
19
|
+
#### Arguments
|
|
20
|
+
|
|
21
|
+
1. `notification` _(Object)_: This notification will get added to the list. See
|
|
22
|
+
`Notification`.
|
|
23
|
+
1. `[options]` _(Object)_: If specified, further customizes the behavior of the
|
|
24
|
+
notification.
|
|
25
|
+
|
|
26
|
+
- `[dismissAfter = 0]` _(Number)_: dismiss the component after this duration
|
|
27
|
+
(milliseconds)
|
|
28
|
+
- `[onDismiss]` _(Function)_: Callback which will get called with the
|
|
29
|
+
notification's `id` before it is dismissed.
|
|
30
|
+
|
|
31
|
+
The payload must be a `Notification` object. The `id` will be added by the
|
|
32
|
+
middleware automatically.
|
|
33
|
+
|
|
34
|
+
#### Returns
|
|
35
|
+
|
|
36
|
+
After dispatching a notification to the middleware a `notificationHandle` will
|
|
37
|
+
be returned.
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
{
|
|
41
|
+
id: Number,
|
|
42
|
+
dismiss: Function,
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### Notification
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
{
|
|
50
|
+
id: Number,
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
A notification in the state will always have an `id` which gets added by the
|
|
55
|
+
middleware automatically, plus any props passed as the payload on creation.
|
|
56
|
+
|
|
57
|
+
#### Action Format
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
{
|
|
61
|
+
type: 'ADD_NOTIFICATION',
|
|
62
|
+
payload: Object,
|
|
63
|
+
meta: {
|
|
64
|
+
dismissAfter: Number
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### `removeNotification(id)`
|
|
70
|
+
|
|
71
|
+
#### Arguments
|
|
72
|
+
|
|
73
|
+
1. `id` _(Number)_: This notification will be removed
|
|
74
|
+
|
|
75
|
+
#### Example action
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
{
|
|
79
|
+
type: 'REMOVE_NOTIFICATION',
|
|
80
|
+
payload: Number,
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Middleware
|
|
85
|
+
|
|
86
|
+
The middleware
|
|
87
|
+
|
|
88
|
+
- adds a unique `id` to every notification
|
|
89
|
+
- handles scheduling removal of notifications specifying `dismissAfter`
|
|
90
|
+
- returns a `notificationHandle` for each dispatched notification containing its
|
|
91
|
+
`id` and a `dismiss` function for convenience.
|
|
92
|
+
|
|
93
|
+
### Integration
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
import { createStore, applyMiddleware } from 'redux';
|
|
97
|
+
import { middleware as notificationsMiddleware } from '@commercetools-frontend/notifications';
|
|
98
|
+
import rootReducer from './reducers';
|
|
99
|
+
|
|
100
|
+
// Note: this API requires redux@>=3.1.0
|
|
101
|
+
const store = createStore(
|
|
102
|
+
rootReducer,
|
|
103
|
+
applyMiddleware(notificationsMiddleware)
|
|
104
|
+
);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Reducer
|
|
108
|
+
|
|
109
|
+
Mount the reducer on your notifications store slice.
|
|
110
|
+
|
|
111
|
+
### `reducer`
|
|
112
|
+
|
|
113
|
+
#### Example
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
combineReducers({
|
|
117
|
+
notifications: reducer,
|
|
118
|
+
...more reducers here...
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Action Types
|
|
123
|
+
|
|
124
|
+
Although not required for use in the application, these are exported as well.
|
|
125
|
+
|
|
126
|
+
- `ADD_NOTIFICATION`
|
|
127
|
+
- `REMOVE_NOTIFICATION`
|
|
128
|
+
|
|
129
|
+
They can be used for filtering out notification actions in the logger.
|
|
130
|
+
|
|
131
|
+
## Usage
|
|
132
|
+
|
|
133
|
+
### With React
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
import React from 'react'
|
|
137
|
+
import PropTypes from 'prop-types';
|
|
138
|
+
import { removeNotification } from '@commercetools-frontend/notifications'
|
|
139
|
+
|
|
140
|
+
// Accepts a notification map function,
|
|
141
|
+
// which returns the component based on the notification.
|
|
142
|
+
//
|
|
143
|
+
// function mapNotificationToComponent (notification) {
|
|
144
|
+
// const map = {
|
|
145
|
+
// 'success': SuccessNotification,
|
|
146
|
+
// 'intercom': IntercomNotification,
|
|
147
|
+
// }
|
|
148
|
+
// return map[notification.kind] || InfoNotification
|
|
149
|
+
// }
|
|
150
|
+
//
|
|
151
|
+
// accept notifications, which should come from connecting to the store
|
|
152
|
+
|
|
153
|
+
class LeftNavigation extends React.PureComponent {
|
|
154
|
+
static propTypes: {
|
|
155
|
+
notifications: PropTypes.arrayOf(PropTypes.object).isRequired,
|
|
156
|
+
mapNotificationToComponent: PropTypes.func.isRequired,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
render () {
|
|
160
|
+
return (
|
|
161
|
+
<div>
|
|
162
|
+
{
|
|
163
|
+
this.props.notifications.map(
|
|
164
|
+
notification => {
|
|
165
|
+
const Component = this.props.mapNotificationToComponent(notification)
|
|
166
|
+
const dismiss = () => removeNotification(notification.id)
|
|
167
|
+
return (
|
|
168
|
+
<Component
|
|
169
|
+
key={notification.id}
|
|
170
|
+
notification={notification}
|
|
171
|
+
dismiss={dismiss}
|
|
172
|
+
/>
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
</div>
|
|
178
|
+
)
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
- The notification system is unaware of the types of notifications you are going
|
|
186
|
+
to dispatch. It's recommended to call the types of notifications you have
|
|
187
|
+
`kind`, to not confuse it with Action Types.
|
|
188
|
+
- The notification system is also unaware of the `domain`'s used in the
|
|
189
|
+
application. A domain is the part of the application where the notification
|
|
190
|
+
will be displayed. We currently have: `global`, `page` and `side`
|
|
191
|
+
- `addNotification` is a low-level function. It's recommended to use it in your
|
|
192
|
+
own action-creators, and use your own action-creators in your application
|
|
193
|
+
exclusively calling `addNotification` behind the scenes.
|
|
194
|
+
- to hide all notifications, iterate over the notifications in the state and
|
|
195
|
+
call `removeNotification(id)` on each one: `state.notifications.forEach(notif => removeNotification(notif.id))`. Simply resetting the state would lead to
|
|
196
|
+
the `onDismiss` callbacks not being fired.
|
|
197
|
+
- It's entirely up to you to render the notifications. Connect to the store and
|
|
198
|
+
iterate over them.
|
|
199
|
+
- If your notifications need to have different styles or even different React
|
|
200
|
+
components when rendering, use the `kind` to decide which component to render
|
|
201
|
+
or which style to use.
|
|
202
|
+
- Since notifications are stored in the Redux store, they should be
|
|
203
|
+
serializeable. Avoid setting functions on notifications. Again, use the
|
|
204
|
+
`kind`, pass any required information and handle it in the component rendering
|
|
205
|
+
the notification.
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tbWVyY2V0b29scy1mcm9udGVuZC1ub3RpZmljYXRpb25zLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL2luZGV4LmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
6
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
7
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
8
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
9
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
10
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
11
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
12
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
13
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
14
|
+
var _includesInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/includes');
|
|
15
|
+
var _Map = require('@babel/runtime-corejs3/core-js-stable/map');
|
|
16
|
+
var _setTimeout = require('@babel/runtime-corejs3/core-js-stable/set-timeout');
|
|
17
|
+
|
|
18
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
19
|
+
|
|
20
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
21
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
22
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
23
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
24
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
25
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
26
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
27
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
28
|
+
var _includesInstanceProperty__default = /*#__PURE__*/_interopDefault(_includesInstanceProperty);
|
|
29
|
+
var _Map__default = /*#__PURE__*/_interopDefault(_Map);
|
|
30
|
+
var _setTimeout__default = /*#__PURE__*/_interopDefault(_setTimeout);
|
|
31
|
+
|
|
32
|
+
// NOTE: This string will be replaced on build time with the package version.
|
|
33
|
+
var version = "24.7.2";
|
|
34
|
+
|
|
35
|
+
const ADD_NOTIFICATION = 'ADD_NOTIFICATION';
|
|
36
|
+
const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';
|
|
37
|
+
|
|
38
|
+
function addNotification(notification, meta) {
|
|
39
|
+
const action = {
|
|
40
|
+
type: ADD_NOTIFICATION,
|
|
41
|
+
payload: notification
|
|
42
|
+
};
|
|
43
|
+
if (meta) action.meta = meta;
|
|
44
|
+
return action;
|
|
45
|
+
}
|
|
46
|
+
function removeNotification(id) {
|
|
47
|
+
return {
|
|
48
|
+
type: REMOVE_NOTIFICATION,
|
|
49
|
+
payload: {
|
|
50
|
+
id
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
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; }
|
|
56
|
+
function _objectSpread(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(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(Object(t))).call(_context3, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
|
|
57
|
+
// Force TS cast of generic action to TNotificationAction
|
|
58
|
+
const isNotificationAction = action => {
|
|
59
|
+
var _context;
|
|
60
|
+
return _includesInstanceProperty__default["default"](_context = [ADD_NOTIFICATION, REMOVE_NOTIFICATION]).call(_context, action.type);
|
|
61
|
+
};
|
|
62
|
+
const dismissCallbacksMap = new _Map__default["default"]();
|
|
63
|
+
let id = 0;
|
|
64
|
+
const middleware = _ref => {
|
|
65
|
+
let dispatch = _ref.dispatch;
|
|
66
|
+
return next => action => {
|
|
67
|
+
if (!isNotificationAction(action)) {
|
|
68
|
+
return next(action);
|
|
69
|
+
}
|
|
70
|
+
switch (action.type) {
|
|
71
|
+
case ADD_NOTIFICATION:
|
|
72
|
+
{
|
|
73
|
+
id += 1;
|
|
74
|
+
const notification = _objectSpread(_objectSpread({}, action.payload), {}, {
|
|
75
|
+
id
|
|
76
|
+
});
|
|
77
|
+
const dismissCallback = () => {
|
|
78
|
+
dispatch(removeNotification(notification.id));
|
|
79
|
+
};
|
|
80
|
+
if (action.meta) {
|
|
81
|
+
if (action.meta.dismissAfter) _setTimeout__default["default"](dismissCallback, action.meta.dismissAfter);
|
|
82
|
+
if (typeof action.meta.onDismiss === 'function') dismissCallbacksMap.set(notification.id, action.meta.onDismiss);
|
|
83
|
+
}
|
|
84
|
+
const nextAction = _objectSpread(_objectSpread({}, action), {}, {
|
|
85
|
+
payload: notification,
|
|
86
|
+
dismiss: dismissCallback
|
|
87
|
+
});
|
|
88
|
+
return next(nextAction);
|
|
89
|
+
}
|
|
90
|
+
case REMOVE_NOTIFICATION:
|
|
91
|
+
{
|
|
92
|
+
const notificationId = action.payload.id;
|
|
93
|
+
const callback = dismissCallbacksMap.get(notificationId);
|
|
94
|
+
if (callback) callback(notificationId);
|
|
95
|
+
dismissCallbacksMap.delete(notificationId);
|
|
96
|
+
return next(action);
|
|
97
|
+
}
|
|
98
|
+
default:
|
|
99
|
+
return next(action);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
function notificationsReducer() {
|
|
105
|
+
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
106
|
+
let action = arguments.length > 1 ? arguments[1] : undefined;
|
|
107
|
+
if (!action || !action.type) return state;
|
|
108
|
+
switch (action.type) {
|
|
109
|
+
case ADD_NOTIFICATION:
|
|
110
|
+
{
|
|
111
|
+
return [action.payload, ...state];
|
|
112
|
+
}
|
|
113
|
+
case REMOVE_NOTIFICATION:
|
|
114
|
+
return _filterInstanceProperty__default["default"](state).call(state, notification => action.payload.id !== notification.id);
|
|
115
|
+
default:
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.ADD_NOTIFICATION = ADD_NOTIFICATION;
|
|
121
|
+
exports.REMOVE_NOTIFICATION = REMOVE_NOTIFICATION;
|
|
122
|
+
exports.addNotification = addNotification;
|
|
123
|
+
exports.middleware = middleware;
|
|
124
|
+
exports.reducer = notificationsReducer;
|
|
125
|
+
exports.removeNotification = removeNotification;
|
|
126
|
+
exports.version = version;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys');
|
|
6
|
+
var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols');
|
|
7
|
+
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
|
8
|
+
var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor');
|
|
9
|
+
var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each');
|
|
10
|
+
var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors');
|
|
11
|
+
var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties');
|
|
12
|
+
var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property');
|
|
13
|
+
var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
|
|
14
|
+
var _includesInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/includes');
|
|
15
|
+
var _Map = require('@babel/runtime-corejs3/core-js-stable/map');
|
|
16
|
+
var _setTimeout = require('@babel/runtime-corejs3/core-js-stable/set-timeout');
|
|
17
|
+
|
|
18
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
19
|
+
|
|
20
|
+
var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys);
|
|
21
|
+
var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols);
|
|
22
|
+
var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty);
|
|
23
|
+
var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor);
|
|
24
|
+
var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty);
|
|
25
|
+
var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors);
|
|
26
|
+
var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties);
|
|
27
|
+
var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty);
|
|
28
|
+
var _includesInstanceProperty__default = /*#__PURE__*/_interopDefault(_includesInstanceProperty);
|
|
29
|
+
var _Map__default = /*#__PURE__*/_interopDefault(_Map);
|
|
30
|
+
var _setTimeout__default = /*#__PURE__*/_interopDefault(_setTimeout);
|
|
31
|
+
|
|
32
|
+
// NOTE: This string will be replaced on build time with the package version.
|
|
33
|
+
var version = "24.7.2";
|
|
34
|
+
|
|
35
|
+
const ADD_NOTIFICATION = 'ADD_NOTIFICATION';
|
|
36
|
+
const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';
|
|
37
|
+
|
|
38
|
+
function addNotification(notification, meta) {
|
|
39
|
+
const action = {
|
|
40
|
+
type: ADD_NOTIFICATION,
|
|
41
|
+
payload: notification
|
|
42
|
+
};
|
|
43
|
+
if (meta) action.meta = meta;
|
|
44
|
+
return action;
|
|
45
|
+
}
|
|
46
|
+
function removeNotification(id) {
|
|
47
|
+
return {
|
|
48
|
+
type: REMOVE_NOTIFICATION,
|
|
49
|
+
payload: {
|
|
50
|
+
id
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
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; }
|
|
56
|
+
function _objectSpread(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(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(Object(t))).call(_context3, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; }
|
|
57
|
+
// Force TS cast of generic action to TNotificationAction
|
|
58
|
+
const isNotificationAction = action => {
|
|
59
|
+
var _context;
|
|
60
|
+
return _includesInstanceProperty__default["default"](_context = [ADD_NOTIFICATION, REMOVE_NOTIFICATION]).call(_context, action.type);
|
|
61
|
+
};
|
|
62
|
+
const dismissCallbacksMap = new _Map__default["default"]();
|
|
63
|
+
let id = 0;
|
|
64
|
+
const middleware = _ref => {
|
|
65
|
+
let dispatch = _ref.dispatch;
|
|
66
|
+
return next => action => {
|
|
67
|
+
if (!isNotificationAction(action)) {
|
|
68
|
+
return next(action);
|
|
69
|
+
}
|
|
70
|
+
switch (action.type) {
|
|
71
|
+
case ADD_NOTIFICATION:
|
|
72
|
+
{
|
|
73
|
+
id += 1;
|
|
74
|
+
const notification = _objectSpread(_objectSpread({}, action.payload), {}, {
|
|
75
|
+
id
|
|
76
|
+
});
|
|
77
|
+
const dismissCallback = () => {
|
|
78
|
+
dispatch(removeNotification(notification.id));
|
|
79
|
+
};
|
|
80
|
+
if (action.meta) {
|
|
81
|
+
if (action.meta.dismissAfter) _setTimeout__default["default"](dismissCallback, action.meta.dismissAfter);
|
|
82
|
+
if (typeof action.meta.onDismiss === 'function') dismissCallbacksMap.set(notification.id, action.meta.onDismiss);
|
|
83
|
+
}
|
|
84
|
+
const nextAction = _objectSpread(_objectSpread({}, action), {}, {
|
|
85
|
+
payload: notification,
|
|
86
|
+
dismiss: dismissCallback
|
|
87
|
+
});
|
|
88
|
+
return next(nextAction);
|
|
89
|
+
}
|
|
90
|
+
case REMOVE_NOTIFICATION:
|
|
91
|
+
{
|
|
92
|
+
const notificationId = action.payload.id;
|
|
93
|
+
const callback = dismissCallbacksMap.get(notificationId);
|
|
94
|
+
if (callback) callback(notificationId);
|
|
95
|
+
dismissCallbacksMap.delete(notificationId);
|
|
96
|
+
return next(action);
|
|
97
|
+
}
|
|
98
|
+
default:
|
|
99
|
+
return next(action);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
function notificationsReducer() {
|
|
105
|
+
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
106
|
+
let action = arguments.length > 1 ? arguments[1] : undefined;
|
|
107
|
+
if (!action || !action.type) return state;
|
|
108
|
+
switch (action.type) {
|
|
109
|
+
case ADD_NOTIFICATION:
|
|
110
|
+
{
|
|
111
|
+
return [action.payload, ...state];
|
|
112
|
+
}
|
|
113
|
+
case REMOVE_NOTIFICATION:
|
|
114
|
+
return _filterInstanceProperty__default["default"](state).call(state, notification => action.payload.id !== notification.id);
|
|
115
|
+
default:
|
|
116
|
+
return state;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.ADD_NOTIFICATION = ADD_NOTIFICATION;
|
|
121
|
+
exports.REMOVE_NOTIFICATION = REMOVE_NOTIFICATION;
|
|
122
|
+
exports.addNotification = addNotification;
|
|
123
|
+
exports.middleware = middleware;
|
|
124
|
+
exports.reducer = notificationsReducer;
|
|
125
|
+
exports.removeNotification = removeNotification;
|
|
126
|
+
exports.version = version;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys';
|
|
2
|
+
import _Object$getOwnPropertySymbols from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols';
|
|
3
|
+
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
|
|
4
|
+
import _Object$getOwnPropertyDescriptor from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor';
|
|
5
|
+
import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each';
|
|
6
|
+
import _Object$getOwnPropertyDescriptors from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors';
|
|
7
|
+
import _Object$defineProperties from '@babel/runtime-corejs3/core-js-stable/object/define-properties';
|
|
8
|
+
import _Object$defineProperty from '@babel/runtime-corejs3/core-js-stable/object/define-property';
|
|
9
|
+
import _defineProperty from '@babel/runtime-corejs3/helpers/esm/defineProperty';
|
|
10
|
+
import _includesInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/includes';
|
|
11
|
+
import _Map from '@babel/runtime-corejs3/core-js-stable/map';
|
|
12
|
+
import _setTimeout from '@babel/runtime-corejs3/core-js-stable/set-timeout';
|
|
13
|
+
|
|
14
|
+
// NOTE: This string will be replaced on build time with the package version.
|
|
15
|
+
var version = "24.7.2";
|
|
16
|
+
|
|
17
|
+
const ADD_NOTIFICATION = 'ADD_NOTIFICATION';
|
|
18
|
+
const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';
|
|
19
|
+
|
|
20
|
+
function addNotification(notification, meta) {
|
|
21
|
+
const action = {
|
|
22
|
+
type: ADD_NOTIFICATION,
|
|
23
|
+
payload: notification
|
|
24
|
+
};
|
|
25
|
+
if (meta) action.meta = meta;
|
|
26
|
+
return action;
|
|
27
|
+
}
|
|
28
|
+
function removeNotification(id) {
|
|
29
|
+
return {
|
|
30
|
+
type: REMOVE_NOTIFICATION,
|
|
31
|
+
payload: {
|
|
32
|
+
id
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = _filterInstanceProperty(o).call(o, function (r) { return _Object$getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
38
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context2, _context3; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty(_context2 = ownKeys(Object(t), !0)).call(_context2, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context3 = ownKeys(Object(t))).call(_context3, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
39
|
+
// Force TS cast of generic action to TNotificationAction
|
|
40
|
+
const isNotificationAction = action => {
|
|
41
|
+
var _context;
|
|
42
|
+
return _includesInstanceProperty(_context = [ADD_NOTIFICATION, REMOVE_NOTIFICATION]).call(_context, action.type);
|
|
43
|
+
};
|
|
44
|
+
const dismissCallbacksMap = new _Map();
|
|
45
|
+
let id = 0;
|
|
46
|
+
const middleware = _ref => {
|
|
47
|
+
let dispatch = _ref.dispatch;
|
|
48
|
+
return next => action => {
|
|
49
|
+
if (!isNotificationAction(action)) {
|
|
50
|
+
return next(action);
|
|
51
|
+
}
|
|
52
|
+
switch (action.type) {
|
|
53
|
+
case ADD_NOTIFICATION:
|
|
54
|
+
{
|
|
55
|
+
id += 1;
|
|
56
|
+
const notification = _objectSpread(_objectSpread({}, action.payload), {}, {
|
|
57
|
+
id
|
|
58
|
+
});
|
|
59
|
+
const dismissCallback = () => {
|
|
60
|
+
dispatch(removeNotification(notification.id));
|
|
61
|
+
};
|
|
62
|
+
if (action.meta) {
|
|
63
|
+
if (action.meta.dismissAfter) _setTimeout(dismissCallback, action.meta.dismissAfter);
|
|
64
|
+
if (typeof action.meta.onDismiss === 'function') dismissCallbacksMap.set(notification.id, action.meta.onDismiss);
|
|
65
|
+
}
|
|
66
|
+
const nextAction = _objectSpread(_objectSpread({}, action), {}, {
|
|
67
|
+
payload: notification,
|
|
68
|
+
dismiss: dismissCallback
|
|
69
|
+
});
|
|
70
|
+
return next(nextAction);
|
|
71
|
+
}
|
|
72
|
+
case REMOVE_NOTIFICATION:
|
|
73
|
+
{
|
|
74
|
+
const notificationId = action.payload.id;
|
|
75
|
+
const callback = dismissCallbacksMap.get(notificationId);
|
|
76
|
+
if (callback) callback(notificationId);
|
|
77
|
+
dismissCallbacksMap.delete(notificationId);
|
|
78
|
+
return next(action);
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
return next(action);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function notificationsReducer() {
|
|
87
|
+
let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
88
|
+
let action = arguments.length > 1 ? arguments[1] : undefined;
|
|
89
|
+
if (!action || !action.type) return state;
|
|
90
|
+
switch (action.type) {
|
|
91
|
+
case ADD_NOTIFICATION:
|
|
92
|
+
{
|
|
93
|
+
return [action.payload, ...state];
|
|
94
|
+
}
|
|
95
|
+
case REMOVE_NOTIFICATION:
|
|
96
|
+
return _filterInstanceProperty(state).call(state, notification => action.payload.id !== notification.id);
|
|
97
|
+
default:
|
|
98
|
+
return state;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { ADD_NOTIFICATION, REMOVE_NOTIFICATION, addNotification, middleware, notificationsReducer as reducer, removeNotification, version };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { TNotificationMetaOptions, TRemoveNotificationAction, TNotification, TAddNotificationAction } from "./types.js";
|
|
2
|
+
export declare function addNotification<Payload extends TNotification>(notification: Payload, meta?: TNotificationMetaOptions): TAddNotificationAction<Payload>;
|
|
3
|
+
export declare function removeNotification(id: TNotification['id']): TRemoveNotificationAction;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as version } from "./version.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export { default as middleware } from "./middleware.js";
|
|
4
|
+
export { default as reducer } from "./reducer.js";
|
|
5
|
+
export { addNotification, removeNotification } from "./action-creators.js";
|
|
6
|
+
export { ADD_NOTIFICATION, REMOVE_NOTIFICATION } from "./action-types.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Dispatch, MiddlewareAPI } from 'redux';
|
|
2
|
+
import type { TNotification, TNotificationAction } from "./types.js";
|
|
3
|
+
declare const middleware: <Payload extends TNotification>({ dispatch }: MiddlewareAPI) => (next: Dispatch<TNotificationAction<Payload>>) => (action: TNotificationAction<Payload>) => import("./types.js").TRemoveNotificationAction | {
|
|
4
|
+
payload: Payload & {
|
|
5
|
+
id: number;
|
|
6
|
+
};
|
|
7
|
+
dismiss: () => void;
|
|
8
|
+
meta?: import("./types.js").TNotificationMetaOptions;
|
|
9
|
+
type: "ADD_NOTIFICATION";
|
|
10
|
+
};
|
|
11
|
+
export default middleware;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { TNotification, TNotificationAction, TNotificationState } from "./types.js";
|
|
2
|
+
export default function notificationsReducer<Payload extends TNotification>(state: TNotificationState<Payload> | undefined, action: TNotificationAction<Payload>): TNotificationState<Payload>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Action } from 'redux';
|
|
2
|
+
import { ADD_NOTIFICATION, REMOVE_NOTIFICATION } from "./action-types.js";
|
|
3
|
+
export type TNotification = {
|
|
4
|
+
id?: number;
|
|
5
|
+
};
|
|
6
|
+
export type TNotificationOnDismiss = (id: TNotification['id']) => void;
|
|
7
|
+
export type TNotificationMetaOptions = {
|
|
8
|
+
dismissAfter?: number;
|
|
9
|
+
onDismiss?: TNotificationOnDismiss;
|
|
10
|
+
};
|
|
11
|
+
export interface TAddNotificationAction<Payload extends TNotification> extends Action<typeof ADD_NOTIFICATION> {
|
|
12
|
+
payload: Payload;
|
|
13
|
+
meta?: TNotificationMetaOptions;
|
|
14
|
+
dismiss?: () => void;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
export interface TRemoveNotificationAction extends Action<typeof REMOVE_NOTIFICATION> {
|
|
18
|
+
payload: TNotification;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
}
|
|
21
|
+
export type TNotificationAction<Payload extends TNotification> = TAddNotificationAction<Payload> | TRemoveNotificationAction;
|
|
22
|
+
export type TNotificationState<Payload extends TNotification> = Payload[];
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commercetools-frontend/notifications",
|
|
3
|
+
"version": "0.0.0-CRAFT-1791-20251006162610",
|
|
4
|
+
"description": "A general-purpose notification system built on top of redux",
|
|
5
|
+
"bugs": "https://github.com/commercetools/merchant-center-application-kit/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/commercetools/merchant-center-application-kit.git",
|
|
9
|
+
"directory": "packages/notifications"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://docs.commercetools.com/merchant-center-customizations",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"javascript",
|
|
14
|
+
"frontend",
|
|
15
|
+
"react",
|
|
16
|
+
"toolkit"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"main": "dist/commercetools-frontend-notifications.cjs.js",
|
|
23
|
+
"module": "dist/commercetools-frontend-notifications.esm.js",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"package.json",
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@babel/runtime": "^7.22.15",
|
|
32
|
+
"@babel/runtime-corejs3": "^7.22.15"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"redux": "5.0.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"redux": "4.x || 5.x"
|
|
39
|
+
}
|
|
40
|
+
}
|