Skip to content

Commit 9fe63d2

Browse files
authored
Merge pull request #4 from tutorcruncher/allow-multiple-recaptchas
allowing multiple recaptchas
2 parents 5bbdf81 + 07d9a45 commit 9fe63d2

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tutorcruncher-socket",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
44
"description": "TutorCruncher socket",
55
"author": "Samuel Colvin <[email protected]>",
66
"private": false,

src/components/enquiry.vue

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<tcs-input :field="field"></tcs-input>
1515
</div>
1616

17-
<div :id="grecaptcha_container_id" class="grecaptcha"></div>
17+
<div :id="$root.grecaptcha_container_id" class="grecaptcha"></div>
1818
<div v-if="grecaptcha_missing" class="error-msg">
1919
{{ $root.get_text('grecaptcha_missing') }}
2020
</div>
@@ -61,8 +61,6 @@ export default {
6161
data: () => ({
6262
submitted: false,
6363
grecaptcha_missing: false,
64-
grecaptcha_container_id: 'grecaptcha_' + Math.random().toString(36).substring(2, 10),
65-
grecaptcha_id: null,
6664
}),
6765
methods: {
6866
submit () {
@@ -83,24 +81,15 @@ export default {
8381
},
8482
/* istanbul ignore next */
8583
prepare_grecaptcha () {
86-
const grecaptcha_callback = response => this.$set(this.$root.enquiry_data, 'grecaptcha_response', response)
87-
8884
if (this.$root.grecaptcha_key === null) {
89-
grecaptcha_callback('-')
85+
this.$root.grecaptcha_callback('-')
9086
return
9187
}
9288
93-
const render_grecaptcha = () => {
94-
this.grecaptcha_id = window.grecaptcha.render(this.grecaptcha_container_id, {
95-
sitekey: this.$root.grecaptcha_key,
96-
callback: grecaptcha_callback
97-
})
98-
}
9989
if (window.grecaptcha === undefined) {
100-
window._tcs_grecaptcha_loaded = render_grecaptcha
10190
add_script('https://www.google.com/recaptcha/api.js?onload=_tcs_grecaptcha_loaded&render=explicit')
10291
} else {
103-
render_grecaptcha()
92+
this.$root.render_grecaptcha()
10493
}
10594
}
10695
},

src/main.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const ConfiguredVueRouter = config => {
6767
}
6868
)
6969
}
70+
console.debug('setting routes:', routes)
7071
return new VueRouter({
7172
mode: config.router_mode,
7273
routes: routes
@@ -101,6 +102,18 @@ module.exports = function (public_key, config) {
101102
config: config,
102103
})
103104

105+
if (!config.console) {
106+
config.console = console
107+
if (!window.localStorage.tcs_enable_debug) {
108+
if (!window.tcs_debug_log) {
109+
window.tcs_debug_log = []
110+
}
111+
console.debug = function () {
112+
window.tcs_debug_log.push(Array.from(arguments).join())
113+
}
114+
}
115+
}
116+
104117
let error = null
105118
if (!config.mode) {
106119
config.mode = 'grid'
@@ -132,10 +145,6 @@ module.exports = function (public_key, config) {
132145
config.router_mode = 'hash'
133146
}
134147

135-
if (!config.console) {
136-
config.console = console
137-
}
138-
139148
if (!config.element) {
140149
config.element = '#socket'
141150
}
@@ -163,7 +172,9 @@ module.exports = function (public_key, config) {
163172

164173
const ga_prefixes = init_ga(router, config)
165174

166-
return new Vue({
175+
console.debug('using config:', config)
176+
177+
const v = new Vue({
167178
el: config.element,
168179
router: router,
169180
render: h => h(app),
@@ -178,6 +189,7 @@ module.exports = function (public_key, config) {
178189
enquiry_form_info: {},
179190
enquiry_data: {},
180191
selected_subject_id: null,
192+
grecaptcha_container_id: 'grecaptcha_' + Math.random().toString(36).substring(2, 10),
181193
},
182194
components: {
183195
app
@@ -190,7 +202,8 @@ module.exports = function (public_key, config) {
190202
}
191203
},
192204
watch: {
193-
'$route' (to) {
205+
'$route' (to, from) {
206+
console.debug(`route change ${from.path} to ${to.path}`, from, to)
194207
if (this.config.mode === 'grid' && to.name === 'index') {
195208
this.get_contractor_list()
196209
}
@@ -208,9 +221,11 @@ module.exports = function (public_key, config) {
208221
},
209222
request (url, callback, expected_status, method, data) {
210223
const xhr = new window.XMLHttpRequest()
211-
xhr.open(method || 'GET', url)
224+
method = method || 'GET'
225+
xhr.open(method, url)
212226
xhr.setRequestHeader('Accept', 'application/json')
213227
xhr.onload = () => {
228+
console.debug(`request ${method} ${url} > ${xhr.status}`, data, xhr)
214229
try {
215230
if (xhr.status !== (expected_status || 200)) {
216231
throw Error(`bad response status ${xhr.status} not 200`)
@@ -325,12 +340,41 @@ ${xhr.responseText}`)
325340
ga_event (category, action, label) {
326341
/* istanbul ignore next */
327342
for (let prefix of ga_prefixes) {
343+
console.debug('ga sending event', prefix, category, action, label)
328344
window.ga(prefix + 'send', 'event', category, action, label)
329345
}
330346
},
347+
grecaptcha_callback (response) {
348+
Vue.set(this.enquiry_data, 'grecaptcha_response', response)
349+
},
350+
render_grecaptcha () {
351+
const el = document.getElementById(this.grecaptcha_container_id)
352+
if (el && el.childElementCount === 0) {
353+
console.debug('rendering grecaptcha')
354+
window.grecaptcha.render(this.grecaptcha_container_id, {
355+
sitekey: this.grecaptcha_key,
356+
callback: this.grecaptcha_callback
357+
})
358+
} else {
359+
console.debug('not rendering grecaptcha', el)
360+
}
361+
},
362+
331363
goto (name, params) {
332364
this.$router.push({'name': name, params: params})
333365
}
334366
}
335367
})
368+
if (window.socket_view === undefined) {
369+
window.socket_view = [v]
370+
} else {
371+
window.socket_view.push(v)
372+
}
373+
374+
window._tcs_grecaptcha_loaded = () => {
375+
for (let v of window.socket_view) {
376+
v.render_grecaptcha()
377+
}
378+
}
379+
return v
336380
}

test/unit/specs/_shared.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function generate_vm (router, vm_data_) {
100100
callback()
101101
},
102102
ga_event () { this.__record_call('ga_event') },
103+
grecaptcha_callback (r) { this.__record_call('grecaptcha_callback', r) },
103104
get_selected_subject () {
104105
this.__record_call('get_selected_subject')
105106
return null

0 commit comments

Comments
 (0)