@pelcro/react-pelcro-js 4.0.0-alpha.13 → 4.0.0-alpha.15

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.
@@ -0,0 +1,254 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var components = require('./components-f9c58ce2.js');
5
+ require('react-dom');
6
+ require('prop-types');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
11
+
12
+ const initialState = {
13
+ cancelationReason: ""
14
+ };
15
+ const store = /*#__PURE__*/React.createContext(initialState);
16
+ const {
17
+ Provider
18
+ } = store;
19
+
20
+ const SubscriptionCancelContainer = ({
21
+ style,
22
+ className = "",
23
+ onSuccess = () => {},
24
+ onFailure = () => {},
25
+ children,
26
+ ...props
27
+ }) => {
28
+ components.useTranslation("verifyEmail");
29
+ const [state, dispatch] = components.useReducerWithSideEffects((state, action) => {
30
+ switch (action.type) {
31
+ case components.SET_CANCEL_SUBSCRIPTION_REASON:
32
+ return components.lib_7({ ...state,
33
+ cancelationReason: action.payload
34
+ });
35
+
36
+ default:
37
+ return state;
38
+ }
39
+ }, initialState);
40
+ return /*#__PURE__*/React__default['default'].createElement("div", {
41
+ style: { ...style
42
+ },
43
+ className: `pelcro-container pelcro-subscription-cancel-container ${className}`
44
+ }, /*#__PURE__*/React__default['default'].createElement(Provider, {
45
+ value: {
46
+ state,
47
+ dispatch
48
+ }
49
+ }, children.length ? children.map((child, i) => /*#__PURE__*/React__default['default'].cloneElement(child, {
50
+ store,
51
+ key: i
52
+ })) : /*#__PURE__*/React__default['default'].cloneElement(children, {
53
+ store
54
+ })));
55
+ };
56
+
57
+ const SubscriptionCancelReason = props => {
58
+ const {
59
+ t
60
+ } = components.useTranslation("subscriptionCancel");
61
+ const {
62
+ dispatch,
63
+ state
64
+ } = React.useContext(store);
65
+
66
+ const handleOnTextAreaBlur = e => {
67
+ dispatch({
68
+ type: components.SET_CANCEL_SUBSCRIPTION_REASON,
69
+ payload: e.target.value.trim()
70
+ });
71
+ };
72
+
73
+ return /*#__PURE__*/React__default['default'].createElement(components.TextArea, Object.assign({
74
+ label: t("labels.cancelReason"),
75
+ onBlur: e => handleOnTextAreaBlur(e)
76
+ }, props));
77
+ };
78
+
79
+ var _window$1, _window$Pelcro$1, _window$Pelcro$uiSett$1;
80
+ const ReactGA$1 = (_window$1 = window) !== null && _window$1 !== void 0 && (_window$Pelcro$1 = _window$1.Pelcro) !== null && _window$Pelcro$1 !== void 0 && (_window$Pelcro$uiSett$1 = _window$Pelcro$1.uiSettings) !== null && _window$Pelcro$uiSett$1 !== void 0 && _window$Pelcro$uiSett$1.enableReactGA4 ? components.ReactGA4 : components.ReactGA1;
81
+ const SubscriptionCancelNowButton = ({
82
+ subscription,
83
+ onClick,
84
+ className
85
+ }) => {
86
+ const {
87
+ switchView
88
+ } = components.usePelcro();
89
+ const {
90
+ state: {
91
+ cancelationReason
92
+ },
93
+ dispatch
94
+ } = React.useContext(store);
95
+ const {
96
+ t
97
+ } = components.useTranslation("subscriptionCancel");
98
+
99
+ const cancelSubscription = (payload, onSuccess, onFailure) => {
100
+ window.Pelcro.subscription.cancel({
101
+ auth_token: window.Pelcro.user.read().auth_token,
102
+ subscription_id: payload.subscription_id,
103
+ mode: payload.mode,
104
+ ...(payload.reason && {
105
+ reason: payload.reason
106
+ })
107
+ }, (err, res) => {
108
+ var _ReactGA$event;
109
+
110
+ if (err) {
111
+ return onFailure === null || onFailure === void 0 ? void 0 : onFailure(err);
112
+ }
113
+
114
+ ReactGA$1 === null || ReactGA$1 === void 0 ? void 0 : (_ReactGA$event = ReactGA$1.event) === null || _ReactGA$event === void 0 ? void 0 : _ReactGA$event.call(ReactGA$1, {
115
+ category: "ACTIONS",
116
+ action: "Canceled",
117
+ nonInteraction: true
118
+ });
119
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(res);
120
+ });
121
+ };
122
+
123
+ const handleCancelNowClick = () => {
124
+ const payload = {
125
+ subscription_id: subscription.id,
126
+ mode: "now",
127
+ ...(cancelationReason && {
128
+ reason: cancelationReason
129
+ })
130
+ };
131
+ onClick === null || onClick === void 0 ? void 0 : onClick(); //Close the modal
132
+
133
+ switchView(null); //Show confirmation alert after closing the modal
134
+
135
+ components.notify.confirm((onSuccess, onFailure) => {
136
+ cancelSubscription(payload, onSuccess, onFailure);
137
+ }, {
138
+ confirmMessage: t("messages.subCancellation.isSureToCancelNow"),
139
+ loadingMessage: t("messages.subCancellation.loading"),
140
+ successMessage: t("messages.subCancellation.success"),
141
+ errorMessage: t("messages.subCancellation.error")
142
+ }, {
143
+ closeButtonLabel: t("labels.subCancellation.goBack")
144
+ });
145
+ };
146
+
147
+ return /*#__PURE__*/React__default['default'].createElement(components.Button, {
148
+ onClick: handleCancelNowClick,
149
+ className: `${className}`
150
+ }, t("messages.cancelNow"));
151
+ };
152
+
153
+ var _window, _window$Pelcro, _window$Pelcro$uiSett;
154
+ const ReactGA = (_window = window) !== null && _window !== void 0 && (_window$Pelcro = _window.Pelcro) !== null && _window$Pelcro !== void 0 && (_window$Pelcro$uiSett = _window$Pelcro.uiSettings) !== null && _window$Pelcro$uiSett !== void 0 && _window$Pelcro$uiSett.enableReactGA4 ? components.ReactGA4 : components.ReactGA1;
155
+ const SubscriptionCancelLaterButton = ({
156
+ subscription,
157
+ onClick,
158
+ className
159
+ }) => {
160
+ const {
161
+ switchView
162
+ } = components.usePelcro();
163
+ const {
164
+ state: {
165
+ cancelationReason
166
+ },
167
+ dispatch
168
+ } = React.useContext(store);
169
+ const {
170
+ t
171
+ } = components.useTranslation("subscriptionCancel");
172
+
173
+ const cancelSubscription = (payload, onSuccess, onFailure) => {
174
+ window.Pelcro.subscription.cancel({
175
+ auth_token: window.Pelcro.user.read().auth_token,
176
+ subscription_id: payload.subscription_id,
177
+ mode: payload.mode,
178
+ ...(payload.reason && {
179
+ reason: payload.reason
180
+ })
181
+ }, (err, res) => {
182
+ var _ReactGA$event;
183
+
184
+ if (err) {
185
+ return onFailure === null || onFailure === void 0 ? void 0 : onFailure(err);
186
+ }
187
+
188
+ ReactGA === null || ReactGA === void 0 ? void 0 : (_ReactGA$event = ReactGA.event) === null || _ReactGA$event === void 0 ? void 0 : _ReactGA$event.call(ReactGA, {
189
+ category: "ACTIONS",
190
+ action: "Canceled",
191
+ nonInteraction: true
192
+ });
193
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(res);
194
+ });
195
+ };
196
+
197
+ const handleCancelNowClick = () => {
198
+ const payload = {
199
+ subscription_id: subscription.id,
200
+ mode: "period_end",
201
+ ...(cancelationReason && {
202
+ reason: cancelationReason
203
+ })
204
+ };
205
+ onClick === null || onClick === void 0 ? void 0 : onClick(); //Close the modal
206
+
207
+ switchView(null); //Show confirmation alert after closing the modal
208
+
209
+ components.notify.confirm((onSuccess, onFailure) => {
210
+ cancelSubscription(payload, onSuccess, onFailure);
211
+ }, {
212
+ confirmMessage: t("messages.subCancellation.isSureToCancel"),
213
+ loadingMessage: t("messages.subCancellation.loading"),
214
+ successMessage: t("messages.subCancellation.success"),
215
+ errorMessage: t("messages.subCancellation.error")
216
+ }, {
217
+ closeButtonLabel: t("labels.subCancellation.goBack")
218
+ });
219
+ };
220
+
221
+ return /*#__PURE__*/React__default['default'].createElement(components.Button, {
222
+ onClick: handleCancelNowClick,
223
+ className: `${className}`
224
+ }, t("messages.cancelLater"));
225
+ };
226
+
227
+ const SubscriptionCancelView = props => {
228
+ const {
229
+ subscriptionToCancel
230
+ } = components.usePelcro();
231
+ const {
232
+ t
233
+ } = components.useTranslation("subscriptionCancel");
234
+ return /*#__PURE__*/React__default['default'].createElement("div", {
235
+ id: "pelcro-subscription-cancel-view"
236
+ }, /*#__PURE__*/React__default['default'].createElement(SubscriptionCancelContainer, props, /*#__PURE__*/React__default['default'].createElement("div", {
237
+ className: "plc-flex plc-flex-col plc-items-center plc-justify-center plc-mt-4"
238
+ }, /*#__PURE__*/React__default['default'].createElement(components.SvgSubscription, {
239
+ className: "plc-w-32 plc-h-32"
240
+ }), /*#__PURE__*/React__default['default'].createElement("p", {
241
+ className: "plc-mb-3 plc-text-gray-900 plc-text-center plc-whitespace-pre-line"
242
+ }, t("messages.subscriptionEnd"), " ", new Date(subscriptionToCancel === null || subscriptionToCancel === void 0 ? void 0 : subscriptionToCancel.current_period_end).toLocaleDateString("en-CA", {
243
+ year: "numeric",
244
+ month: "short",
245
+ day: "numeric"
246
+ }), "."), /*#__PURE__*/React__default['default'].createElement(SubscriptionCancelReason, null), /*#__PURE__*/React__default['default'].createElement(SubscriptionCancelNowButton, {
247
+ className: "plc-mb-2",
248
+ subscription: subscriptionToCancel
249
+ }), subscriptionToCancel.cancel_at_period_end === 0 && /*#__PURE__*/React__default['default'].createElement(SubscriptionCancelLaterButton, {
250
+ subscription: subscriptionToCancel
251
+ }))));
252
+ };
253
+
254
+ exports.SubscriptionCancelView = SubscriptionCancelView;