docbrown/app/javascript/shared/components/UserStore.js
ktmouk 8963e185f8
Fix username combobox tries to load all users (#20227)
* Fix username combobox tries to load all users

* Add the limit param to the users_query

* Add a comment
2023-10-25 15:53:53 -04:00

42 lines
966 B
JavaScript

export class UserStore {
constructor(users = []) {
this.users = users;
}
static async fetch(url) {
try {
const res = await window.fetch(url);
return new UserStore(await res.json());
} catch (error) {
Honeybadger.notify(error);
}
}
matchingIds(arrayOfIds) {
const allUsers = this.users;
const someUsers = arrayOfIds.reduce((array, idString) => {
const aUser = allUsers.find((user) => user.id == idString);
if (typeof aUser != 'undefined') {
array.push(aUser);
}
return array;
}, []);
return someUsers;
}
search(term, options) {
options ||= {};
const { except } = options;
const allUsers = this.users;
const results = [];
for (const aUser of allUsers) {
if (
aUser.id != except &&
(aUser.name.search(term) >= 0 || aUser.username.search(term) >= 0)
) {
results.push(aUser);
}
}
return results;
}
}