Skip to content

Commit 6c0bfab

Browse files
committed
adding recaptcha
1 parent 1328e5c commit 6c0bfab

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
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.8",
3+
"version": "0.0.9",
44
"description": "TutorCruncher socket",
55
"author": "Samuel Colvin <[email protected]>",
66
"private": false,

src/components/con-modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<div class="tcs-scroll">
3333
<transition name="tcs-squeeze" mode="out-in">
34-
<enquiry v-if="show_enquiry" :contractor="contractor"></enquiry>
34+
<enquiry v-if="show_enquiry" :contractor="contractor" :ismodal="true"></enquiry>
3535
<con-details v-else :contractor="contractor"></con-details>
3636
</transition>
3737
</div>

src/components/enquiry-modal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<modal title="Enquiry">
3-
<enquiry></enquiry>
3+
<enquiry :ismodal="true"></enquiry>
44
</modal>
55
</template>
66

src/components/enquiry.vue

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<div class="tcs-enquiry">
3-
<div class="tcs-submitted" v-if="submitted" v-html="$root.get_text('enquiry_submitted_thanks', {}, true)"></div>
3+
<div class="tcs-submitted" v-if="submitted">
4+
<div v-if="ismodal" v-html="$root.get_text('enquiry_modal_submitted_thanks', {}, true)"></div>
5+
<div v-else v-html="$root.get_text('enquiry_submitted_thanks', {}, true)"></div>
6+
</div>
47
<div v-else>
58
<div v-if="contractor" class="tcs-centre" v-html="$root.get_text('contractor_enquiry_message', {contractor_name: contractor.name}, true)">
69
</div>
@@ -13,6 +16,12 @@
1316
<div v-for="field in attribute_fields">
1417
<tcs-input :field="field" prefix="attributes"></tcs-input>
1518
</div>
19+
20+
<div :id="grecaptcha_container_id" class="grecaptcha"></div>
21+
<div v-if="grecaptcha_missing" class="error-msg">
22+
{{ $root.get_text('grecaptcha_missing') }}
23+
</div>
24+
1625
<div class="tcs-field tcs-submit">
1726
<button type="submit">
1827
{{ $root.config.submit_enquiry }}
@@ -25,13 +34,18 @@
2534

2635
<script>
2736
import input from './input.vue'
37+
import {add_script} from '../utils'
2838
2939
export default {
3040
props: {
3141
contractor: {
3242
type: Object,
3343
default: null,
3444
},
45+
ismodal: {
46+
type: Boolean,
47+
default: false,
48+
},
3549
},
3650
components: {
3751
'tcs-input': input
@@ -45,10 +59,19 @@ export default {
4559
}
4660
},
4761
data: () => ({
48-
submitted: false
62+
submitted: false,
63+
grecaptcha_missing: false,
64+
grecaptcha_container_id: 'grecaptcha_' + Math.random().toString(36).substring(2, 10),
65+
grecaptcha_id: null,
4966
}),
5067
methods: {
5168
submit: function () {
69+
let grecaptcha_response = window.grecaptcha ? window.grecaptcha.getResponse(this.grecaptcha_id) : '-'
70+
if (grecaptcha_response === '') {
71+
this.grecaptcha_missing = true
72+
return
73+
}
74+
this.$set(this.$root.enquiry_data, 'grecaptcha_response', grecaptcha_response)
5275
if (this.contractor !== null) {
5376
this.$set(this.$root.enquiry_data, 'contractor', this.contractor.id)
5477
}
@@ -59,6 +82,20 @@ export default {
5982
}
6083
},
6184
created: function () {
85+
/* istanbul ignore next */
86+
if (this.$root.grecaptcha_key !== null) {
87+
let render_grecaptcha = () => {
88+
this.grecaptcha_id = window.grecaptcha.render(this.grecaptcha_container_id, {
89+
'sitekey': this.$root.grecaptcha_key,
90+
})
91+
}
92+
if (window.grecaptcha === undefined) {
93+
window._grecaptcha_loaded = render_grecaptcha
94+
add_script('https://www.google.com/recaptcha/api.js?onload=_grecaptcha_loaded&render=explicit')
95+
} else {
96+
setTimeout(render_grecaptcha, 50)
97+
}
98+
}
6299
this.$root.get_enquiry()
63100
},
64101
}
@@ -84,11 +121,19 @@ form.tcs {
84121
}
85122
86123
.tcs-submit {
87-
text-align: right;
124+
text-align: center;
88125
button {
89126
@extend .tcs-button;
90127
font-size: 17px;
91128
padding: 10px 12px;
92129
}
93130
}
131+
132+
.grecaptcha > div {
133+
margin: 0 auto;
134+
}
135+
.error-msg {
136+
color: darkred;
137+
text-align: center;
138+
}
94139
</style>

src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ const ConfiguredVueRouter = config => {
6868
})
6969
}
7070

71+
// TODO these need a consist prefix
7172
const STRINGS = {
7273
skills_label: 'Skills',
7374
contractor_enquiry_message: 'Please enter your details below to enquire about tutoring with {contractor_name}.',
7475
enquiry_message: 'Please enter your details below and we will get in touch with you shortly.',
7576
contractor_enquiry_button: 'Contact {contractor_name}',
7677
contractor_details_button: 'Show Profile',
7778
submit_enquiry: 'Submit Enquiry',
78-
enquiry_submitted_thanks: 'Enquiry submitted, thank you.\n\nYou can now close this window.',
79+
enquiry_submitted_thanks: 'Enquiry submitted, thank you.',
80+
enquiry_modal_submitted_thanks: 'Enquiry submitted, thank you.\n\nYou can now close this window.',
7981
enquiry_button: 'Get in touch',
82+
grecaptcha_missing: 'This captcha is required',
8083
}
8184

8285
const MODES = ['grid', 'enquiry', 'enquiry-modal']
@@ -139,6 +142,7 @@ module.exports = function (public_key, config) {
139142
router: ConfiguredVueRouter(config),
140143
render: h => h(app),
141144
data: {
145+
grecaptcha_key: process.env.NODE_ENV === 'testing' ? null : '6LdyXRgUAAAAADUNhMVKJDXiRr6DUN8TGOgllqbt',
142146
contractors: [],
143147
contractors_extra: {},
144148
config: config,

src/utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ const auto_url_root = (path) => {
3434
return path
3535
}
3636

37-
export {to_markdown, clean, auto_url_root}
37+
const add_script = (url) => {
38+
let s = document.createElement('script')
39+
s.src = url
40+
document.body.appendChild(s)
41+
}
42+
43+
export {to_markdown, clean, auto_url_root, add_script}

test/unit/specs/_shared.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const enquiry_options = {
3636
const vm_data = () => ({
3737
contractors: [{name: 'Fred Bloggs', link: '123-fred-bloggs', tag_line: 'hello'}],
3838
config: {},
39+
grecaptcha_key: null,
3940
contractors_extra: {'123-fred-bloggs': {'extra_attributes': [{'name': 'Bio', 'value': 'I am great'}]}},
4041
enquiry_form_info: enquiry_options,
4142
enquiry_data: {},

0 commit comments

Comments
 (0)