I’ve been stumped for over a week now. So far with the codes I’ve tried I haven’t had the results I’m looking for. I’m new to all this coding, I’m good at reading and understanding the code and how to edit and work with it. Writing it from scratch though is an entirely different animal I have not yet mastered. So here is my situation explained as best as I can, also I can provide any other information if need be.
-
I have a dynamic member page setup called Members(ID) with the dataset #dynamicdataset attached, which is where it identifies unique users and displays user specific content
-
On this dynamic member page I have 7 different options (System 1, 2, 3, 4,5, 6, and 7) for clients to subscribe too. For this example I will only refer to System 1
-
Within the #dynamicdataset I have a boolean field called “System 1 Subscription” fieldkey= #system1 that becomes true if the current logged in user checks #switch1 and then clicks on #button23 to submit the boolean result into #dynamicdatabase
-
So if a new client wants to join “System 1” this is the process I’m trying to create…
They click on #button25 which opens up a lightbox asking for confirmation to subscribe to “System 1”. On this light box there is #switch1 which the client would switch to true if the client wants to join “System 1” and then click on #button23 to confirm the #switch1 results and submit the data to the #dynamicdatabase collection
What I need to do now is check to see if the logged in user has the boolean field #system1 true or not. And if it is true i need to change the text in #text30 to “Subscribed”, else “Join Now”
- #text30 is the text I’m using to display over #button25
Any help is very much appreciated, the code below is the code I have on my landing/login page. I’d imagine the code I’m looking for is similiar but everytime I work with it and try to edit it I can’t get it exactly right. Thanks in advance!!
Do I need to add an and right after the if(wixUsers.currentUser.loggedIn) to reference the boolean field #system1?
~~~~~~~
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#button22”).label = “Logout”;
$w(“#button23”).show();
}
else {
$w(“#button22”).label = “Login”;
$w(“#button23”).hide();
}
} );
export function button22_click(event, $w) {
// user is logged in
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#button22”).label = “Login”;
$w(“#button23”).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(“Members”)
.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(“Members”, toInsert)
. catch ( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w(“#button22”).label = “Logout”;
$w(“#button23”).show();
} )
. catch ( (err) => {
console.log(err);
} );
}
}
export function button23_click(event, $w) {
wixLocation.to(/Members/${wixUsers.currentUser.id}
);
}