docbrown/app/javascript/packs/dashboards/convertCoauthorIdsToUsernameInputs.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

49 lines
1.4 KiB
JavaScript

import { h, render } from 'preact';
import { UsernameInput } from '../../shared/components/UsernameInput';
import { UserStore } from '../../shared/components/UserStore';
import '@utilities/document_ready';
export async function convertCoauthorIdsToUsernameInputs() {
const usernameFields = document.getElementsByClassName(
'article_org_co_author_ids_list',
);
const users = new UserStore();
for (const targetField of usernameFields) {
targetField.type = 'hidden';
const exceptAuthorId =
targetField.form.querySelector('#article_user_id').value;
const inputId = `auto${targetField.id}`;
const fetchUrl = targetField.dataset.fetchUsers;
const row = targetField.parentElement;
await users.fetch(fetchUrl).then(() => {
const value = users.matchingIds(targetField.value.split(','));
const fetchSuggestions = function (term) {
return users.search(term, { except: exceptAuthorId });
};
const handleSelectionsChanged = function (ids) {
targetField.value = ids;
};
render(
<UsernameInput
labelText="Add up to 4"
placeholder="Add up to 4..."
maxSelections={4}
inputId={inputId}
defaultValue={value}
fetchSuggestions={fetchSuggestions}
handleSelectionsChanged={handleSelectionsChanged}
/>,
row,
);
});
}
}
document.ready.then(() => {
convertCoauthorIdsToUsernameInputs();
});