diff --git a/api/frontend_dic/src/components/Search.js b/api/frontend_dic/src/components/Search.js index eade2d8..56d2ce5 100644 --- a/api/frontend_dic/src/components/Search.js +++ b/api/frontend_dic/src/components/Search.js @@ -16,6 +16,14 @@ const GridBasicExample=()=> { const [isLoggedIn, setIsLoggedIn] = useState(false); + + function getMinDiff(startTimeStamp, endTimeStamp) { + const msInMinute = 60 * 1000; + + return Math.floor( + Math.abs(new Date(endTimeStamp) - new Date(startTimeStamp)) / msInMinute + ) + } const Go = (e) => { @@ -27,20 +35,51 @@ const GridBasicExample=()=> { 'X-RapidAPI-Host': 'wordsapiv1.p.rapidapi.com' } }; - - fetch('https://wordsapiv1.p.rapidapi.com/words/'+words+'/definitions', options) + + const fetchWordMeaning = () => { + fetch('https://wordsapiv1.p.rapidapi.com/words/'+words+'/definitions', options) .then(response => response.json()) .then(response => { setdefinitions([...response.definitions]) - // if(localStorage.getItem('searched')) { - // let searched = JSON.parse(localStorage.getItem('searched')); - // } else { - - // } - }) .catch(err => console.error(err)); - console.log(definitionss); + } + + if(localStorage.getItem('searched')) { + let searched = JSON.parse(localStorage.getItem('searched')); + if(searched.length > 1) { + console.log('greater than one') + let last_entry = searched[searched.length -1]; + if(getMinDiff(last_entry, Number(new Date())) < 2) { + console.log('not pass two mins') + alert(`Please you must wait for extra ${2 - getMinDiff(last_entry, Number(new Date()))}minute${(2 - getMinDiff(last_entry, Number(new Date())) > 1 ? 's' : '')} before making another search`); + return; + } else { + // send request + fetchWordMeaning(); + console.log('pass two mins') + let searched = [ + Number(new Date()) + ] + localStorage.setItem('searched', JSON.stringify(searched)) + } + console.log(last_entry) + } else { + fetchWordMeaning(); + console.log('just on item') + let searched = JSON.parse(localStorage.getItem('searched')); + searched.push(Number(new Date())) + localStorage.setItem('searched', JSON.stringify(searched)) + } + } else { + fetchWordMeaning(); + let searched = [ + Number(new Date()) + ] + localStorage.setItem('searched', JSON.stringify(searched)) + console.log(searched); + } + } // add to favorite DB const Fav = (e) => { @@ -61,8 +100,6 @@ const GridBasicExample=()=> { }).then((res) =>{ return res.json() }).then(data => { - - console.log(data) alert(data.message) }) @@ -82,7 +119,6 @@ const GridBasicExample=()=> { return res.json() }).then(data => { if(data.id){ - console.log(data.id) addFavourites('http://127.0.0.1:8000/api/add', data.id) } @@ -104,11 +140,10 @@ const GridBasicExample=()=> { }).then((res) =>{ return res.json() }).then(data => { - console.log(data) + // console.log(data) }) } const tokenId=localStorage.getItem('token') - console.log(tokenId); if(tokenId){ fetch('http://127.0.0.1:8000/api/user', { method: "GET", @@ -120,7 +155,6 @@ const GridBasicExample=()=> { }).then((res) =>{ return res.json() }).then(data => { - console.log(data.id) getFavourites('http://127.0.0.1:8000/api/data', data.id) }) }