Skip to content

Commit 66ac912

Browse files
committed
tweak grecaptcha
1 parent 11e9e5d commit 66ac912

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@
2626
</main>
2727
</body>
2828
<script>
29-
socket('f8d88b6562b3434ba4ef', {
29+
var public_key = 'f8d88b6562b3434ba4ef'
30+
var api_root = null // 'http://localhost:8000'
31+
socket(public_key, {
3032
element: '#socket1',
33+
api_root: api_root,
3134
})
32-
socket('f8d88b6562b3434ba4ef', {
35+
socket(public_key, {
3336
element: '#socket2',
3437
mode: 'enquiry',
38+
api_root: api_root,
3539
})
36-
socket('f8d88b6562b3434ba4ef', {
40+
socket(public_key, {
3741
element: '#socket3',
3842
mode: 'enquiry-modal',
43+
api_root: api_root,
3944
})
4045
</script>
4146
</html>

src/components/enquiry.vue

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ export default {
6969
}),
7070
methods: {
7171
submit: function () {
72-
let grecaptcha_response = window.grecaptcha ? window.grecaptcha.getResponse(this.grecaptcha_id) : '-'
73-
if (grecaptcha_response === '') {
72+
if (window.grecaptcha && !this.$root.enquiry_data.grecaptcha_response) {
7473
this.grecaptcha_missing = true
7574
return
7675
}
77-
this.$set(this.$root.enquiry_data, 'grecaptcha_response', grecaptcha_response)
76+
7877
if (this.contractor !== null) {
7978
this.$set(this.$root.enquiry_data, 'contractor', this.contractor.id)
8079
}
@@ -83,27 +82,36 @@ export default {
8382
},
8483
submission_complete: function () {
8584
this.submitted = true
86-
}
87-
},
88-
created: function () {
85+
},
8986
/* istanbul ignore next */
90-
if (this.$root.grecaptcha_key !== null) {
91-
let render_grecaptcha = () => {
87+
prepare_grecaptcha: function () {
88+
const grecaptcha_callback = (response) => this.$set(this.$root.enquiry_data, 'grecaptcha_response', response)
89+
90+
if (this.$root.grecaptcha_key === null) {
91+
grecaptcha_callback('-')
92+
return
93+
}
94+
95+
const render_grecaptcha = () => {
9296
this.grecaptcha_id = window.grecaptcha.render(this.grecaptcha_container_id, {
93-
'sitekey': this.$root.grecaptcha_key,
97+
sitekey: this.$root.grecaptcha_key,
98+
callback: grecaptcha_callback
9499
})
95100
}
96101
if (window.grecaptcha === undefined) {
97102
window._grecaptcha_loaded = render_grecaptcha
98103
add_script('https://www.google.com/recaptcha/api.js?onload=_grecaptcha_loaded&render=explicit')
99104
} else {
100-
setTimeout(render_grecaptcha, 50)
105+
render_grecaptcha()
101106
}
102107
}
108+
},
109+
created: function () {
103110
this.$root.get_enquiry()
104111
if (this.mode !== 'vanilla') {
105112
this.$root.ga_event('enquiry-form', 'loaded', this.mode)
106113
}
114+
setTimeout(this.prepare_grecaptcha, 50)
107115
},
108116
}
109117
</script>

src/main.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ module.exports = function (public_key, config) {
9797
})
9898

9999
let error = null
100-
if (config.mode === undefined) {
100+
if (!config.mode) {
101101
config.mode = 'grid'
102102
} else if (MODES.indexOf(config.mode) === -1) {
103103
error = `invalid mode "${config.mode}", options are: ${MODES.join(', ')}`
104104
config.mode = 'grid'
105105
}
106106

107-
if (config.api_root === undefined) {
107+
if (!config.api_root) {
108108
config.api_root = process.env.SOCKET_API_URL
109109
}
110110

111-
if (config.url_root === undefined) {
111+
if (!config.url_root) {
112112
config.url_root = 'auto'
113113
} else if (config.url_root !== 'auto' && config.url_root[0] !== '/') {
114114
config.url_root = '/'
@@ -119,18 +119,18 @@ module.exports = function (public_key, config) {
119119
config.url_root = auto_url_root(window.location.pathname)
120120
}
121121

122-
if (config.router_mode === undefined) {
122+
if (!config.router_mode) {
123123
config.router_mode = 'hash'
124124
} else if (ROUTER_MODES.indexOf(config.router_mode) === -1) {
125125
error = `invalid router mode "${config.router_mode}", options are: ${ROUTER_MODES.join(', ')}`
126126
config.router_mode = 'hash'
127127
}
128128

129-
if (config.console === undefined) {
129+
if (!config.console) {
130130
config.console = console
131131
}
132132

133-
if (config.element === undefined) {
133+
if (!config.element) {
134134
config.element = '#socket'
135135
}
136136

@@ -140,7 +140,7 @@ module.exports = function (public_key, config) {
140140
}
141141

142142
for (let k of Object.keys(STRINGS)) {
143-
if (config[k] === undefined) {
143+
if (!config[k]) {
144144
config[k] = STRINGS[k]
145145
}
146146
}

0 commit comments

Comments
 (0)