-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.js
executable file
·77 lines (67 loc) · 2.27 KB
/
email.js
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
$(document).ready(function () {
var autocompleteListTemplate = _.template('<div class="list-entry" data-value="<%= user.email %>"><%= user.firstName %> <%= user.lastName %> (<%= user.email %>)</div>');
// Autocomplete
$('[name=to]').autocomplete({
url: 'https://trunkclub-ui-takehome.now.sh/search',
listSelector: '.autocomplete-list',
entrySelector: '.list-entry',
entryTemplate: autocompleteListTemplate
});
$('[name=cc]').autocomplete({
url: 'https://trunkclub-ui-takehome.now.sh/search',
listSelector: '.autocomplete-list',
entrySelector: '.list-entry',
entryTemplate: autocompleteListTemplate,
allowMultipleSelect: true,
onBeforeSearch: function (input) {
var emails = input.split(',');
return emails[emails.length - 1];
},
onAfterSelect: function (input, entry) {
var currentEntries = input.val().split(',').slice(0, -1);
currentEntries.push(entry.attr('data-value'));
input.val(currentEntries.join(','));
}
});
// Send email
$('.sendEmail').on('click', function (event) {
event.preventDefault();
$('.error').remove();
$('strong').remove();
var message = {
to: $('[name=to]').val(),
cc: JSON.stringify($('[name=cc]').val().split(',')),
subject: $('[name=subject]').val(),
body: $('[name=body]').val()
};
if (message.cc == '[""]') {
delete message.cc;
}
if (!message.to) {
$('[name=to]').after('<span class="error">"To" is required</span>');
}
if (!message.subject) {
$('[name=subject]').after('<span class="error">"Subject" is required</span>');
}
if (!message.body) {
$('[name=body]').after('<span class="error">"Body" is required</span>');
}
if ($('.error').length) {
return;
}
$.ajax({
url: 'https://trunkclub-ui-takehome.now.sh/submit',
method: 'POST',
data: message,
success: function (data) {
$('[name=to], [name=cc], [name=subject], [name=body]').val('');
$('body').append('<strong>Success! Your email has been sent.</strong>');
},
error: function (error) {
if (error.status === 500) {
$('body').append('<strong>An error occurred. Please try sending your email again.</strong>');
}
}
});
});
});