/
actions.js
108 lines (98 loc) · 3.8 KB
/
actions.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* globals $:false */
let posting = false;
let handleResultError = function(title, message, btnText) {
document.querySelector('#mm-alertbox .mm-alertbox-title').textContent = title || 'Error';
document.querySelector('#mm-alertbox .mm-alertbox-body').textContent = message;
document.querySelector('#mm-alertbox .mm-alertbox-btn').textContent = btnText || 'Close';
$('#mm-alertbox').modal('show');
};
let handleResultSuccess = function(title, message, btnText) {
document.querySelector('#mm-alertbox .mm-alertbox-title').textContent = title || 'Success';
document.querySelector('#mm-alertbox .mm-alertbox-body').textContent = message;
document.querySelector('#mm-alertbox .mm-alertbox-btn').textContent = btnText || 'Close';
$('#mm-alertbox').modal('show');
};
let actions = {
resendValidation: function() {
if (posting) {
return;
}
posting = true;
let form = {
_csrf: document.getElementById('_csrf').value
};
fetch('/account/settings/api/resend-validation', {
method: 'post',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify(form)
})
.then(function(response) {
return response.json();
})
.then(function(result) {
posting = false;
if (!result.success) {
return handleResultError(result.error);
}
handleResultSuccess('Email sent', 'Validation email sent to ' + result.email);
})
.catch(function(err) {
posting = false;
console.error(err);
handleResultError(err.name || 'Error', 'Failed to post data to server: ' + (err.message || '').replace(/^\w*Error:\s*/, ''));
});
},
siteUpgrade: function() {
if (posting) {
return;
}
posting = true;
let form = {
_csrf: document.getElementById('_csrf').value
};
fetch('/account/settings/site/api/upgrade', {
method: 'post',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify(form)
})
.then(function(response) {
return response.json();
})
.then(function(result) {
posting = false;
if (!result.success) {
return handleResultError(result.error);
}
handleResultSuccess('Upgrade', 'Upgrade process started. This may take some time.');
document.getElementById('mm-upgrade-info').classList.remove('mm-hidden');
document.getElementById('mm-upgrade-box').classList.add('mm-hidden');
})
.catch(function(err) {
posting = false;
console.error(err);
handleResultError(err.name || 'Error', 'Failed to post data to server: ' + (err.message || '').replace(/^\w*Error:\s*/, ''));
});
}
};
let actionElms = document.querySelectorAll('.mm-action');
for (let i = 0, len = actionElms.length; i < len; i++) {
let actionElm = actionElms[i];
if (actionElm.dataset.mmEvent && actionElm.dataset.mmAction && typeof actions[actionElm.dataset.mmAction] === 'function') {
actionElm.addEventListener(
actionElm.dataset.mmEvent,
function(e) {
e.preventDefault();
actions[actionElm.dataset.mmAction](e);
},
false
);
}
}