@shopgate/pwa-common 7.31.5-beta.4 → 7.31.5-beta.6
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 +3 -3
- package/providers/toast/index.js +46 -39
- package/providers/toast/spec.js +105 -0
- package/subscriptions/error.js +50 -27
- package/subscriptions/helpers/pipeline.js +68 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shopgate/pwa-common",
|
|
3
|
-
"version": "7.31.5-beta.
|
|
3
|
+
"version": "7.31.5-beta.6",
|
|
4
4
|
"description": "Common library for the Shopgate Connect PWA.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Shopgate <support@shopgate.com>",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@redux-devtools/extension": "^3.3.0",
|
|
19
19
|
"@sentry/browser": "6.0.1",
|
|
20
|
-
"@shopgate/pwa-benchmark": "7.31.5-beta.
|
|
20
|
+
"@shopgate/pwa-benchmark": "7.31.5-beta.6",
|
|
21
21
|
"@virtuous/conductor": "~2.5.0",
|
|
22
22
|
"@virtuous/react-conductor": "~2.5.0",
|
|
23
23
|
"@virtuous/redux-persister": "1.1.0-beta.7",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"swiper": "12.1.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@shopgate/pwa-core": "7.31.5-beta.
|
|
43
|
+
"@shopgate/pwa-core": "7.31.5-beta.6",
|
|
44
44
|
"@types/lodash": "^4.17.24",
|
|
45
45
|
"@types/react-portal": "^3.0.9",
|
|
46
46
|
"lodash": "^4.17.23",
|
package/providers/toast/index.js
CHANGED
|
@@ -32,47 +32,41 @@ let ToastProvider = /*#__PURE__*/function (_Component) {
|
|
|
32
32
|
if (!toast.message) {
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
|
-
const {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
const nextToast = {
|
|
36
|
+
id: toast.id,
|
|
37
|
+
action: toast.action,
|
|
38
|
+
actionLabel: toast.actionLabel,
|
|
39
|
+
onLongPress: toast.onLongPress,
|
|
40
|
+
message: toast.message,
|
|
41
|
+
messageParams: toast.messageParams,
|
|
42
|
+
duration: toast.duration || duration
|
|
43
|
+
};
|
|
43
44
|
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
found.message = toast.message;
|
|
49
|
-
found.messageParams = toast.messageParams;
|
|
50
|
-
found.duration = toast.duration || duration;
|
|
51
|
-
} else {
|
|
52
|
-
toasts.push({
|
|
53
|
-
id: toast.id,
|
|
54
|
-
action: toast.action,
|
|
55
|
-
actionLabel: toast.actionLabel,
|
|
56
|
-
message: toast.message,
|
|
57
|
-
messageParams: toast.messageParams,
|
|
58
|
-
duration: toast.duration || duration
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
_this.setState({
|
|
45
|
+
// Update the queue immutably: consumers rely on the array reference changing to re-render
|
|
46
|
+
// (e.g. the SnackBar memoizes the currently shown toast on this reference). A toast whose id
|
|
47
|
+
// is already queued replaces that entry with the new data; otherwise it is appended.
|
|
48
|
+
_this.setState(({
|
|
62
49
|
toasts
|
|
50
|
+
}) => {
|
|
51
|
+
const exists = toasts.some(({
|
|
52
|
+
id
|
|
53
|
+
}) => id === toast.id);
|
|
54
|
+
return {
|
|
55
|
+
toasts: exists ? toasts.map(item => item.id === toast.id ? nextToast : item) : [].concat(toasts, [nextToast])
|
|
56
|
+
};
|
|
63
57
|
});
|
|
64
58
|
};
|
|
65
59
|
/**
|
|
66
60
|
* Removes the first toast from the list.
|
|
67
61
|
*/
|
|
68
62
|
_this.removeToast = () => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
toasts.shift();
|
|
73
|
-
_this.setState({
|
|
63
|
+
// Drop the first toast immutably so the array reference changes and the SnackBar advances to
|
|
64
|
+
// the next queued toast (rather than re-rendering the same, now-stale, memoized toast).
|
|
65
|
+
_this.setState(({
|
|
74
66
|
toasts
|
|
75
|
-
})
|
|
67
|
+
}) => ({
|
|
68
|
+
toasts: toasts.slice(1)
|
|
69
|
+
}));
|
|
76
70
|
};
|
|
77
71
|
_this.flushToasts = () => {
|
|
78
72
|
if (_this.state.toasts.length) {
|
|
@@ -90,11 +84,21 @@ let ToastProvider = /*#__PURE__*/function (_Component) {
|
|
|
90
84
|
}
|
|
91
85
|
|
|
92
86
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @returns {Object}
|
|
87
|
+
* Removes the UIEvents listeners registered in the constructor.
|
|
95
88
|
*/
|
|
96
89
|
_inheritsLoose(ToastProvider, _Component);
|
|
97
90
|
var _proto = ToastProvider.prototype;
|
|
91
|
+
_proto.componentWillUnmount = function componentWillUnmount() {
|
|
92
|
+
UIEvents.removeListener(this.constructor.ADD, this.addToast);
|
|
93
|
+
UIEvents.removeListener(this.constructor.FLUSH, this.flushToasts);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Returns the context value to be passed to consumers. The object reference is cached and only
|
|
98
|
+
* rebuilt when `toasts` changes, so consumers (Toaster → SnackBarContainer → SnackBar) don't
|
|
99
|
+
* re-render on every unrelated ToastProvider render.
|
|
100
|
+
* @returns {Object}
|
|
101
|
+
*/;
|
|
98
102
|
/**
|
|
99
103
|
* @returns {JSX}
|
|
100
104
|
*/
|
|
@@ -107,11 +111,14 @@ let ToastProvider = /*#__PURE__*/function (_Component) {
|
|
|
107
111
|
return _createClass(ToastProvider, [{
|
|
108
112
|
key: "provided",
|
|
109
113
|
get: function () {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
114
|
+
if (!this.providedCache || this.providedCache.toasts !== this.state.toasts) {
|
|
115
|
+
this.providedCache = {
|
|
116
|
+
addToast: this.addToast,
|
|
117
|
+
removeToast: this.removeToast,
|
|
118
|
+
toasts: this.state.toasts
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return this.providedCache;
|
|
115
122
|
}
|
|
116
123
|
}]);
|
|
117
124
|
}(Component);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
import { render, act } from '@testing-library/react';
|
|
3
|
+
import ToastProvider from "./index";
|
|
4
|
+
import ToastContext from "./context";
|
|
5
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
+
jest.mock('@shopgate/pwa-core', () => ({
|
|
7
|
+
UIEvents: {
|
|
8
|
+
addListener: jest.fn(),
|
|
9
|
+
removeListener: jest.fn(),
|
|
10
|
+
emit: jest.fn()
|
|
11
|
+
}
|
|
12
|
+
}));
|
|
13
|
+
jest.mock('@shopgate/pwa-common/helpers/config', () => ({
|
|
14
|
+
themeConfig: {
|
|
15
|
+
variables: {
|
|
16
|
+
toast: {
|
|
17
|
+
duration: 5000
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}));
|
|
22
|
+
let ctx;
|
|
23
|
+
const Capture = () => {
|
|
24
|
+
ctx = useContext(ToastContext);
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
27
|
+
const renderProvider = () => render(/*#__PURE__*/_jsx(ToastProvider, {
|
|
28
|
+
children: /*#__PURE__*/_jsx(Capture, {})
|
|
29
|
+
}));
|
|
30
|
+
const toast = (id, message) => ({
|
|
31
|
+
id,
|
|
32
|
+
message
|
|
33
|
+
});
|
|
34
|
+
describe('ToastProvider', () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
ctx = undefined;
|
|
37
|
+
});
|
|
38
|
+
it('appends a toast and applies the default duration', () => {
|
|
39
|
+
renderProvider();
|
|
40
|
+
act(() => {
|
|
41
|
+
ctx.addToast(toast('a', 'A'));
|
|
42
|
+
});
|
|
43
|
+
expect(ctx.toasts).toHaveLength(1);
|
|
44
|
+
expect(ctx.toasts[0]).toMatchObject({
|
|
45
|
+
id: 'a',
|
|
46
|
+
message: 'A',
|
|
47
|
+
duration: 5000
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
it('ignores a toast without a message', () => {
|
|
51
|
+
renderProvider();
|
|
52
|
+
act(() => {
|
|
53
|
+
ctx.addToast({
|
|
54
|
+
id: 'x'
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
expect(ctx.toasts).toHaveLength(0);
|
|
58
|
+
});
|
|
59
|
+
it('replaces a same-id toast in place, keeping its length and position', () => {
|
|
60
|
+
renderProvider();
|
|
61
|
+
act(() => {
|
|
62
|
+
ctx.addToast(toast('a', 'A'));
|
|
63
|
+
});
|
|
64
|
+
act(() => {
|
|
65
|
+
ctx.addToast(toast('b', 'B'));
|
|
66
|
+
});
|
|
67
|
+
const before = ctx.toasts;
|
|
68
|
+
act(() => {
|
|
69
|
+
ctx.addToast(toast('a', 'A2'));
|
|
70
|
+
});
|
|
71
|
+
expect(ctx.toasts).toHaveLength(2);
|
|
72
|
+
expect(ctx.toasts[0]).toMatchObject(toast('a', 'A2')); // content updated
|
|
73
|
+
expect(ctx.toasts[1]).toMatchObject(toast('b', 'B')); // sibling untouched
|
|
74
|
+
expect(ctx.toasts).not.toBe(before); // new array reference (immutable update)
|
|
75
|
+
expect(ctx.toasts[0]).not.toBe(before[0]); // new head object
|
|
76
|
+
});
|
|
77
|
+
it('removes the first toast (FIFO) on removeToast', () => {
|
|
78
|
+
renderProvider();
|
|
79
|
+
act(() => {
|
|
80
|
+
ctx.addToast(toast('a', 'A'));
|
|
81
|
+
});
|
|
82
|
+
act(() => {
|
|
83
|
+
ctx.addToast(toast('b', 'B'));
|
|
84
|
+
});
|
|
85
|
+
act(() => {
|
|
86
|
+
ctx.removeToast();
|
|
87
|
+
});
|
|
88
|
+
expect(ctx.toasts).toHaveLength(1);
|
|
89
|
+
expect(ctx.toasts[0]).toMatchObject(toast('b', 'B'));
|
|
90
|
+
});
|
|
91
|
+
it('keeps the context value referentially stable when toasts do not change', () => {
|
|
92
|
+
const {
|
|
93
|
+
rerender
|
|
94
|
+
} = renderProvider();
|
|
95
|
+
act(() => {
|
|
96
|
+
ctx.addToast(toast('a', 'A'));
|
|
97
|
+
});
|
|
98
|
+
const provided = ctx;
|
|
99
|
+
// Re-render the provider without changing the queue.
|
|
100
|
+
rerender(/*#__PURE__*/_jsx(ToastProvider, {
|
|
101
|
+
children: /*#__PURE__*/_jsx(Capture, {})
|
|
102
|
+
}));
|
|
103
|
+
expect(ctx).toBe(provided); // same context object → consumers don't needlessly re-render
|
|
104
|
+
});
|
|
105
|
+
});
|
package/subscriptions/error.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import "core-js/modules/es.array.includes.js";
|
|
2
|
-
import after from 'lodash/after';
|
|
3
|
-
import before from 'lodash/before';
|
|
4
|
-
import over from 'lodash/over';
|
|
5
2
|
import { isAvailable } from '@shopgate/native-modules';
|
|
6
3
|
import { init, addBreadcrumb, configureScope, captureException, captureMessage, captureEvent, withScope, Severity as SentrySeverity } from '@sentry/browser';
|
|
7
4
|
import { EBIGAPI, emitter, errorManager, ETIMEOUT, ENETUNREACH, EUNKNOWN, EFAVORITE } from '@shopgate/pwa-core';
|
|
@@ -12,7 +9,7 @@ import {
|
|
|
12
9
|
// eslint-disable-next-line import/no-named-default
|
|
13
10
|
default as appConfig, themeName, pckVersion } from "../helpers/config";
|
|
14
11
|
import { env } from "../helpers/environment";
|
|
15
|
-
import { transformGeneralPipelineError } from "./helpers/pipeline";
|
|
12
|
+
import { transformGeneralPipelineError, getDisplayErrorMessage } from "./helpers/pipeline";
|
|
16
13
|
import { historyPop } from "../actions/router";
|
|
17
14
|
import showModal from "../actions/modal/showModal";
|
|
18
15
|
import { getUserData } from "../selectors/user";
|
|
@@ -24,6 +21,9 @@ import { getRouterStack } from "../selectors/router";
|
|
|
24
21
|
import { MODAL_PIPELINE_ERROR } from "../constants/ModalTypes";
|
|
25
22
|
import ToastProvider from "../providers/toast";
|
|
26
23
|
|
|
24
|
+
// Generic, translated fallback shown when a backend error carries no code we can map to a message.
|
|
25
|
+
const GENERIC_ERROR_MESSAGE = 'modal.body_error';
|
|
26
|
+
|
|
27
27
|
/**
|
|
28
28
|
* App errors subscriptions.
|
|
29
29
|
* @param {Function} subscribe The subscribe function.
|
|
@@ -60,15 +60,35 @@ export default subscribe => {
|
|
|
60
60
|
error
|
|
61
61
|
} = action;
|
|
62
62
|
const {
|
|
63
|
-
message,
|
|
64
63
|
code,
|
|
65
64
|
context,
|
|
66
65
|
meta = {}
|
|
67
66
|
} = error;
|
|
68
67
|
const {
|
|
69
|
-
behavior
|
|
68
|
+
behavior,
|
|
69
|
+
message: originalMessage
|
|
70
70
|
} = meta;
|
|
71
|
-
|
|
71
|
+
|
|
72
|
+
// Never surface a raw backend message to the user: show the extension's own translated
|
|
73
|
+
// message, a code-mapped translated message, or a generic fallback. The resolver is the single
|
|
74
|
+
// source of truth for both the text and whether it is ready-to-display (`displayTranslated`) or
|
|
75
|
+
// a locale key that must still go through I18n.Text.
|
|
76
|
+
const {
|
|
77
|
+
message: displayMessage,
|
|
78
|
+
translated: displayTranslated
|
|
79
|
+
} = getDisplayErrorMessage(error, GENERIC_ERROR_MESSAGE);
|
|
80
|
+
|
|
81
|
+
// Unknown/generic and connection-style backend errors are treated as general platform issues.
|
|
82
|
+
// EUNKNOWN is always treated as general — even when the backend supplied a translated message —
|
|
83
|
+
// so it is shown as a toast with its translated message or generic fallback, never a modal.
|
|
84
|
+
const isConnectionError = displayMessage === 'error.general' || [ETIMEOUT, ENETUNREACH].includes(code);
|
|
85
|
+
const isGeneralError = isConnectionError || displayMessage === GENERIC_ERROR_MESSAGE || code === EUNKNOWN;
|
|
86
|
+
|
|
87
|
+
// Some pipeline actions (e.g. fetchCategory) register an error behavior for *every* error via
|
|
88
|
+
// `setResponseBehavior`. Those behaviors are only meaningful for the specific errors they
|
|
89
|
+
// expect; when the platform fails with an unexpected/unmappable message we skip the behavior
|
|
90
|
+
// and fall through to the generic toast handling instead of, for example, showing a modal.
|
|
91
|
+
if (behavior && !isGeneralError) {
|
|
72
92
|
behavior({
|
|
73
93
|
dispatch,
|
|
74
94
|
getState,
|
|
@@ -78,38 +98,41 @@ export default subscribe => {
|
|
|
78
98
|
return;
|
|
79
99
|
}
|
|
80
100
|
|
|
81
|
-
/**
|
|
82
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Shows the pipeline error modal. When openWithDetails is set, the modal opens directly on the
|
|
103
|
+
* developer detail view (pipeline, code, raw message, params) — used for the toast long-press.
|
|
104
|
+
* @param {boolean} [openWithDetails=false] Whether to open the modal in developer detail mode.
|
|
105
|
+
*/
|
|
106
|
+
const showModalError = (openWithDetails = false) => {
|
|
83
107
|
dispatch(showModal({
|
|
84
108
|
confirm: 'modal.ok',
|
|
85
109
|
dismiss: null,
|
|
86
110
|
title: null,
|
|
87
|
-
message,
|
|
111
|
+
message: displayMessage,
|
|
88
112
|
type: MODAL_PIPELINE_ERROR,
|
|
89
113
|
params: {
|
|
90
114
|
pipeline: context,
|
|
91
115
|
request: meta.input,
|
|
92
|
-
message:
|
|
93
|
-
code
|
|
116
|
+
message: originalMessage,
|
|
117
|
+
code,
|
|
118
|
+
translated: displayTranslated,
|
|
119
|
+
messageParams: meta.additionalParams,
|
|
120
|
+
openWithDetails
|
|
94
121
|
}
|
|
95
122
|
}));
|
|
96
123
|
};
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
message: 'error.general',
|
|
109
|
-
action: over([showToast, showToastAfter])
|
|
110
|
-
});
|
|
124
|
+
|
|
125
|
+
// General platform errors surface as a toast instead of a modal.
|
|
126
|
+
// Long-pressing the toast opens the error modal in developer detail mode.
|
|
127
|
+
if (isGeneralError) {
|
|
128
|
+
// Connection-style errors show the generic connection message, but never override a message
|
|
129
|
+
// an extension already translated for us.
|
|
130
|
+
const useGenericConnectionText = isConnectionError && !displayTranslated;
|
|
131
|
+
events.emit(ToastProvider.ADD, {
|
|
132
|
+
id: 'pipeline.error',
|
|
133
|
+
message: useGenericConnectionText ? 'error.general' : displayMessage,
|
|
134
|
+
onLongPress: () => showModalError(true)
|
|
111
135
|
});
|
|
112
|
-
showToast();
|
|
113
136
|
return;
|
|
114
137
|
}
|
|
115
138
|
showModalError();
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { i18n } from '@shopgate/engage/core/helpers';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @param {Object} error error
|
|
3
5
|
* @returns {string|*}
|
|
@@ -12,4 +14,70 @@ export function transformGeneralPipelineError(error) {
|
|
|
12
14
|
return 'error.general';
|
|
13
15
|
}
|
|
14
16
|
return message;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {Object} DisplayErrorMessage
|
|
21
|
+
* @property {string} message The text to display — an already-translated string or a locale key.
|
|
22
|
+
* @property {boolean} translated Whether `message` is ready-to-display text (`true`) or a locale
|
|
23
|
+
* key that still needs to go through I18n.Text (`false`).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolves the message to display for a pipeline error, without ever surfacing raw backend text.
|
|
28
|
+
* Also reports whether the result is ready-to-display text or a locale key
|
|
29
|
+
* Precedence:
|
|
30
|
+
* 1. An extension's own message when it is explicitly flagged as already translated.
|
|
31
|
+
* 2. A resolvable i18n key — safe to surface even when errorManager did not remap it, because a
|
|
32
|
+
* translation key (e.g. `cart.error_out_of_stock`) is not raw backend text. `i18n.has` confirms
|
|
33
|
+
* the key actually resolves in the loaded locales, rather than merely looking like a key.
|
|
34
|
+
* 3. The code-mapped message. When errorManager maps the error code it resolves a message that
|
|
35
|
+
* differs from the raw backend text (`error.meta.message`).
|
|
36
|
+
* 4. A generic, translated fallback otherwise — including when nothing mapped and errorManager fell
|
|
37
|
+
* back to the raw backend message (`error.message === error.meta.message`).
|
|
38
|
+
* @param {Object} error The queued pipeline error.
|
|
39
|
+
* @param {string} error.message The message resolved by errorManager (code-mapped or raw fallback).
|
|
40
|
+
* @param {Object} [error.meta] The error meta data.
|
|
41
|
+
* @param {boolean} [error.meta.translated] Whether the backend message is already translated.
|
|
42
|
+
* @param {string} [error.meta.message] The original, raw backend message.
|
|
43
|
+
* @param {string} genericMessage The generic fallback message key.
|
|
44
|
+
* @returns {DisplayErrorMessage}
|
|
45
|
+
*/
|
|
46
|
+
export function getDisplayErrorMessage(error, genericMessage) {
|
|
47
|
+
const {
|
|
48
|
+
message,
|
|
49
|
+
meta = {}
|
|
50
|
+
} = error;
|
|
51
|
+
const {
|
|
52
|
+
translated,
|
|
53
|
+
message: originalMessage
|
|
54
|
+
} = meta;
|
|
55
|
+
if (translated && originalMessage) {
|
|
56
|
+
return {
|
|
57
|
+
message: originalMessage,
|
|
58
|
+
translated: true
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// A translation key is never raw backend text, so surface it even when errorManager left it
|
|
63
|
+
// untouched (`message === originalMessage`).
|
|
64
|
+
if (typeof message === 'string' && i18n.has(message)) {
|
|
65
|
+
return {
|
|
66
|
+
message,
|
|
67
|
+
translated: false
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Only trust `message` when we have the raw backend message to compare against and a mapping
|
|
72
|
+
// actually replaced it. Without the raw message we cannot rule out that `message` is raw text.
|
|
73
|
+
if (originalMessage !== undefined && message !== originalMessage) {
|
|
74
|
+
return {
|
|
75
|
+
message,
|
|
76
|
+
translated: false
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
message: genericMessage,
|
|
81
|
+
translated: false
|
|
82
|
+
};
|
|
15
83
|
}
|