@subiz/widget-v4 4.1.63 → 4.1.65
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/assets/less/widget.less
CHANGED
|
@@ -40,7 +40,11 @@ class Widget extends _common.Com {
|
|
|
40
40
|
}
|
|
41
41
|
let text = message.text;
|
|
42
42
|
if (!this.props.cid) {
|
|
43
|
-
this.
|
|
43
|
+
this._creating = (this._creating || 0) + 1;
|
|
44
|
+
let _creating = this._creating;
|
|
45
|
+
this.setState({
|
|
46
|
+
creating: true
|
|
47
|
+
});
|
|
44
48
|
const {
|
|
45
49
|
convo,
|
|
46
50
|
error
|
|
@@ -49,20 +53,27 @@ class Widget extends _common.Com {
|
|
|
49
53
|
message: text,
|
|
50
54
|
initial_by: this.props.store.matchUser().id
|
|
51
55
|
});
|
|
56
|
+
if (_creating != this._creating) return;
|
|
52
57
|
if (error) {}
|
|
53
|
-
this.
|
|
54
|
-
this.props.store.sendConvoEvent(convo.id, 'message_sent', {
|
|
58
|
+
await this.props.store.sendConvoEvent(convo.id, 'message_sent', {
|
|
55
59
|
message
|
|
56
60
|
});
|
|
61
|
+
if (_creating != this._creating) return;
|
|
62
|
+
this.setState({
|
|
63
|
+
creating: false
|
|
64
|
+
});
|
|
57
65
|
this.props.onGo({
|
|
58
66
|
view: 'chat',
|
|
59
67
|
cid: convo.id
|
|
60
68
|
});
|
|
61
69
|
return;
|
|
62
70
|
}
|
|
63
|
-
this.props.store.sendConvoEvent(this.props.cid, 'message_sent', {
|
|
71
|
+
await this.props.store.sendConvoEvent(this.props.cid, 'message_sent', {
|
|
64
72
|
message
|
|
65
73
|
});
|
|
74
|
+
this.setState({
|
|
75
|
+
creating: false
|
|
76
|
+
});
|
|
66
77
|
});
|
|
67
78
|
_defineProperty(this, "onLink", (e, url) => {
|
|
68
79
|
if (!url) return e.preventDefault();
|
|
@@ -99,11 +110,13 @@ class Widget extends _common.Com {
|
|
|
99
110
|
}
|
|
100
111
|
componentDidMount() {
|
|
101
112
|
if (!this.props.store.isMobile()) this.focusInput();
|
|
113
|
+
this.updateFormDataFields();
|
|
102
114
|
}
|
|
103
115
|
componentDidUpdate(prevProp) {
|
|
104
116
|
if (prevProp.isShow !== this.props.isShow) {
|
|
105
117
|
if (this.props.isShow && !this.props.store.isMobile()) this.focusInput();
|
|
106
118
|
if (this.props.isShow && this.props.store.isMobile()) this.blur();
|
|
119
|
+
this.updateFormDataFields();
|
|
107
120
|
}
|
|
108
121
|
if (prevProp.cid !== this.props.cid) {
|
|
109
122
|
this.closeOrder();
|
|
@@ -115,8 +128,8 @@ class Widget extends _common.Com {
|
|
|
115
128
|
this.events && this.events.focus();
|
|
116
129
|
if (!this.props.store.isMobile()) this.focusInput();
|
|
117
130
|
}, 100);
|
|
131
|
+
this.updateFormDataFields();
|
|
118
132
|
}
|
|
119
|
-
this.updateFormDataFields();
|
|
120
133
|
}
|
|
121
134
|
shouldShowForm() {
|
|
122
135
|
let form;
|
|
@@ -131,6 +144,7 @@ class Widget extends _common.Com {
|
|
|
131
144
|
}
|
|
132
145
|
render() {
|
|
133
146
|
let showform = this.shouldShowForm();
|
|
147
|
+
let formName = '';
|
|
134
148
|
let form;
|
|
135
149
|
let accountSetting = this.props.plugin.chatbox || {};
|
|
136
150
|
let acc = this.props.store.matchAccount() || {};
|
|
@@ -140,19 +154,22 @@ class Widget extends _common.Com {
|
|
|
140
154
|
if (this.isOffline(acc, accountSetting) && offlineEnabled) {
|
|
141
155
|
form = accountSetting.offline_form;
|
|
142
156
|
submitLabel = this.props.t('offline_form_btn');
|
|
157
|
+
formName = 'offline';
|
|
143
158
|
}
|
|
144
159
|
if (this.props.view === 'offline_form') {
|
|
145
160
|
showform = true;
|
|
146
161
|
form = accountSetting.offline_form;
|
|
162
|
+
formName = 'offline';
|
|
147
163
|
submitLabel = this.props.t('offline_form_btn');
|
|
148
164
|
}
|
|
149
165
|
if (this.props.view === 'prechat_form') {
|
|
150
166
|
showform = true;
|
|
151
167
|
form = accountSetting.prechat_form;
|
|
152
168
|
submitLabel = this.props.t('prechat_form_btn');
|
|
169
|
+
formName = 'prechat';
|
|
153
170
|
}
|
|
154
171
|
let $body = null;
|
|
155
|
-
if (this.props.view === 'list') $body = this.renderConvoList();else if (showform) $body = this.renderForm(form, submitLabel);else $body = this.renderChat();
|
|
172
|
+
if (this.props.view === 'list') $body = this.renderConvoList();else if (showform) $body = this.renderForm(form, submitLabel, formName);else $body = this.renderChat();
|
|
156
173
|
let position = _index.default.get(accountSetting, 'desktop_button.position') || 'right';
|
|
157
174
|
if (this.props.store.isMobile()) position = _index.default.get(accountSetting, 'mobile_button.position', position);
|
|
158
175
|
let cls = 'widget-container widget-container--' + position;
|
|
@@ -172,7 +189,8 @@ class Widget extends _common.Com {
|
|
|
172
189
|
})
|
|
173
190
|
}), $body);
|
|
174
191
|
}
|
|
175
|
-
async submitPrechatForm(form) {
|
|
192
|
+
async submitPrechatForm(form, fornName) {
|
|
193
|
+
if (this.state.submiting) return;
|
|
176
194
|
var hasError = false;
|
|
177
195
|
_index.default.map(this.state.formData, (field, key) => {
|
|
178
196
|
if (field.is_required && (field.value === undefined || field.value === '' || _index.default.size(field.value) === 0 && Array.isArray(field.value))) {
|
|
@@ -210,8 +228,20 @@ class Widget extends _common.Com {
|
|
|
210
228
|
});
|
|
211
229
|
let firstmsg = _index.default.get(data, 'question.value');
|
|
212
230
|
let groupid = _index.default.get(data, 'group.value', '');
|
|
231
|
+
this._creating = (this._creating || 0) + 1;
|
|
232
|
+
let _creating = this._creating;
|
|
233
|
+
this.setState({
|
|
234
|
+
creating: true
|
|
235
|
+
});
|
|
236
|
+
let out = await this.props.store.submitForm('chatbox_' + fornName, _index.default.map(data, (field, key) => {
|
|
237
|
+
return {
|
|
238
|
+
key: key,
|
|
239
|
+
type: field.type,
|
|
240
|
+
value: field.value + ''
|
|
241
|
+
};
|
|
242
|
+
}), user);
|
|
243
|
+
if (_creating != this._creating) return;
|
|
213
244
|
if (this.props.cid) {
|
|
214
|
-
this.props.store.setUser(user);
|
|
215
245
|
this.setState({
|
|
216
246
|
clickedJoin: false
|
|
217
247
|
});
|
|
@@ -226,15 +256,15 @@ class Widget extends _common.Com {
|
|
|
226
256
|
attachments: this.state.quickReplyMessage.attachments
|
|
227
257
|
}
|
|
228
258
|
});
|
|
259
|
+
if (_creating != this._creating) return;
|
|
229
260
|
this.setState({
|
|
261
|
+
creating: false,
|
|
230
262
|
quickReplyMessage: {}
|
|
231
263
|
});
|
|
232
264
|
if (form.end_conversation_after_submit) await this.props.store.endConvo(this.props.cid);
|
|
265
|
+
if (_creating != this._creating) return;
|
|
233
266
|
return;
|
|
234
267
|
}
|
|
235
|
-
this.setState({
|
|
236
|
-
creating: true
|
|
237
|
-
});
|
|
238
268
|
let message = {
|
|
239
269
|
text: firstmsg,
|
|
240
270
|
format: 'plaintext'
|
|
@@ -248,11 +278,11 @@ class Widget extends _common.Com {
|
|
|
248
278
|
this.setState({
|
|
249
279
|
creatingError: null
|
|
250
280
|
});
|
|
251
|
-
await this.props.store.setUser(user);
|
|
252
281
|
const {
|
|
253
282
|
error,
|
|
254
283
|
convo
|
|
255
284
|
} = await this.props.store.startConvo(req);
|
|
285
|
+
if (_creating != this._creating) return;
|
|
256
286
|
if (error) {
|
|
257
287
|
this.setState({
|
|
258
288
|
creating: false,
|
|
@@ -262,18 +292,19 @@ class Widget extends _common.Com {
|
|
|
262
292
|
}
|
|
263
293
|
let newconvoid = convo.id;
|
|
264
294
|
this.userSent[newconvoid] = true;
|
|
265
|
-
this.props.
|
|
266
|
-
view: 'chat',
|
|
267
|
-
cid: newconvoid
|
|
268
|
-
});
|
|
269
|
-
this.props.store.sendConvoEvent(newconvoid, 'message_sent', {
|
|
295
|
+
await this.props.store.sendConvoEvent(newconvoid, 'message_sent', {
|
|
270
296
|
message
|
|
271
297
|
});
|
|
298
|
+
if (_creating != this._creating) return;
|
|
272
299
|
this.setState({
|
|
273
300
|
creating: false,
|
|
274
301
|
clickedJoin: false,
|
|
275
302
|
quickReplyMessage: {}
|
|
276
303
|
});
|
|
304
|
+
this.props.onGo({
|
|
305
|
+
view: 'chat',
|
|
306
|
+
cid: newconvoid
|
|
307
|
+
});
|
|
277
308
|
if (!this.props.store.isMobile()) this.focusInput();
|
|
278
309
|
}
|
|
279
310
|
formMounted(form) {
|
|
@@ -455,6 +486,7 @@ class Widget extends _common.Com {
|
|
|
455
486
|
},
|
|
456
487
|
value: field.value || '',
|
|
457
488
|
onInput: e => this.onInputChange(key, e.target.value),
|
|
489
|
+
onChange: e => this.onInputChange(key, e.target.value),
|
|
458
490
|
row: "4",
|
|
459
491
|
class: "form-control"
|
|
460
492
|
});
|
|
@@ -462,6 +494,7 @@ class Widget extends _common.Com {
|
|
|
462
494
|
$field = (0, _preact.h)("input", {
|
|
463
495
|
value: field.value || '',
|
|
464
496
|
onInput: e => this.onInputChange(key, e.target.value),
|
|
497
|
+
onChange: e => this.onInputChange(key, e.target.value),
|
|
465
498
|
ref: ref => {
|
|
466
499
|
this['form_' + encode(key)] = ref;
|
|
467
500
|
},
|
|
@@ -540,7 +573,9 @@ class Widget extends _common.Com {
|
|
|
540
573
|
label = field.label || this.props.t('your_question');
|
|
541
574
|
}
|
|
542
575
|
if (field.label) label = field.label;
|
|
543
|
-
return (0, _preact.h)("div",
|
|
576
|
+
return (0, _preact.h)("div", {
|
|
577
|
+
key: 'form_' + encode(key)
|
|
578
|
+
}, (0, _preact.h)("div", {
|
|
544
579
|
class: "label"
|
|
545
580
|
}, label, field.is_required ? (0, _preact.h)("span", {
|
|
546
581
|
class: "ml-1 red"
|
|
@@ -552,14 +587,7 @@ class Widget extends _common.Com {
|
|
|
552
587
|
class: "pt-2"
|
|
553
588
|
}, $fields);
|
|
554
589
|
}
|
|
555
|
-
renderForm(form, submitLabel) {
|
|
556
|
-
let $loading = null;
|
|
557
|
-
if (this.state.creating) $loading = (0, _preact.h)("span", {
|
|
558
|
-
class: "ml-2"
|
|
559
|
-
}, (0, _preact.h)(_spinner.default, {
|
|
560
|
-
size: "16",
|
|
561
|
-
mode: "white"
|
|
562
|
-
}));
|
|
590
|
+
renderForm(form, submitLabel, formName) {
|
|
563
591
|
let title = this.props.t.i18n(_index.default.get(form, `i18n_title`, {}));
|
|
564
592
|
if (!title) title = _index.default.get(form, 'title', this.props.t('capture_lead_title'));
|
|
565
593
|
title = title.replace('↵', '');
|
|
@@ -581,8 +609,9 @@ class Widget extends _common.Com {
|
|
|
581
609
|
class: "message-input message-input--preform"
|
|
582
610
|
}, (0, _preact.h)("button", {
|
|
583
611
|
class: "btn--start-convo",
|
|
584
|
-
onClick: _ => this.submitPrechatForm(form)
|
|
585
|
-
|
|
612
|
+
onClick: _ => this.submitPrechatForm(form, formName),
|
|
613
|
+
disabled: this.state.creating
|
|
614
|
+
}, submitLabel)));
|
|
586
615
|
}
|
|
587
616
|
goToConvo(cid) {
|
|
588
617
|
this.props.onGo({
|
|
@@ -40,6 +40,23 @@ class _default {
|
|
|
40
40
|
_defineProperty(this, "getConversation", cid => this.apireq.get(`/accounts/${this.accid}/conversations/${cid}`));
|
|
41
41
|
_defineProperty(this, "getLocaleContent", locale => this.apireq.get(`/accounts/${this.accid}/locales/${locale}`));
|
|
42
42
|
_defineProperty(this, "sendEvent", data => this.apireq.addQuery('shorten', true).post(`/accounts/${this.accid}/users/${this.uid}/events`, data));
|
|
43
|
+
_defineProperty(this, "submitForm", (form_id, page_url, device, data, user) => this.apireq.post(`/accounts/${this.accid}/users/${this.uid}/events`, {
|
|
44
|
+
by: {
|
|
45
|
+
id: this.uid,
|
|
46
|
+
device: device
|
|
47
|
+
},
|
|
48
|
+
type: 'form_submitted',
|
|
49
|
+
data: {
|
|
50
|
+
user: user,
|
|
51
|
+
form_submitted: {
|
|
52
|
+
user_id: this.uid,
|
|
53
|
+
form_id: form_id,
|
|
54
|
+
page_url: page_url,
|
|
55
|
+
submitted_at: _index.default.Date.now(),
|
|
56
|
+
values: data
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}));
|
|
43
60
|
_defineProperty(this, "sendTyping", cid => this.apireq.post(`/accounts/${this.accid}/conversations/${cid}/typing`));
|
|
44
61
|
_defineProperty(this, "sendLostFocus", (device, productid, source_id, sku) => this.sendEvent({
|
|
45
62
|
by: {
|
|
@@ -51,6 +51,7 @@ class _default {
|
|
|
51
51
|
}));
|
|
52
52
|
_defineProperty(this, "getLocaleContent", locale => this.apireq.get(`/accounts/${this.accid}/locales/${locale}`));
|
|
53
53
|
_defineProperty(this, "sendEvent", data => OK200);
|
|
54
|
+
_defineProperty(this, "submitForm", (form_id, page_url, device, data, user) => OK200);
|
|
54
55
|
_defineProperty(this, "sendTyping", cid => OK200);
|
|
55
56
|
_defineProperty(this, "sendPresence", data => OK200);
|
|
56
57
|
_defineProperty(this, "sendContent", data => OK200);
|
|
@@ -481,6 +481,11 @@ class Store {
|
|
|
481
481
|
}
|
|
482
482
|
return this.reconnectGate.open();
|
|
483
483
|
}
|
|
484
|
+
async submitForm(formid, data, user) {
|
|
485
|
+
await this.readyGate.entry();
|
|
486
|
+
if (user) await this.setUser(user);
|
|
487
|
+
return this.api.submitForm(formid, document.URL, this.device, data, user);
|
|
488
|
+
}
|
|
484
489
|
async setUser(user) {
|
|
485
490
|
if (!user) return;
|
|
486
491
|
await this.readyGate.entry();
|
|
@@ -684,36 +689,6 @@ class Store {
|
|
|
684
689
|
this._setConvo(convo);
|
|
685
690
|
this.pubsub.publish('account');
|
|
686
691
|
}
|
|
687
|
-
handleFormSubmit(convoKey, ev) {
|
|
688
|
-
if (ev.type !== 'message_sent') return;
|
|
689
|
-
let formSubmits = [];
|
|
690
|
-
let attachments = _index.default.get(ev, 'data.message.attachments') || [];
|
|
691
|
-
_index.default.each(attachments, att => {
|
|
692
|
-
if (!att) return;
|
|
693
|
-
if (att.type !== 'form_submit') return;
|
|
694
|
-
formSubmits.push(att.form_submit);
|
|
695
|
-
});
|
|
696
|
-
_index.default.each(formSubmits, submit => {
|
|
697
|
-
let formev = _index.default.get(this.convoEventM, convoKey + '.' + submit.form_message_id);
|
|
698
|
-
if (!formev) return;
|
|
699
|
-
let forms = _index.default.filter(_index.default.get(formev, 'data.message.attachments'), att => {
|
|
700
|
-
if (!att) return false;
|
|
701
|
-
if (att.type !== 'form' && att.type !== 'ask_info_form') return false;
|
|
702
|
-
return true;
|
|
703
|
-
});
|
|
704
|
-
_index.default.each(forms, form => {
|
|
705
|
-
form = form.form;
|
|
706
|
-
_index.default.each(form.fields, formfield => {
|
|
707
|
-
_index.default.each(submit.fields, submitfield => {
|
|
708
|
-
if (formfield.name !== submitfield.name) return;
|
|
709
|
-
if (formfield.default_answer) return;
|
|
710
|
-
formfield.default_answer = submitfield.value;
|
|
711
|
-
});
|
|
712
|
-
});
|
|
713
|
-
});
|
|
714
|
-
if (_index.default.size(forms) > 0) this.pubsub.publish('state.convoEventM', this.convoEventM, convoKey);
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
692
|
typing(cid) {
|
|
718
693
|
if (!cid) return;
|
|
719
694
|
this._typing(cid);
|
package/package.json
CHANGED
package/src/chatbox/Widget.js
CHANGED
|
@@ -203,7 +203,6 @@ export default class Widget extends Com {
|
|
|
203
203
|
)
|
|
204
204
|
|
|
205
205
|
if (_creating != this._creating) return // outdated
|
|
206
|
-
return
|
|
207
206
|
if (this.props.cid) {
|
|
208
207
|
// this.props.store.setUser(user)
|
|
209
208
|
this.setState({clickedJoin: false})
|
|
@@ -226,14 +225,13 @@ export default class Widget extends Com {
|
|
|
226
225
|
return
|
|
227
226
|
}
|
|
228
227
|
|
|
229
|
-
return
|
|
230
|
-
|
|
231
228
|
let message = {text: firstmsg, format: 'plaintext'}
|
|
232
229
|
let req = {group_id: groupid, message: firstmsg, locale: this.props.t.locale(), initial_by: user.id}
|
|
233
230
|
|
|
234
231
|
this.setState({creatingError: null})
|
|
235
232
|
// await this.props.store.setUser(user)
|
|
236
233
|
const {error, convo} = await this.props.store.startConvo(req)
|
|
234
|
+
|
|
237
235
|
if (_creating != this._creating) return // outdated
|
|
238
236
|
if (error) {
|
|
239
237
|
this.setState({creating: false, creatingError: error})
|
|
@@ -749,7 +747,6 @@ export default class Widget extends Com {
|
|
|
749
747
|
return
|
|
750
748
|
}
|
|
751
749
|
await this.props.store.sendConvoEvent(this.props.cid, 'message_sent', {message})
|
|
752
|
-
if (_creating != this._creating) return // outdated
|
|
753
750
|
this.setState({creating: false})
|
|
754
751
|
}
|
|
755
752
|
|