-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsefulLinksPageCode.js
91 lines (71 loc) · 2.27 KB
/
UsefulLinksPageCode.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
//-------------Imports-------------//
import {local} from 'wix-storage';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixUsers from 'wix-users';
import wixCRM from 'wix-crm';
//-------------Global Variables-------------//
// Current user.
let user = wixUsers.currentUser;
$w.onReady(async function () {
//-------------Page Setup-------------//
// Set the action that occurs when the login message is clicked to be the loginMessageClick() function.
$w('#loginMessage').onClick(loginMessageClick);
});
//~~~~~~~~~~~~~~~~~~~~~~ USEFUL LINK SUGGESTION STUFF ~~~~~~~~~~~~~~~~~~~~`
//-------------Event Handlers-------------//
export function suggestlink_onclick(event, $w) {
if (user.loggedIn){
wixWindow.openLightbox("Useful Links Suggestions");
}
else {
$w('#loginMessage').show();
}
}
// Set the action that occurs when the login message is clicked.
async function loginMessageClick() {
// Set the login options.
let options = {"mode": "login"};
// Hide the login message.
$w('#loginMessage').hide();
// Prompt the user to login using the options created above.
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("MemberProfile")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
// add the item to the collection
wixData.insert("MemberProfile", toInsert)
.catch( (err) => {
console.log(err);
} );
wixCRM.emailContact('testEmail', wixUsers.currentUser.id);
}
// update buttons accordingly
$w("#button15").label = "LOGOUT";
$w("#button16").show();
wixLocation.to(wixLocation.url);
} )
.catch( (err) => {
console.log(err);
} );
}