docbrown/app/javascript/shared/components/UserStore.js
Joshua Wehner 6fe9d7d7a6
Allow org admins to add members as co-authors (#20008)
* Basics might be working?

* Stop propagating button clicks in autocomplete pills

* Better blank slate

* Better location to stop propagation

* Remove author_id from org co-authors

* Move UserStore and try testing it

* Remove extraneous comments

* OH... that's what that does!

* Very basic testing

* Re-organize javascripts

* Rename & re-org for testing

* Cleanup

* More tests

* Remove unnecessary nesting

* Coninuing to try to bump coverage

* Include /packs/ in code coverage metric

* Try tweaking jest coverage more?

We probably can't collect coverage from all of packs/* (because coverage is too low) but maybe we can try to opt-in for newer areas as we go?

* Relocate JS tests, for build & coverage

* User ID exception on search, not fetch

* Remove commented-out console.log

---------

Co-authored-by: Mac Siri <krairit.siri@gmail.com>
2023-09-15 08:12:41 -04:00

59 lines
1.4 KiB
JavaScript

export class UserStore {
constructor() {
this.users = [];
this.wasFetched = false;
}
fetch(url) {
const myStore = this;
return new Promise((resolve, _reject) => {
if (myStore.wasFetched) {
resolve();
} else {
window
.fetch(url)
.then((res) => res.json())
.then((data) => {
myStore.users = data.reduce((array, aUser) => {
array.push(aUser);
return array;
}, []);
myStore.wasFetched = true;
resolve();
})
.catch((error) => {
Honeybadger.notify(error);
resolve();
});
}
});
}
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;
}
}