Skip to content

Commit

Permalink
Merge pull request #1373 from AletheiaFact/deboucing-in-wikidata-topics
Browse files Browse the repository at this point in the history
Adding a debouncing mechanism when searching wikidata topics
  • Loading branch information
thesocialdev authored Oct 17, 2024
2 parents 07b4b1a + 0306db2 commit c1618c4
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/components/topics/TopicDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const TopicDisplay = ({
const [tags, setTags] = useState<any[]>([]);
const { t } = useTranslation();
const dispatch = useDispatch();
let timeout: NodeJS.Timeout;

useEffect(() => {
const inputValueFormatted = inputValue.map((inputValue) =>
inputValue?.value
? {
label: inputValue?.label.toLowerCase().replace(" ", "-"),
value: inputValue?.value,
}
label: inputValue?.label.toLowerCase().replace(" ", "-"),
value: inputValue?.value,
}
: inputValue
);

Expand All @@ -44,22 +45,25 @@ const TopicDisplay = ({
setTags(topicsArray?.concat(filterValues) || []);
}, [inputValue, topicsArray]);

const fetchTopicList = async (
const fetchTopicList = (
topic: string
): Promise<{ label: string; value: string }[]> => {
const topicSearchResults = await TopicsApi.getTopics({
topicName: topic,
t: t,
dispatch: dispatch,
});
) => new Promise<{ label: string; value: string }[]>((resolve) => {
if (timeout) clearTimeout(timeout);

return (
topicSearchResults?.map((topic) => ({
label: topic.name,
value: topic.wikidata,
})) || []
);
};
timeout = setTimeout(async () => {
const topicSearchResults = await TopicsApi.getTopics({
topicName: topic,
t,
dispatch,
});
resolve(
topicSearchResults?.map(({ name, wikidata }) => ({
label: name,
value: wikidata
})) || []
);
}, 1000);
});

const handleClose = async (removedTopicValue: any) => {
const newTopicsArray = topicsArray.filter(
Expand Down

0 comments on commit c1618c4

Please sign in to comment.