[SOLVED] Filter repeater based on Member Role

For anyone trying to accomplish the same thing, here’s my setup.

Shoutout to @jonatandor35 , a true Corvid Master, for the amazing help figuring out the code. Obviously tweak it to fit your setup.

• Three strips, all collapsed on load. One strip (“stripLoading”) says “loading” and loads immediately, one strip (“repeaterStrip”) contains the repeater and expands when the filter is finished, one strip (“stripNoOrders”) says “No Orders” and expands if the user has no role.

• Database with your fields, then add a field to filter by. Mine is “roleFilter” and I have tag values that correspond to each of my Roles. (With “admin” in all, so Admin get access to everything)

• Page code:

import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
import wixData from 'wix-data';

let user = wixUsers.currentUser;

$w.onReady(function () {
    $w('#stripLoading').expand();
    $w('#repeaterStrip').collapse(); 
 //collapse repeater until filter is complete
 if (user.loggedIn) {
        user.getRoles()
            .then(r => {
 if (r.length > 0) {
 let roleValues = [ 
 // role = role name, value = value in database field
          {role: "Role Name 1", value: "expTB" },
 // Change according to your Role names, and database field values
          { role: "Role Name 2", value: "expMM" },
          { role: "Role Name 3", value: "expSS" },
          { role: "Admin", value: "admin" },
          ] //etc, for as many roles as you have
                    console.log("roles defined");

 let filter = wixData.filter();
 let roles = r.map(e => e.name);
                    roles = roles.filter(e => roleValues.some(i => i.role === e));
                    console.log("roles", roles);
 let relevantValues = roles.map(e => roleValues.find(i => i.role === e).value);
                    console.log("relevant values", relevantValues);
                    filter = filter.eq("roleFilter", relevantValues[0]); 
 //"roleFilter" is the database field I'm filtering by.
                    relevantValues.shift();
 if (relevantValues.length > 0) {
                        relevantValues.forEach(e => {
                            filter = filter.or(wixData.filter().eq("roleFilter", e));
                        })
                    }
                    $w("#ocDataset").setFilter(filter).then(() => {
                console.log("Dataset is filtered");
                ordersView(); //function to show repeater
                    })
                } else {
                    noOrdersView(); 
                    //function if user has no roles
                }
            })
    }
})

function ordersView() {//function if user has roles
    $w('#repeaterStrip').expand();
    $w('#stripLoading').collapse();
}
function noOrdersView() {//function if user has NO roles
    $w('#stripLoading').collapse();
    $w('#stripNoOrders').expand();
}