-
Notifications
You must be signed in to change notification settings - Fork 25
/
Feedback.js
161 lines (149 loc) · 6.48 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
import * as L from "./Logger.js";
class Feedback {
#state;
constructor(state) {
this.#state = state;
}
/**
* 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, getAvailableFeedbackWidgets");
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;