Refactor editor tags selector to fetch all tag info in one network call (#16841)

* use bulk get of tags in editor

* prefer tag_ids if both tag_ids and tag_names are present

* undo changes to public api tags

* use new endpoint
This commit is contained in:
Suzanne Aitchison 2022-03-17 09:30:09 +00:00 committed by GitHub
parent 8379232bb3
commit 7c1993604c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,20 +29,19 @@ export const TagsField = ({ onInput, defaultValue, switchHelpContext }) => {
// Fetching further tag data allows us to display a richer UI
// This fetch only happens once on first component load
if (defaultValue && defaultValue !== '' && !defaultsLoaded) {
const tagNames = defaultValue.split(', ');
const tagNamesQueryString = defaultValue
.split(', ')
.reduce((queryString, nextTagName) => {
if (nextTagName) {
return queryString
? `${queryString}&tag_names[]=${nextTagName}`
: `tag_names[]=${nextTagName}`;
}
}, '');
const tagRequests = tagNames.map((tagName) =>
fetchSearch('tags', { name: tagName }).then(({ result = [] }) => {
const [potentialMatch = {}] = result;
return potentialMatch.name === tagName
? potentialMatch
: { name: tagName };
}),
);
Promise.all(tagRequests).then((data) => {
setDefaultSelections(data);
});
fetch(`/tags/bulk?${tagNamesQueryString}`)
.then((res) => res.json())
.then((data) => setDefaultSelections(data));
}
setDefaultsLoaded(true);
}, [defaultValue, defaultsLoaded]);