User Role filter and Search

Question:
Hi everyone.
I’m trying to get my head around an issue I’m having. I have a page with a repeater (“repeater1”) linked to “dataset2” which is filtered by the role assigned to the current logged on user. I also want to be able to search to narrow down results and only display content the user role has been assigned to speed up the process. I believe I require additional code for the “function filter” which re-instates user role filter

Product:
Editor X.

What have you already tried:

Current code is:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

let user = wixUsers.currentUser;

$w.onReady(function () {
$w(‘#stripLoading’).expand();
$w(‘#stripSitelist’).collapse();
if (user.loggedIn) {
user.getRoles()
.then(r => {
if (r.length > 0) {
let roleValues = [
{role: “Admin”, value: “Admin” },
{ role: “User1”, value: “SiteName1” },
{ role: “User2”, value: “SiteName2” },
]
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);
//roleFilter is database title
filter = filter.eq(“roleFilter”, relevantValues[0]);
relevantValues.shift();
if (relevantValues.length > 0) {
relevantValues.forEach(e => {
filter = filter.or(wixData.filter().eq(“roleFilter”, e));
})
}
$w(“#dataset2”).setFilter(filter).then(() => {
console.log(“Dataset is filtered”);
ordersView();
})
} else {
noOrdersView();
}
})
}
})

$w.onReady(function (){

});

let debounceTimer;
export function search_keyPress(event) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w(‘#search’).value);
}, 200);
}

function ordersView() {//function if user has roles
$w(‘#stripSitelist’).expand();
$w(‘#stripLoading’).collapse();
}
function noOrdersView() {//function if user has NO roles
$w(‘#stripLoading’).collapse();
$w(‘#stripNoSites’).expand();
}

function filter(title) {//function for search
$w(‘#dataset2’).setFilter(wixData.filter()
.contains(‘title’, $w(‘#search’).value)
)
}

Additional information:
Whilst this code does work, if you remove the text from the input it displays all the results and is no longer filtered by the user role.

Any help would be much appreciated

is there anyone able to assist with this issue in any way?

The user role filter is removed if the search input box (#search) is emptied of characters. This should be if the search input =0 characters then rerun the role filter

Hi!! In many Wix Data searches, when the search condition is an empty string (“”), it may match all items. Therefore, it’s important to avoid executing the search when the search string has a length of zero, or in other words, only perform the search process when the search string’s length is greater than zero.