@shopgate/pwa-common 7.32.2-alpha.2 → 7.32.2-alpha.3
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/package.json +2 -3
- package/store/middelwares/logger.js +113 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopgate/pwa-common",
|
|
3
|
-
"version": "7.32.2-alpha.
|
|
3
|
+
"version": "7.32.2-alpha.3",
|
|
4
4
|
"description": "Common library for the Shopgate Connect PWA.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Shopgate <support@shopgate.com>",
|
|
@@ -37,14 +37,13 @@
|
|
|
37
37
|
"react-portal": "^3.1.0",
|
|
38
38
|
"react-redux": "^8.1.3",
|
|
39
39
|
"redux": "^5.0.1",
|
|
40
|
-
"redux-logger": "^3.0.6",
|
|
41
40
|
"redux-thunk": "^3.1.0",
|
|
42
41
|
"reselect": "^4.1.8",
|
|
43
42
|
"rxjs": "~5.5.12",
|
|
44
43
|
"swiper": "12.2.0"
|
|
45
44
|
},
|
|
46
45
|
"devDependencies": {
|
|
47
|
-
"@shopgate/pwa-core": "7.32.2-alpha.
|
|
46
|
+
"@shopgate/pwa-core": "7.32.2-alpha.3",
|
|
48
47
|
"@types/lodash": "^4.17.25",
|
|
49
48
|
"@types/react-portal": "^3.0.9",
|
|
50
49
|
"lodash": "^4.18.1",
|
|
@@ -1,7 +1,114 @@
|
|
|
1
|
-
import { createLogger } from 'redux-logger';
|
|
2
1
|
import { logger } from '@shopgate/pwa-core';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
const STORAGE_KEY = 'sgReduxLogger';
|
|
3
|
+
const MUTED = 'color: gray; font-weight: lighter;';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reads whether the logger was switched off with sgReduxLogger.off().
|
|
7
|
+
* @return {boolean}
|
|
8
|
+
*/
|
|
9
|
+
const isSwitchedOff = () => {
|
|
10
|
+
try {
|
|
11
|
+
return window.localStorage.getItem(STORAGE_KEY) === 'off';
|
|
12
|
+
} catch (e) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
let enabled = !isSwitchedOff();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Remembers the switch for the next app start.
|
|
20
|
+
* @param {boolean} value Whether actions are logged.
|
|
21
|
+
* @return {boolean} Whether the switch could be saved.
|
|
22
|
+
*/
|
|
23
|
+
const saveSwitch = value => {
|
|
24
|
+
try {
|
|
25
|
+
if (value) {
|
|
26
|
+
window.localStorage.removeItem(STORAGE_KEY);
|
|
27
|
+
} else {
|
|
28
|
+
window.localStorage.setItem(STORAGE_KEY, 'off');
|
|
29
|
+
}
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Switches the logger on or off.
|
|
38
|
+
* @param {boolean} value Whether actions are logged.
|
|
39
|
+
*/
|
|
40
|
+
const setEnabled = value => {
|
|
41
|
+
enabled = value;
|
|
42
|
+
const saved = saveSwitch(value);
|
|
43
|
+
logger.info(`Redux logger switched ${value ? 'on' : 'off'}${saved ? '' : ' until the next reload'}`);
|
|
44
|
+
};
|
|
45
|
+
window.sgReduxLogger = {
|
|
46
|
+
on: () => setEnabled(true),
|
|
47
|
+
off: () => setEnabled(false)
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Formats the time of a date, e.g. 14:03:07.042.
|
|
52
|
+
* @param {Date} date The date.
|
|
53
|
+
* @return {string}
|
|
54
|
+
*/
|
|
55
|
+
const formatTime = date => [date.getHours(), date.getMinutes(), date.getSeconds()].map(value => String(value).padStart(2, '0')).join(':').concat(`.${String(date.getMilliseconds()).padStart(3, '0')}`);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Prints an action as a collapsed console group.
|
|
59
|
+
* @param {Object} entry The logged action with the states before and after it.
|
|
60
|
+
*/
|
|
61
|
+
const printEntry = ({
|
|
62
|
+
action,
|
|
63
|
+
prevState,
|
|
64
|
+
nextState,
|
|
65
|
+
error,
|
|
66
|
+
startedAt,
|
|
67
|
+
took
|
|
68
|
+
}) => {
|
|
69
|
+
logger.groupCollapsed(`%c action %c${String(action.type)} %c@ ${formatTime(startedAt)} %c(in ${took.toFixed(2)} ms)`, MUTED, 'color: inherit;', MUTED, MUTED);
|
|
70
|
+
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', prevState);
|
|
71
|
+
logger.log('%c action ', 'color: #03A9F4; font-weight: bold', action);
|
|
72
|
+
if (error) {
|
|
73
|
+
logger.log('%c error ', 'color: #F20404; font-weight: bold;', error);
|
|
74
|
+
}
|
|
75
|
+
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', nextState);
|
|
76
|
+
logger.groupEnd();
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Logs every action with the state before and after it to the console, unless the logger was
|
|
81
|
+
* switched off with sgReduxLogger.off() in the console.
|
|
82
|
+
* @param {Object} store The redux store.
|
|
83
|
+
* @return {Function}
|
|
84
|
+
*/
|
|
85
|
+
const loggerMiddleware = ({
|
|
86
|
+
getState
|
|
87
|
+
}) => next => action => {
|
|
88
|
+
if (!enabled) {
|
|
89
|
+
return next(action);
|
|
90
|
+
}
|
|
91
|
+
const startedAt = new Date();
|
|
92
|
+
const started = performance.now();
|
|
93
|
+
const prevState = getState();
|
|
94
|
+
let result;
|
|
95
|
+
let error;
|
|
96
|
+
try {
|
|
97
|
+
result = next(action);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
error = e;
|
|
100
|
+
}
|
|
101
|
+
printEntry({
|
|
102
|
+
action,
|
|
103
|
+
prevState,
|
|
104
|
+
nextState: getState(),
|
|
105
|
+
error,
|
|
106
|
+
startedAt,
|
|
107
|
+
took: performance.now() - started
|
|
108
|
+
});
|
|
109
|
+
if (error) {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
export default loggerMiddleware;
|