Skip to content

Commit

Permalink
Merge pull request #80 from WildCodeSchool/hotfix/err-to-many-req-for…
Browse files Browse the repository at this point in the history
…-search

correction err too many request for search and keep the language for …
  • Loading branch information
YDrogen authored Feb 28, 2025
2 parents c3d1e64 + e742b21 commit f12e6dc
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/hook/use-display-list-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ export default function useDisplayListResult() {
setIsLoading(true);

let requestLanguage = '';
if (!isRandom && choiceLocalStorage === 'yes') {
if (isRandom && choiceLocalStorage === 'yes') {
requestLanguage = `&language=${storedChoiceLanguage}`;
}

if (searchValue) {
fetch(`https://newsdata.io/api/1/latest?apikey=${API_KEY}${requestLanguage}&q=${searchValue}`)
.then((response) => {
// if we are too many request update the variable
.then(async (response) => {
if (response.status === 429) {
setIsTooManyRequest(true);
throw new Error('TOO MANY REQUESTS');
return null;
}

if (!response.ok) {
throw new Error(`Erreur : ${response.status}`);
}

return response.json();
})
.then((data) => {
setIsLoading(false);
const articles = data.results;
setListSearch(articles);
setIsTooManyRequest(false);
if (data) {
setIsLoading(false);
const articles = data.results;
setListSearch(articles);
setIsTooManyRequest(false);
}
})

.finally(() => {
Expand Down

0 comments on commit f12e6dc

Please sign in to comment.