Hi Guys,
Currently I use the following code:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#button12").label = "Logout";
$w("#button14").show();
}
else {
$w("#button12").label = "Login";
$w("#button14").hide();
}
} );
export function button12_click() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#button12").label = "Login";
$w("#button14").hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
wixLocation.to('*the member login page link will be stated here*);
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("Free1")
.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("Free1", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#button12").label = "Logout";
$w("#button14").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button14_click() {
wixLocation.to(`/Free1/${wixUsers.currentUser.id}`);
}
button 14 will direct users to a form they can fill in. Afterwards, their own personal page will be created and a reference field 'Propertytitle (ID)' is filled in the database 'free1' which indicate a personal page is created. However, I would like to add another button (button 15) where visitors can view this personal page.
In practical, with button 14 they can make changes afterwards in the form and with button 15 they can view the result of these changes on their personal page.
I would like to only show button15 if user have filled in the form. Therefore I think I should check if the field 'Propertytitle (ID) is empty or not.
I think I should use something like the following code: ( Check the userId and check whether Propertytitle (ID) field is empty or not)
let userId; wixData.query("Free1") .eq("_id", userId) .eq("Free 1 Propertytitle _id", " ") .find() .then( (results) => { // if an item for the user is found if(results.items.length > 0) { $w("#button15").show(); } else { $w('#button15').hide(); } })
export function button15_click() { wixLocation.to(`/Free1/Propertytitle/${wixUsers.currentUser.id}`); }
First, the code not work yet, does somebody know how to setup the code? Secondly, I would like to know how to insert the specific code into the other script above!
Thanks for any help.
Kind regards,
Vincent
bump. Could somebody help please