I want to assign a variable to
privacyStatus which I understand to be in
wix-members api → currentMember
What i want to do is create a router page which will check the currently logged in member to see if their privacyStatus is “private” or “public”
If private, then redirect to a page for private status
if public, then redirect to a page for public status
This is what i have currently and I am stuck. I am editing the site in velo/ developer mode.
import { currentMember } from ‘wix-members’ ;
import wixLocation from ‘wix-location’ ;
$w . onReady ( function () {
// Write your JavaScript here
currentMember . getMember ()
. then (( member ) => {
let privacy = member . privacyStatus
})
// To select an element by ID use: $w(‘#elementID’)
// Click 'Preview' to run your code
});
I want public members to access the forum pages and private members to be sent to a page saying “you profile is private, please change it to continue to the forum”
any help would be appreciated. If anyone could give me some code, I can use it as a learning tool.
Heshan
On one hand you said you wanted to have a ROUTER page, on the other hand from the code you posted it looks like you wanted to load the current page and then navigate to the other pages with wixLocation.to .
So what would you like to do?
Thanks for replying. Maybe i havent understood router pages yet. I assumed that the code on the router page would be written as above to redirect . The process i want to happen is this…
Please note, you can only access the forum as a member with a subscription plan paid for. If you are not on a plan, you will be redirected to login and pay for one if you click forum
member / subscribed person logs in → wants to go to the forum → clicks forum → somehow, the site checks the profilestatus. -->if the profile is “public” then on to the forum as normal
If the profile is “private”…
At the moment, wix will take the client to a page which states that they need to subscribe to access this page. But clearly, the client has subsribed - its only that they havent made their profile public. I have asked wix and this page cannot be edited (i am told)
Therefore, if the profilestatis is “private” i want the client to be redirected to a page which tells them that they need to be public and how to do so. They can then make their profile public and go to the forum.
I hope that makes things clearer.
If router pages are the wrong thing to do, i would like to know the best way to do this.
Thanks in advance.
heshan
Router page and wixLocation.to are two different things.
For router page, you have a function running also on the backend, if there’s a redirection there, the current page will elements will never display.
On the other hand, if you go for the wixLocation.to, it loads the current page first and then navigates to the target page (you may see the element of the current page for a second or few. It also may be slower),
Now. since you started with the latter, all you need to add is:
Re: URL error - is this to do with the wixLocation.to line - where i have written /about and /forum. Do these paths to the pages need to be more complete?
Can i start by saying a big thanks for continuing to help. It has been reassuring.
At the moment, i am testing the code by hitting preview after editing code in the editor itself. The website appears to have a login with a member but i will check again.
I am new to the syntax for velo code, so you may have to be as explicit as you can.
the {fieldsets:[‘FULL’]} is just to be safe (it is the default despite the documentation).
The question mark ?. tells the system that if there is no member at all it shouldn’t throw an error for trying to access a property of undefined member, but it should set the privacy variable to be undefined (which is falsy).
I am a little bit further
Using preview made the page change to forum (as i was logged in on the preview i thought it was ok). An error about invalid url came up
I then made a dummy account with a private profile
Now i find that whether i login as a public or a private profile, the second of the urls is used always i.e.
for my benefit and learning - this is how i understand the code to be working.
import { currentMember } from ‘wix-members’ ; import wixLocation from ‘wix-location’ ;
// these lines are for interrogating the api functions in wix for members and locations.
$w . onReady ( function (){
// this is the “start code once page loads fully”
currentMember . getMember ({ fieldsets :[ ‘FULL’ ]})
. then (( member )=>{
// these lines mean - get member details from the FULL fieldsets and assign to the variable member
const privacy = member ?. privacyStatus ;
// assign the privacyStatus of the member variable (created above) to the variable privacy. The question mark is to do with querying the array or data.
// i have added these - send an output to the console - the member variable, privacy variable and the string “hi there”
wixLocation . to ( privacy ? ‘/becoming-a-member’ : ‘/about’ );})});
// use the wixLocation api (interrogated at the start of this) based onthe ‘?’ interrogation of the privacy variable. I presume that ACTIVE would be the first url location sent to (currently set at the /becoming-a-member page). Followed by a ‘:’ colon. PRIVATE would be the second url to be sent to (currently /about page)
just to add, if i wanted to open an lightbox if the privacyStatus is private then how would i combine the privacy check with both wixlocation.to (send to the forum webpage) or send to a lightbox (give a message to make profile active and send to the account page or settings page)