forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountManager.js
81 lines (71 loc) · 2.24 KB
/
AccountManager.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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import { abortableGet, put, post, del } from 'lib/AJAX';
import { unescape } from 'lib/StringEscaping';
let currentUser = null;
let xhrMap = {};
let AccountManager = {
init() {
let accountData = document.getElementById('accountData');
if (!accountData) {
return;
}
currentUser = JSON.parse(unescape(accountData.innerHTML));
},
currentUser() {
if (!currentUser) {
AccountManager.init();
}
return currentUser || {};
},
resetPasswordAndEmailAndName(currentPassword, newPassword, newEmail, newName) {
let path = '/account';
return put(path, {
confirm_password: currentPassword,
'user[password]':newPassword,
'user[email]': newEmail,
'user[name]': newName,
});
},
createAccountKey(keyName) {
let path = '/account/keys';
let promise = post(path, {name: keyName});
promise.then(newKey => {
let hiddenKey = {...newKey, token: '\u2022\u2022' + newKey.token.substr(newKey.token.length - 4)};
//TODO: save the account key better. This currently only works because everywhere that uses
// the account keys happens to rerender after the account keys change anyway.
currentUser.account_keys.unshift(hiddenKey);
});
return promise;
},
deleteAccountKeyById(id) {
let path = '/account/keys/' + id.toString();
let promise = del(path);
promise.then(() => {
//TODO: delete the account key better. This currently only works because everywhere that uses
// the account keys happens to rerender after the account keys change anyway.
currentUser.account_keys = currentUser.account_keys.filter(key => key.id != id);
});
return promise;
},
fetchLinkedAccounts(xhrKey) {
let path = '/account/linked_accounts';
let {xhr, promise} = abortableGet(path);
xhrMap[xhrKey] = xhr;
promise.then((result) => {
this.linkedAccounts = result;
});
return promise;
},
abortFetch(xhrKey) {
if (xhrMap[xhrKey]) {
xhrMap[xhrKey].abort();
}
},
};
module.exports = AccountManager;