@hellotext/hellotext 1.7.1 → 1.7.2

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.
@@ -1,101 +0,0 @@
1
- import { Controller } from '@hotwired/stimulus'
2
-
3
- import Hellotext from '../hellotext'
4
- import SubmissionsAPI from '../api/submissions'
5
-
6
- export default class extends Controller {
7
- static values = {
8
- submissionId: String,
9
- }
10
-
11
- static targets = ['input', 'submitButton', 'resendButton']
12
-
13
- initialize() {
14
- super.initialize()
15
-
16
- this.attempts = 0
17
- this.onInputChange = this.onInputChange.bind(this)
18
-
19
- this.throttleInterval = setInterval(() => {
20
- this.attempts = 0
21
- }, 60000)
22
- }
23
-
24
- connect() {
25
- this.inputTarget.addEventListener('input', this.onInputChange)
26
- this.inputTarget.focus()
27
-
28
- super.connect()
29
- }
30
-
31
- disconnect() {
32
- clearInterval(this.throttleInterval)
33
- this.inputTarget.removeEventListener('input', this.onInputChange)
34
-
35
- super.disconnect()
36
- }
37
-
38
- async resend() {
39
- if (this.throttled) {
40
- return alert(Hellotext.business.locale.otp.throttled)
41
- }
42
-
43
- this.resendButtonTarget.disabled = true
44
- const response = await SubmissionsAPI.resendOTP(this.submissionIdValue)
45
-
46
- if (response.succeeded) {
47
- this.resendButtonTarget.disabled = false
48
- }
49
-
50
- alert(Hellotext.business.locale.otp.resend_successful)
51
- this.attempts += 1
52
- }
53
-
54
- onInputChange() {
55
- if (this.inputTarget.value.length !== 6) return
56
- this.submit()
57
- }
58
-
59
- async submit() {
60
- if(this.inputTarget.value.length !== 6) {
61
- return alert(Hellotext.business.locale.otp.invalid)
62
- }
63
-
64
- this.disable()
65
-
66
- const response = await SubmissionsAPI.verifyOTP(this.submissionIdValue, this.inputTarget.value)
67
-
68
- if (response.succeeded) {
69
- this.dispatch('verified', {
70
- detail: { submissionId: this.submissionIdValue },
71
- })
72
- } else {
73
- alert(Hellotext.business.locale.otp.invalid)
74
-
75
- setTimeout(() => {
76
- this.inputTarget.value = ''
77
- this.inputTarget.focus()
78
- })
79
- }
80
-
81
- this.enable()
82
- }
83
-
84
- // private
85
-
86
- disable() {
87
- this.inputTarget.disabled = true
88
- this.resendButtonTarget.disabled = true
89
- this.submitButtonTarget.disabled = true
90
- }
91
-
92
- enable() {
93
- this.inputTarget.disabled = false
94
- this.resendButtonTarget.disabled = false
95
- this.submitButtonTarget.disabled = false
96
- }
97
-
98
- get throttled() {
99
- return this.attempts >= 3
100
- }
101
- }