-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFeedback.js
231 lines (215 loc) · 9.85 KB
/
Feedback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as L from "./Logger.js";
class Feedback {
#state;
constructor(state) {
this.#state = state;
}
/**
* Shows the first available NPS widget that meets the criteria.
* @param {String} [nameIDorTag] - name, id, or tag of the widget to show (optional)
* @param {callback} [callback] - called when the widget is closed (optional)
*/
showNPS(nameIDorTag, callback) {
L.i(`showNPS, Will show NPS widget with name, id, or tag: [${nameIDorTag}], callback provided: [${typeof callback === "function"}]`);
this.#showInternalFeedback("nps", nameIDorTag, callback);
}
/**
* Shows the first available Survey widget that meets the criteria.
* @param {String} [nameIDorTag] - name, id, or tag of the widget to show (optional)
* @param {callback} [callback] - called when the widget is closed (optional)
*/
showSurvey(nameIDorTag, callback) {
L.i(`showSurvey, Will show Survey widget with name, id, or tag: [${nameIDorTag}], callback provided: [${typeof callback === "function"}]`);
this.#showInternalFeedback("survey", nameIDorTag, callback);
}
/**
* Shows the first available Rating widget that meets the criteria.
* @param {String} [nameIDorTag] - name, id, or tag of the widget to show (optional)
* @param {callback} [callback] - called when the widget is closed (optional)
*/
showRating(nameIDorTag, callback) {
L.i(`showRating, Will show Rating widget with name, id, or tag: [${nameIDorTag}], callback provided: [${typeof callback === "function"}]`);
this.#showInternalFeedback("rating", nameIDorTag, callback);
}
#showInternalFeedback(widgetType, nameIDorTag, callback) {
if (!this.#state.isInitialized) {
L.e(`showInternalFeedback, 'init' must be called before 'showInternalFeedback'`);
return;
}
if (typeof nameIDorTag !== "string") {
L.d(`showInternalFeedback, unsupported data type of nameIDorTag or its not given : [${typeof nameIDorTag}]`);
}
this.getAvailableFeedbackWidgets((retrievedWidgets, error) => {
if (error) {
L.e(`showInternalFeedback, ${error}`);
return;
}
if (!retrievedWidgets || retrievedWidgets.length === 0) {
L.d(`showInternalFeedback, no feedback widgets found`);
return;
}
L.d(`showInternalFeedback, Found [${retrievedWidgets.length}] feedback widgets`);
let widget = retrievedWidgets.find(w => w.type === widgetType);
try {
if (nameIDorTag && typeof nameIDorTag === 'string') {
const matchedWidget = retrievedWidgets.find(w =>
w.type === widgetType && (w.name === nameIDorTag || w.id === nameIDorTag || w.tags.includes(nameIDorTag))
);
if (matchedWidget) {
widget = matchedWidget;
L.v(`showInternalFeedback, Found ${widgetType} widget by name, id, or tag: [${JSON.stringify(matchedWidget)}]`);
}
}
} catch (error) {
L.e(`showInternalFeedback, Error while finding widget: ${error}`);
}
if (!widget) {
L.d(`showInternalFeedback, No ${widgetType} widget found.`);
return;
}
this.presentFeedbackWidget(widget, null, null, callback);
});
}
/**
* Get a list of available feedback widgets as an array of objects.
* @param {callback} [onFinished] - returns (retrievedWidgets, error). This parameter is optional.
* @return {object} object {error: String or null, data: Array or null }
*/
async getAvailableFeedbackWidgets(onFinished) {
if (!this.#state.isInitialized) {
const message = "'init' must be called before 'getAvailableFeedbackWidgets'";
L.e(`getAvailableFeedbackWidgets, ${message}`);
return { error: message, data: null };
}
L.d("getAvailableFeedbackWidgets, fetching available feedback widgets");
let result = null;
let error = null;
try {
result = await this.#state.CountlyReactNative.getFeedbackWidgets();
} catch (e) {
error = e.message;
}
if (onFinished) {
onFinished(result, error);
}
return { error: error, data: result };
}
/**
* Present a chosen feedback widget
*
* @param {object} feedbackWidget - feedback Widget with id, type and name
* @param {string} closeButtonText - text for cancel/close button
* @param {callback} [widgetShownCallback] - Callback to be executed when feedback widget is displayed. This parameter is optional.
* @param {callback} [widgetClosedCallback] - Callback to be executed when feedback widget is closed. This parameter is optional.
*
* @return {object} object {error: string or null}
*/
presentFeedbackWidget(feedbackWidget, closeButtonText, widgetShownCallback, widgetClosedCallback) {
if (!this.#state.isInitialized) {
const message = "'init' must be called before 'presentFeedbackWidget'";
L.e(`presentFeedbackWidget, ${message}`);
return { error: message };
}
let message = null;
if (!feedbackWidget) {
message = "feedbackWidget should not be null or undefined";
L.e(`presentFeedbackWidget, ${message}`);
return { error: message };
}
if (!feedbackWidget.id) {
message = "FeedbackWidget id should not be null or empty";
L.e(`presentFeedbackWidget, ${message}`);
return { error: message };
}
if (!feedbackWidget.type) {
message = "FeedbackWidget type should not be null or empty";
L.e(`presentFeedbackWidget, ${message}`);
return { error: message };
}
if (typeof closeButtonText !== "string") {
closeButtonText = "";
L.w(`presentFeedbackWidget, unsupported data type of closeButtonText : [${typeof args}]`);
}
L.d(`presentFeedbackWidget, presentFeedbackWidget with widget:[${JSON.stringify(feedbackWidget)}]`);
if (widgetShownCallback) {
this.#state.widgetShownCallback = this.#state.eventEmitter.addListener(this.#state.widgetShownCallbackName, () => {
widgetShownCallback();
this.#state.widgetShownCallback.remove();
});
}
if (widgetClosedCallback) {
this.#state.widgetClosedCallback = this.#state.eventEmitter.addListener(this.#state.widgetClosedCallbackName, () => {
widgetClosedCallback();
this.#state.widgetClosedCallback.remove();
});
}
feedbackWidget.name = feedbackWidget.name || "";
closeButtonText = closeButtonText || "";
this.#state.CountlyReactNative.presentFeedbackWidget([feedbackWidget.id, feedbackWidget.type, feedbackWidget.name, closeButtonText]);
return { error: null };
}
/**
* Get a feedback widget's data as an object.
* @param {object} widgetInfo - widget to get data for. You should get this from 'getAvailableFeedbackWidgets' method.
* @param {callback} [onFinished] - returns (object retrievedWidgetData, error). This parameter is optional.
* @return {object} object {error: string, data: object or null}
*/
async getFeedbackWidgetData(widgetInfo, onFinished) {
if (!this.#state.isInitialized) {
const message = "'initWithConfig' must be called before 'getFeedbackWidgetData'";
L.e(`getFeedbackWidgetData, ${message}`);
return { error: message, data: null };
}
const widgetId = widgetInfo.id;
const widgetType = widgetInfo.type;
L.d(`getFeedbackWidgetData, Calling "getFeedbackWidgetData" with Type:[${widgetType}]`);
const args = [];
args.push(widgetId);
args.push(widgetType);
args.push(widgetInfo.name);
let result = null;
let error = null;
try {
result = await this.#state.CountlyReactNative.getFeedbackWidgetData(args);
} catch (e) {
error = e.message;
}
if (onFinished) {
onFinished(result, error);
}
return { error: error, data: result };
}
/**
* Report manually for a feedback widget.
* @param {object} widgetInfo - the widget you are targeting. You should get this from 'getAvailableFeedbackWidgets' method.
* @param {object} widgetData - data of that widget. You should get this from 'getFeedbackWidgetData' method.
* @param {object} widgetResult - Information you want to report.
* @return {object} object {error: string}
*/
async reportFeedbackWidgetManually(widgetInfo, widgetData, widgetResult) {
if (!this.#state.isInitialized) {
const message = "'initWithConfig' must be called before 'reportFeedbackWidgetManually'";
L.e(`reportFeedbackWidgetManually, ${message}`);
return { error: message };
}
const widgetId = widgetInfo.id;
const widgetType = widgetInfo.type;
L.d(`reportFeedbackWidgetManually, Calling "reportFeedbackWidgetManually" with Type:[${widgetType}]`);
const widgetInfoList = [];
widgetInfoList.push(widgetId);
widgetInfoList.push(widgetType);
widgetInfoList.push(widgetInfo.name);
const args = [];
args.push(widgetInfoList);
args.push(widgetData);
args.push(widgetResult);
let error = null;
try {
await this.#state.CountlyReactNative.reportFeedbackWidgetManually(args);
} catch (e) {
error = e.message;
}
return { error: error };
}
}
export default Feedback;