-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailchimp.vue
More file actions
93 lines (84 loc) · 2.44 KB
/
Mailchimp.vue
File metadata and controls
93 lines (84 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template>
<div class="mailchimp">
<form class="mailchimp-subscribe" @submit.prevent="subscribe">
<input v-model="email" type="email" name="EMAIL" value="" placeholder="YOUR EMAIL ADDRESS..." required>
<input class="button" type="submit" value="Notify Me" :disabled="in_progress">
</form>
<div v-if="response" :class="['status', response_status]">{{ response }}</div>
</div>
</template>
<script>
import $ from 'jquery';
const qs = require('qs');
export default {
props: {
action: '',
validation: '',
},
data: () => ({
email: '',
response: '',
response_status: '',
in_progress: false,
}),
methods: {
subscribe: function (event) {
this.in_progress = true;
this.response = '';
this.response_status = '';
try{
$.ajax({
url: this.getTransformedAction(),
crossDomain: true,
data: this.prepareRequestData(),
method: "GET",
cache: false,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: (data) => {
// enable submission again
this.in_progress = false;
// --- Success --
if (data.result === "success") {
this.email = '';
this.response = this.adjustResponse(data.msg);
this.response_status = 'success';
// --- Error ---
} else {
// Error from Mail Chimp
this.response = this.adjustResponse(data.msg);
this.response_status = 'error';
}
}
});
// request error
} catch(err){
console.log(err);
this.in_progress = false;
this.response = this.adjustResponse(err);
this.response_status = 'error';
}
},
// add few necessary params to default form action
getTransformedAction() {
var action = this.action.replace('/post?', '/post-json?') + "&c=?";
return action;
},
// prepare data to pass to request
prepareRequestData() {
let data = {
'EMAIL': this.email,
'subscribe': 'Subscribe',
};
data[this.validation] = '';
return qs.stringify( data );
},
// adjust response from mailchimp
adjustResponse(text) {
return text
.replace(/<(?:.|\n)*?>/gm, '') // strip html
.replace('Click here to update your profile', ''); // remove update profile link
}
}
}
</script>