Repository navigation
Expand file tree
/
Copy pathSiteCode.js
More file actions
100 lines (87 loc) · 2.6 KB
/
Copy pathSiteCode.js
File metadata and controls
100 lines (87 loc) · 2.6 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
94
95
96
97
98
99
100
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixCRM from 'wix-crm';
import {local} from 'wix-storage';
import wixWindow from 'wix-window';
$w.onReady( () => {
// CHANGE LOGIN/LOGOUT BUTTON AND SHOW/HIDE MY PROFILE BUTTON AND MEMBERS FORUM
if(wixUsers.currentUser.loggedIn) {
$w("#button15").label = "LOGOUT";
$w("#button16").show();
$w("#button20").show();
}
else {
$w("#button15").label = "LOGIN";
$w("#button16").hide();
$w("#button20").hide();
//$w("#stripArea").collapse();
}
} );
// ON CLICK FUNCTION OF LOGIN/LOGOUT
export async function button15_onclick(event, $w) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
await wixLocation.to('/');
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#button15").label = "LOGIN";
$w("#button16").hide();
$w("#button20").hide();
} );
}
// user is logged out
else {
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
// SET DEFAULT VALUES FOR NEW MEMBERS
const toInsert = {
"_id": userId,
"email": userEmail,
"firstName": "New Forge",
"surname": "Member"
};
// add the item to the collection and send the new member default email created
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();
$w("#button20").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button16_onclick() {
// CHANGE TO MY PROFILE PAGE WHEN CLICKED
wixLocation.to(`/MemberProfile/${wixUsers.currentUser.id}`);
}
export function button20_onclick(event) {
// CHANGE TO MEMBERS FORUM PAGE WHEN CLICKED
wixLocation.to(`/MemberProfile/forum/${wixUsers.currentUser.id}`);
}